Convert ShellError::UnsupportedInput to named fields (#10971)

# Description

This is easy to do with rust-analyzer, but I didn't want to just pump
these all out without feedback.

Part of #10700

# User-Facing Changes

None

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

N/A

---------

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
This commit is contained in:
Eric Hodel
2023-11-07 14:25:32 -08:00
committed by GitHub
parent 45b02ce2ab
commit 7a3cbf43e8
56 changed files with 506 additions and 567 deletions

View File

@@ -95,12 +95,12 @@ fn collect_binary(input: PipelineData, span: Span) -> Result<Vec<u8>, ShellError
}
Some(Value::Error { error, .. }) => return Err(*error),
Some(x) => {
return Err(ShellError::UnsupportedInput(
"Expected binary from pipeline".to_string(),
"value originates from here".into(),
span,
x.span(),
))
return Err(ShellError::UnsupportedInput {
msg: "Expected binary from pipeline".to_string(),
input: "value originates from here".into(),
msg_span: span,
input_span: x.span(),
})
}
None => break,
}
@@ -117,13 +117,11 @@ fn from_ods(
let span = input.span();
let bytes = collect_binary(input, head)?;
let buf: Cursor<Vec<u8>> = Cursor::new(bytes);
let mut ods = Ods::<_>::new(buf).map_err(|_| {
ShellError::UnsupportedInput(
"Could not load ODS file".to_string(),
"value originates from here".into(),
head,
span.unwrap_or(head),
)
let mut ods = Ods::<_>::new(buf).map_err(|_| ShellError::UnsupportedInput {
msg: "Could not load ODS file".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span.unwrap_or(head),
})?;
let mut dict = IndexMap::new();
@@ -160,12 +158,12 @@ fn from_ods(
dict.insert(sheet_name, Value::list(sheet_output, head));
} else {
return Err(ShellError::UnsupportedInput(
"Could not load sheet".to_string(),
"value originates from here".into(),
head,
span.unwrap_or(head),
));
return Err(ShellError::UnsupportedInput {
msg: "Could not load sheet".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span.unwrap_or(head),
});
}
}

View File

@@ -94,12 +94,12 @@ fn collect_binary(input: PipelineData, span: Span) -> Result<Vec<u8>, ShellError
bytes.extend_from_slice(&b);
}
Some(x) => {
return Err(ShellError::UnsupportedInput(
"Expected binary from pipeline".to_string(),
"value originates from here".into(),
span,
x.span(),
))
return Err(ShellError::UnsupportedInput {
msg: "Expected binary from pipeline".to_string(),
input: "value originates from here".into(),
msg_span: span,
input_span: x.span(),
})
}
None => break,
}
@@ -116,13 +116,11 @@ fn from_xlsx(
let span = input.span();
let bytes = collect_binary(input, head)?;
let buf: Cursor<Vec<u8>> = Cursor::new(bytes);
let mut xlsx = Xlsx::<_>::new(buf).map_err(|_| {
ShellError::UnsupportedInput(
"Could not load XLSX file".to_string(),
"value originates from here".into(),
head,
span.unwrap_or(head),
)
let mut xlsx = Xlsx::<_>::new(buf).map_err(|_| ShellError::UnsupportedInput {
msg: "Could not load XLSX file".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span.unwrap_or(head),
})?;
let mut dict = IndexMap::new();
@@ -159,12 +157,12 @@ fn from_xlsx(
dict.insert(sheet_name, Value::list(sheet_output, head));
} else {
return Err(ShellError::UnsupportedInput(
"Could not load sheet".to_string(),
"value originates from here".into(),
head,
span.unwrap_or(head),
));
return Err(ShellError::UnsupportedInput {
msg: "Could not load sheet".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span.unwrap_or(head),
});
}
}

View File

@@ -81,12 +81,12 @@ fn convert_yaml_value_to_nu_value(
span: Span,
val_span: Span,
) -> Result<Value, ShellError> {
let err_not_compatible_number = ShellError::UnsupportedInput(
"Expected a nu-compatible number in YAML input".to_string(),
"value originates from here".into(),
span,
val_span,
);
let err_not_compatible_number = ShellError::UnsupportedInput {
msg: "Expected a nu-compatible number in YAML input".to_string(),
input: "value originates from here".into(),
msg_span: span,
input_span: val_span,
};
Ok(match v {
serde_yaml::Value::Bool(b) => Value::bool(*b, span),
serde_yaml::Value::Number(n) if n.is_i64() => {
@@ -109,12 +109,12 @@ fn convert_yaml_value_to_nu_value(
for (k, v) in t {
// A ShellError that we re-use multiple times in the Mapping scenario
let err_unexpected_map = ShellError::UnsupportedInput(
format!("Unexpected YAML:\nKey: {k:?}\nValue: {v:?}"),
"value originates from here".into(),
span,
val_span,
);
let err_unexpected_map = ShellError::UnsupportedInput {
msg: format!("Unexpected YAML:\nKey: {k:?}\nValue: {v:?}"),
input: "value originates from here".into(),
msg_span: span,
input_span: val_span,
};
match (k, v) {
(serde_yaml::Value::Number(k), _) => {
collected.insert(
@@ -198,14 +198,13 @@ pub fn from_yaml_string_to_value(
let mut documents = vec![];
for document in serde_yaml::Deserializer::from_str(&s) {
let v: serde_yaml::Value = serde_yaml::Value::deserialize(document).map_err(|x| {
ShellError::UnsupportedInput(
format!("Could not load YAML: {x}"),
"value originates from here".into(),
span,
val_span,
)
})?;
let v: serde_yaml::Value =
serde_yaml::Value::deserialize(document).map_err(|x| ShellError::UnsupportedInput {
msg: format!("Could not load YAML: {x}"),
input: "value originates from here".into(),
msg_span: span,
input_span: val_span,
})?;
documents.push(convert_yaml_value_to_nu_value(&v, span, val_span)?);
}

View File

@@ -126,12 +126,12 @@ fn to_string_tagged_value(
}
fn make_unsupported_input_error(value: &Value, head: Span, span: Span) -> ShellError {
ShellError::UnsupportedInput(
"Unexpected type".to_string(),
format!("input type: {:?}", value.get_type()),
head,
span,
)
ShellError::UnsupportedInput {
msg: "Unexpected type".to_string(),
input: format!("input type: {:?}", value.get_type()),
msg_span: head,
input_span: span,
}
}
pub fn find_non_record(values: &[Value]) -> Option<&Value> {

View File

@@ -131,28 +131,28 @@ pub fn value_to_string(
let mut s = String::with_capacity(2 * val.len());
for byte in val {
if write!(s, "{byte:02X}").is_err() {
return Err(ShellError::UnsupportedInput(
"could not convert binary to string".into(),
"value originates from here".into(),
span,
v.span(),
));
return Err(ShellError::UnsupportedInput {
msg: "could not convert binary to string".into(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
});
}
}
Ok(format!("0x[{s}]"))
}
Value::Block { .. } => Err(ShellError::UnsupportedInput(
"blocks are currently not nuon-compatible".into(),
"value originates from here".into(),
span,
v.span(),
)),
Value::Closure { .. } => Err(ShellError::UnsupportedInput(
"closures are currently not nuon-compatible".into(),
"value originates from here".into(),
span,
v.span(),
)),
Value::Block { .. } => Err(ShellError::UnsupportedInput {
msg: "blocks are currently not nuon-compatible".into(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
}),
Value::Closure { .. } => Err(ShellError::UnsupportedInput {
msg: "closures are currently not nuon-compatible".into(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
}),
Value::Bool { val, .. } => {
if *val {
Ok("true".to_string())
@@ -160,18 +160,18 @@ pub fn value_to_string(
Ok("false".to_string())
}
}
Value::CellPath { .. } => Err(ShellError::UnsupportedInput(
"cell-paths are currently not nuon-compatible".to_string(),
"value originates from here".into(),
span,
v.span(),
)),
Value::CustomValue { .. } => Err(ShellError::UnsupportedInput(
"custom values are currently not nuon-compatible".to_string(),
"value originates from here".into(),
span,
v.span(),
)),
Value::CellPath { .. } => Err(ShellError::UnsupportedInput {
msg: "cell-paths are currently not nuon-compatible".to_string(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
}),
Value::CustomValue { .. } => Err(ShellError::UnsupportedInput {
msg: "custom values are currently not nuon-compatible".to_string(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
}),
Value::Date { val, .. } => Ok(val.to_rfc3339()),
// FIXME: make durations use the shortest lossless representation.
Value::Duration { val, .. } => Ok(format!("{}ns", *val)),
@@ -241,12 +241,12 @@ pub fn value_to_string(
))
}
}
Value::MatchPattern { .. } => Err(ShellError::UnsupportedInput(
"match patterns are currently not nuon-compatible".to_string(),
"value originates from here".into(),
span,
v.span(),
)),
Value::MatchPattern { .. } => Err(ShellError::UnsupportedInput {
msg: "match patterns are currently not nuon-compatible".to_string(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
}),
Value::Nothing { .. } => Ok("null".to_string()),
Value::Range { val, .. } => Ok(format!(
"{}..{}{}",

View File

@@ -137,12 +137,12 @@ fn value_to_toml_value(
Value::Record { .. } => helper(engine_state, v),
// Propagate existing errors
Value::Error { error, .. } => Err(*error.clone()),
_ => Err(ShellError::UnsupportedInput(
format!("{:?} is not valid top-level TOML", v.get_type()),
"value originates from here".into(),
head,
v.span(),
)),
_ => Err(ShellError::UnsupportedInput {
msg: format!("{:?} is not valid top-level TOML", v.get_type()),
input: "value originates from here".into(),
msg_span: head,
input_span: v.span(),
}),
}
}