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

@ -50,12 +50,12 @@ impl Command for SubCommand {
let base: Spanned<f64> = call.req(engine_state, stack, 0)?;
if base.item <= 0.0f64 {
return Err(ShellError::UnsupportedInput(
"Base has to be greater 0".into(),
"value originates from here".into(),
head,
base.span,
));
return Err(ShellError::UnsupportedInput {
msg: "Base has to be greater 0".into(),
input: "value originates from here".into(),
msg_span: head,
input_span: base.span,
});
}
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
@ -103,13 +103,13 @@ fn operate(value: Value, head: Span, base: f64) -> Value {
if val <= 0.0 {
return Value::error(
ShellError::UnsupportedInput(
"'math log' undefined for values outside the open interval (0, Inf)."
ShellError::UnsupportedInput {
msg: "'math log' undefined for values outside the open interval (0, Inf)."
.into(),
"value originates from here".into(),
head,
span,
),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
},
span,
);
}

View File

@ -107,14 +107,14 @@ pub fn median(values: &[Value], span: Span, head: Span) -> Result<Value, ShellEr
match take {
Pick::Median => {
let idx = (values.len() as f64 / 2.0).floor() as usize;
let out = sorted.get(idx).ok_or_else(|| {
ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
head,
span,
)
})?;
let out = sorted
.get(idx)
.ok_or_else(|| ShellError::UnsupportedInput {
msg: "Empty input".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
})?;
Ok(out.clone())
}
Pick::MedianAverage => {
@ -123,25 +123,21 @@ pub fn median(values: &[Value], span: Span, head: Span) -> Result<Value, ShellEr
let left = sorted
.get(idx_start)
.ok_or_else(|| {
ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
head,
span,
)
.ok_or_else(|| ShellError::UnsupportedInput {
msg: "Empty input".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
})?
.clone();
let right = sorted
.get(idx_end)
.ok_or_else(|| {
ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
head,
span,
)
.ok_or_else(|| ShellError::UnsupportedInput {
msg: "Empty input".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
})?
.clone();

View File

@ -138,12 +138,12 @@ pub fn mode(values: &[Value], _span: Span, head: Span) -> Result<Value, ShellErr
Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Filesize))
}
Value::Error { error, .. } => Err(*error.clone()),
other => Err(ShellError::UnsupportedInput(
"Unable to give a result with this input".to_string(),
"value originates from here".into(),
head,
other.span(),
)),
other => Err(ShellError::UnsupportedInput {
msg: "Unable to give a result with this input".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: other.span(),
}),
})
.collect::<Result<Vec<HashableType>, ShellError>>()?;

View File

@ -23,13 +23,11 @@ pub fn reducer_for(command: Reduce) -> ReducerFunction {
pub fn max(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError> {
let mut biggest = data
.first()
.ok_or_else(|| {
ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
head,
span,
)
.ok_or_else(|| ShellError::UnsupportedInput {
msg: "Empty input".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
})?
.clone();
@ -54,13 +52,11 @@ pub fn max(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
pub fn min(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError> {
let mut smallest = data
.first()
.ok_or_else(|| {
ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
head,
span,
)
.ok_or_else(|| ShellError::UnsupportedInput {
msg: "Empty input".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
})?
.clone();
@ -96,12 +92,12 @@ pub fn sum(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
}
}
None => Err(ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
head,
span,
)),
None => Err(ShellError::UnsupportedInput {
msg: "Empty input".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
}),
}?;
for value in &data {
@ -114,12 +110,13 @@ pub fn sum(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
}
Value::Error { error, .. } => return Err(*error.clone()),
other => {
return Err(ShellError::UnsupportedInput(
"Attempted to compute the sum of a value that cannot be summed".to_string(),
"value originates from here".into(),
head,
other.span(),
));
return Err(ShellError::UnsupportedInput {
msg: "Attempted to compute the sum of a value that cannot be summed"
.to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: other.span(),
});
}
}
}
@ -137,12 +134,12 @@ pub fn product(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellE
_ => Ok(Value::nothing(head)),
}
}
None => Err(ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
head,
span,
)),
None => Err(ShellError::UnsupportedInput {
msg: "Empty input".to_string(),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
}),
}?;
for value in &data {
@ -152,13 +149,13 @@ pub fn product(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellE
}
Value::Error { error, .. } => return Err(*error.clone()),
other => {
return Err(ShellError::UnsupportedInput(
"Attempted to compute the product of a value that cannot be multiplied"
return Err(ShellError::UnsupportedInput {
msg: "Attempted to compute the product of a value that cannot be multiplied"
.to_string(),
"value originates from here".into(),
head,
other.span(),
));
input: "value originates from here".into(),
msg_span: head,
input_span: other.span(),
});
}
}
}

View File

@ -93,12 +93,12 @@ fn operate(value: Value, head: Span) -> Value {
fn error_negative_sqrt(head: Span, span: Span) -> Value {
Value::error(
ShellError::UnsupportedInput(
String::from("Can't square root a negative number"),
"value originates from here".into(),
head,
span,
),
ShellError::UnsupportedInput {
msg: String::from("Can't square root a negative number"),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
},
span,
)
}

View File

@ -49,12 +49,12 @@ fn helper_for_tables(
}
}
if column_totals.keys().len() == 0 {
return Err(ShellError::UnsupportedInput(
"Unable to give a result with this input".to_string(),
"value originates from here".into(),
name,
val_span,
));
return Err(ShellError::UnsupportedInput {
msg: "Unable to give a result with this input".to_string(),
input: "value originates from here".into(),
msg_span: name,
input_span: val_span,
});
}
Ok(Value::record(column_totals.into_iter().collect(), name))
@ -107,13 +107,13 @@ pub fn calculate(
}
PipelineData::Value(val, ..) => mf(&[val], span, name),
PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty { dst_span: name }),
val => Err(ShellError::UnsupportedInput(
"Only ints, floats, lists, records, or ranges are supported".into(),
"value originates from here".into(),
name,
// This requires both the ListStream and Empty match arms to be above it.
val.span()
val => Err(ShellError::UnsupportedInput {
msg: "Only ints, floats, lists, records, or ranges are supported".into(),
input: "value originates from here".into(),
msg_span: name,
input_span: val
.span()
.expect("non-Empty non-ListStream PipelineData had no span"),
)),
}),
}
}

View File

@ -65,12 +65,13 @@ fn sum_of_squares(values: &[Value], span: Span) -> Result<Value, ShellError> {
let v = match &value {
Value::Int { .. } | Value::Float { .. } => Ok(value),
Value::Error { error, .. } => Err(*error.clone()),
_ => Err(ShellError::UnsupportedInput(
"Attempted to compute the sum of squares of a non-int, non-float value".to_string(),
"value originates from here".into(),
span,
value.span(),
)),
_ => Err(ShellError::UnsupportedInput {
msg: "Attempted to compute the sum of squares of a non-int, non-float value"
.to_string(),
input: "value originates from here".into(),
msg_span: span,
input_span: value.span(),
}),
}?;
let v_squared = &v.mul(span, v, span)?;
sum_x2 = sum_x2.add(span, v_squared, span)?;