mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 05:38:19 +02:00
Standardise the use of ShellError::UnsupportedInput and ShellError::TypeMismatch and add spans to every instance of the former (#7217)
# Description * I was dismayed to discover recently that UnsupportedInput and TypeMismatch are used *extremely* inconsistently across the codebase. UnsupportedInput is sometimes used for input type-checks (as per the name!!), but *also* used for argument type-checks. TypeMismatch is also used for both. I thus devised the following standard: input type-checking *only* uses UnsupportedInput, and argument type-checking *only* uses TypeMismatch. Moreover, to differentiate them, UnsupportedInput now has *two* error arrows (spans), one pointing at the command and the other at the input origin, while TypeMismatch only has the one (because the command should always be nearby) * In order to apply that standard, a very large number of UnsupportedInput uses were changed so that the input's span could be retrieved and delivered to it. * Additionally, I noticed many places where **errors are not propagated correctly**: there are lots of `match` sites which take a Value::Error, then throw it away and replace it with a new Value::Error with less/misleading information (such as reporting the error as an "incorrect type"). I believe that the earliest errors are the most important, and should always be propagated where possible. * Also, to standardise one broad subset of UnsupportedInput error messages, who all used slightly different wordings of "expected `<type>`, got `<type>`", I created OnlySupportsThisInputType as a variant of it. * Finally, a bunch of error sites that had "repeated spans" - i.e. where an error expected two spans, but `call.head` was given for both - were fixed to use different spans. # Example BEFORE ``` 〉20b | str starts-with 'a' Error: nu:🐚:unsupported_input (link) × Unsupported input ╭─[entry #31:1:1] 1 │ 20b | str starts-with 'a' · ┬ · ╰── Input's type is filesize. This command only works with strings. ╰──── 〉'a' | math cos Error: nu:🐚:unsupported_input (link) × Unsupported input ╭─[entry #33:1:1] 1 │ 'a' | math cos · ─┬─ · ╰── Only numerical values are supported, input type: String ╰──── 〉0x[12] | encode utf8 Error: nu:🐚:unsupported_input (link) × Unsupported input ╭─[entry #38:1:1] 1 │ 0x[12] | encode utf8 · ───┬── · ╰── non-string input ╰──── ``` AFTER ``` 〉20b | str starts-with 'a' Error: nu:🐚:pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #1:1:1] 1 │ 20b | str starts-with 'a' · ┬ ───────┬─────── · │ ╰── only string input data is supported · ╰── input type: filesize ╰──── 〉'a' | math cos Error: nu:🐚:pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #2:1:1] 1 │ 'a' | math cos · ─┬─ ────┬─── · │ ╰── only numeric input data is supported · ╰── input type: string ╰──── 〉0x[12] | encode utf8 Error: nu:🐚:pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #3:1:1] 1 │ 0x[12] | encode utf8 · ───┬── ───┬── · │ ╰── only string input data is supported · ╰── input type: binary ╰──── ``` # User-Facing Changes Various error messages suddenly make more sense (i.e. have two arrows instead of one). # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
@ -66,13 +66,13 @@ fn abs_helper(val: Value, head: Span) -> Value {
|
||||
val: val.abs(),
|
||||
span,
|
||||
},
|
||||
Value::Error { .. } => val,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -35,6 +35,10 @@ impl Command for SubCommand {
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
let use_degrees = call.has_flag("degrees");
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -75,18 +79,20 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
|
||||
Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
"'arccos' undefined for values outside the closed interval [-1, 1].".into(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -65,18 +69,20 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
"'arccosh' undefined for values below 1.".into(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -35,6 +35,10 @@ impl Command for SubCommand {
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
let use_degrees = call.has_flag("degrees");
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -76,18 +80,20 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
|
||||
Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
"'arcsin' undefined for values outside the closed interval [-1, 1].".into(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -61,13 +65,13 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
|
||||
Value::Float { val, span }
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -35,6 +35,10 @@ impl Command for SubCommand {
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
let use_degrees = call.has_flag("degrees");
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -72,13 +76,13 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
|
||||
|
||||
Value::Float { val, span }
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -65,18 +69,20 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
"'arctanh' undefined for values outside the open interval (-1, 1).".into(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -45,9 +45,9 @@ impl Command for SubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn average(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
pub fn average(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
|
||||
let sum = reducer_for(Reduce::Summation);
|
||||
let total = &sum(Value::int(0, *head), values.to_vec(), *head)?;
|
||||
let total = &sum(Value::int(0, *head), values.to_vec(), span, *head)?;
|
||||
match total {
|
||||
Value::Filesize { val, span } => Ok(Value::Filesize {
|
||||
val: val / values.len() as i64,
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -58,13 +62,13 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
val: val.ceil(),
|
||||
span,
|
||||
},
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -35,6 +35,10 @@ impl Command for SubCommand {
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
let use_degrees = call.has_flag("degrees");
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -82,13 +86,13 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
|
||||
span,
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -63,13 +67,13 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
span,
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -63,6 +63,8 @@ pub fn eval(
|
||||
Ok(value) => Ok(PipelineData::Value(value, None)),
|
||||
Err(err) => Err(ShellError::UnsupportedInput(
|
||||
format!("Math evaluation error: {}", err),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
expr.span,
|
||||
)),
|
||||
}
|
||||
@ -71,25 +73,19 @@ pub fn eval(
|
||||
return Ok(input);
|
||||
}
|
||||
input.map(
|
||||
move |val| {
|
||||
if let Ok(string) = val.as_string() {
|
||||
match parse(&string, &val.span().unwrap_or(head)) {
|
||||
Ok(value) => value,
|
||||
Err(err) => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!("Math evaluation error: {}", err),
|
||||
val.span().unwrap_or(head),
|
||||
),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
Value::Error {
|
||||
move |val| match val.as_string() {
|
||||
Ok(string) => match parse(&string, &val.span().unwrap_or(head)) {
|
||||
Ok(value) => value,
|
||||
Err(err) => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
"Expected a string from pipeline".to_string(),
|
||||
val.span().unwrap_or(head),
|
||||
format!("Math evaluation error: {}", err),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
val.expect_span(),
|
||||
),
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
Err(error) => Value::Error { error },
|
||||
},
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -58,13 +62,13 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
val: val.floor(),
|
||||
span,
|
||||
},
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -65,18 +69,20 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
"'ln' undefined for values outside the open interval (0, Inf).".into(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -46,10 +46,15 @@ impl Command for SubCommand {
|
||||
if base.item <= 0.0f64 {
|
||||
return Err(ShellError::UnsupportedInput(
|
||||
"Base has to be greater 0".into(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
base.span,
|
||||
));
|
||||
}
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
let base = base.item;
|
||||
input.map(
|
||||
move |value| operate(value, head, base),
|
||||
@ -94,6 +99,8 @@ fn operate(value: Value, head: Span, base: f64) -> Value {
|
||||
error: ShellError::UnsupportedInput(
|
||||
"'math log' undefined for values outside the open interval (0, Inf)."
|
||||
.into(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
),
|
||||
};
|
||||
@ -109,13 +116,13 @@ fn operate(value: Value, head: Span, base: f64) -> Value {
|
||||
|
||||
Value::Float { val, span }
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -59,9 +59,9 @@ impl Command for SubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn maximum(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
pub fn maximum(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
|
||||
let max_func = reducer_for(Reduce::Maximum);
|
||||
max_func(Value::nothing(*head), values.to_vec(), *head)
|
||||
max_func(Value::nothing(*head), values.to_vec(), span, *head)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -66,7 +66,7 @@ enum Pick {
|
||||
Median,
|
||||
}
|
||||
|
||||
pub fn median(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
pub fn median(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
|
||||
let take = if values.len() % 2 == 0 {
|
||||
Pick::MedianAverage
|
||||
} else {
|
||||
@ -103,9 +103,14 @@ pub fn median(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
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(), *head))?;
|
||||
let out = sorted.get(idx).ok_or_else(|| {
|
||||
ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
"value originates from here".into(),
|
||||
*head,
|
||||
span,
|
||||
)
|
||||
})?;
|
||||
Ok(out.clone())
|
||||
}
|
||||
Pick::MedianAverage => {
|
||||
@ -114,15 +119,29 @@ pub fn median(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
|
||||
let left = sorted
|
||||
.get(idx_start)
|
||||
.ok_or_else(|| ShellError::UnsupportedInput("Empty input".to_string(), *head))?
|
||||
.ok_or_else(|| {
|
||||
ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
"value originates from here".into(),
|
||||
*head,
|
||||
span,
|
||||
)
|
||||
})?
|
||||
.clone();
|
||||
|
||||
let right = sorted
|
||||
.get(idx_end)
|
||||
.ok_or_else(|| ShellError::UnsupportedInput("Empty input".to_string(), *head))?
|
||||
.ok_or_else(|| {
|
||||
ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
"value originates from here".into(),
|
||||
*head,
|
||||
span,
|
||||
)
|
||||
})?
|
||||
.clone();
|
||||
|
||||
average(&[left, right], head)
|
||||
average(&[left, right], span, head)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,9 +59,9 @@ impl Command for SubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn minimum(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
pub fn minimum(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
|
||||
let min_func = reducer_for(Reduce::Minimum);
|
||||
min_func(Value::nothing(*head), values.to_vec(), *head)
|
||||
min_func(Value::nothing(*head), values.to_vec(), span, *head)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -97,7 +97,7 @@ impl Command for SubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mode(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
pub fn mode(values: &[Value], _span: Span, head: &Span) -> Result<Value, ShellError> {
|
||||
if let Some(Err(values)) = values
|
||||
.windows(2)
|
||||
.map(|elem| {
|
||||
@ -132,9 +132,12 @@ pub fn mode(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
Value::Filesize { val, .. } => {
|
||||
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(),
|
||||
other.span()?,
|
||||
"value originates from here".into(),
|
||||
*head,
|
||||
other.expect_span(),
|
||||
)),
|
||||
})
|
||||
.collect::<Result<Vec<HashableType>, ShellError>>()?;
|
||||
|
@ -46,9 +46,9 @@ impl Command for SubCommand {
|
||||
}
|
||||
|
||||
/// Calculate product of given values
|
||||
pub fn product(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
pub fn product(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
|
||||
let product_func = reducer_for(Reduce::Product);
|
||||
product_func(Value::nothing(*head), values.to_vec(), *head)
|
||||
product_func(Value::nothing(*head), values.to_vec(), span, *head)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -9,21 +9,28 @@ pub enum Reduce {
|
||||
}
|
||||
|
||||
pub type ReducerFunction =
|
||||
Box<dyn Fn(Value, Vec<Value>, Span) -> Result<Value, ShellError> + Send + Sync + 'static>;
|
||||
Box<dyn Fn(Value, Vec<Value>, Span, Span) -> Result<Value, ShellError> + Send + Sync + 'static>;
|
||||
|
||||
pub fn reducer_for(command: Reduce) -> ReducerFunction {
|
||||
match command {
|
||||
Reduce::Summation => Box::new(|_, values, head| sum(values, head)),
|
||||
Reduce::Product => Box::new(|_, values, head| product(values, head)),
|
||||
Reduce::Minimum => Box::new(|_, values, head| min(values, head)),
|
||||
Reduce::Maximum => Box::new(|_, values, head| max(values, head)),
|
||||
Reduce::Summation => Box::new(|_, values, span, head| sum(values, span, head)),
|
||||
Reduce::Product => Box::new(|_, values, span, head| product(values, span, head)),
|
||||
Reduce::Minimum => Box::new(|_, values, span, head| min(values, span, head)),
|
||||
Reduce::Maximum => Box::new(|_, values, span, head| max(values, span, head)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn max(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
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(), head))?
|
||||
.ok_or_else(|| {
|
||||
ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
)
|
||||
})?
|
||||
.clone();
|
||||
|
||||
for value in &data {
|
||||
@ -44,10 +51,17 @@ pub fn max(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
Ok(biggest)
|
||||
}
|
||||
|
||||
pub fn min(data: Vec<Value>, 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(), head))?
|
||||
.ok_or_else(|| {
|
||||
ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
)
|
||||
})?
|
||||
.clone();
|
||||
|
||||
for value in &data {
|
||||
@ -68,7 +82,7 @@ pub fn min(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
Ok(smallest)
|
||||
}
|
||||
|
||||
pub fn sum(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
pub fn sum(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError> {
|
||||
let initial_value = data.get(0);
|
||||
|
||||
let mut acc = match initial_value {
|
||||
@ -83,7 +97,9 @@ pub fn sum(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::int(0, *span)),
|
||||
None => Err(ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
)),
|
||||
_ => Ok(Value::nothing(head)),
|
||||
}?;
|
||||
@ -96,10 +112,13 @@ pub fn sum(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
| Value::Duration { .. } => {
|
||||
acc = acc.add(head, value, head)?;
|
||||
}
|
||||
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(),
|
||||
other.span().unwrap_or(head),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -107,14 +126,16 @@ pub fn sum(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
Ok(acc)
|
||||
}
|
||||
|
||||
pub fn product(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
pub fn product(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError> {
|
||||
let initial_value = data.get(0);
|
||||
|
||||
let mut acc = match initial_value {
|
||||
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::int(1, *span)),
|
||||
None => Err(ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
)),
|
||||
_ => Ok(Value::nothing(head)),
|
||||
}?;
|
||||
@ -124,11 +145,14 @@ pub fn product(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
Value::Int { .. } | Value::Float { .. } => {
|
||||
acc = acc.mul(head, value, head)?;
|
||||
}
|
||||
Value::Error { error } => return Err(error.clone()),
|
||||
other => {
|
||||
return Err(ShellError::UnsupportedInput(
|
||||
"Attempted to compute the product of a value that cannot be multiplied"
|
||||
.to_string(),
|
||||
other.span().unwrap_or(head),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,10 @@ impl Command for SubCommand {
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let precision_param: Option<i64> = call.get_flag(engine_state, stack, "precision")?;
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, precision_param),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -89,13 +93,13 @@ fn operate(value: Value, head: Span, precision: Option<i64>) -> Value {
|
||||
},
|
||||
},
|
||||
Value::Int { .. } => value,
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -35,6 +35,10 @@ impl Command for SubCommand {
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
let use_degrees = call.has_flag("degrees");
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -82,13 +86,13 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
|
||||
span,
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -63,13 +67,13 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
span,
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -56,33 +60,35 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
Value::Int { val, span } => {
|
||||
let squared = (val as f64).sqrt();
|
||||
if squared.is_nan() {
|
||||
return error_negative_sqrt(span);
|
||||
return error_negative_sqrt(head, span);
|
||||
}
|
||||
Value::Float { val: squared, span }
|
||||
}
|
||||
Value::Float { val, span } => {
|
||||
let squared = val.sqrt();
|
||||
if squared.is_nan() {
|
||||
return error_negative_sqrt(span);
|
||||
return error_negative_sqrt(head, span);
|
||||
}
|
||||
Value::Float { val: squared, span }
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn error_negative_sqrt(span: Span) -> Value {
|
||||
fn error_negative_sqrt(head: Span, span: Span) -> Value {
|
||||
Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
String::from("Can't square root a negative number"),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
),
|
||||
}
|
||||
|
@ -65,17 +65,21 @@ impl Command for SubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compute_stddev(sample: bool) -> impl Fn(&[Value], &Span) -> Result<Value, ShellError> {
|
||||
move |values: &[Value], span: &Span| {
|
||||
let variance = variance(sample)(values, span);
|
||||
pub fn compute_stddev(sample: bool) -> impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError> {
|
||||
move |values: &[Value], span: Span, head: &Span| {
|
||||
let variance = variance(sample)(values, span, head);
|
||||
match variance {
|
||||
Ok(Value::Float { val, span }) => Ok(Value::Float { val: val.sqrt(), span }),
|
||||
Ok(Value::Int { val, span }) => Ok(Value::Float { val: (val as f64).sqrt(), span }),
|
||||
Err(ShellError::UnsupportedInput(_, err_span)) => Err(ShellError::UnsupportedInput(
|
||||
"Attempted to compute the standard deviation with an item that cannot be used for that.".to_string(),
|
||||
err_span,
|
||||
)),
|
||||
other => other
|
||||
Ok(Value::Float { val, span }) => Ok(Value::Float {
|
||||
val: val.sqrt(),
|
||||
span,
|
||||
}),
|
||||
Ok(Value::Int { val, span }) => Ok(Value::Float {
|
||||
val: (val as f64).sqrt(),
|
||||
span,
|
||||
}),
|
||||
// variance() produces its own usable error, which can simply be propagated.
|
||||
Err(e) => Err(e),
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -52,9 +52,9 @@ impl Command for SubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn summation(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
pub fn summation(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
|
||||
let sum_func = reducer_for(Reduce::Summation);
|
||||
sum_func(Value::nothing(*head), values.to_vec(), *head)
|
||||
sum_func(Value::nothing(*head), values.to_vec(), span, *head)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -35,6 +35,10 @@ impl Command for SubCommand {
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
let use_degrees = call.has_flag("degrees");
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -80,13 +84,13 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
|
||||
span,
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -33,6 +33,10 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
@ -62,13 +66,13 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
span,
|
||||
}
|
||||
}
|
||||
Value::Error { .. } => value,
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Only numerical values are supported, input type: {:?}",
|
||||
other.get_type()
|
||||
),
|
||||
other.span().unwrap_or(head),
|
||||
error: ShellError::OnlySupportsThisInputType(
|
||||
"numeric".into(),
|
||||
other.get_type().to_string(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Span, Spanned, Val
|
||||
pub fn run_with_function(
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
mf: impl Fn(&[Value], &Span) -> Result<Value, ShellError>,
|
||||
mf: impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError>,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let name = call.head;
|
||||
let res = calculate(input, name, mf);
|
||||
@ -17,36 +17,43 @@ pub fn run_with_function(
|
||||
|
||||
fn helper_for_tables(
|
||||
values: &[Value],
|
||||
val_span: Span,
|
||||
name: Span,
|
||||
mf: impl Fn(&[Value], &Span) -> Result<Value, ShellError>,
|
||||
mf: impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError>,
|
||||
) -> Result<Value, ShellError> {
|
||||
// If we are not dealing with Primitives, then perhaps we are dealing with a table
|
||||
// Create a key for each column name
|
||||
let mut column_values = IndexMap::new();
|
||||
for val in values {
|
||||
if let Value::Record { cols, vals, .. } = val {
|
||||
for (key, value) in cols.iter().zip(vals.iter()) {
|
||||
column_values
|
||||
.entry(key.clone())
|
||||
.and_modify(|v: &mut Vec<Value>| v.push(value.clone()))
|
||||
.or_insert_with(|| vec![value.clone()]);
|
||||
match val {
|
||||
Value::Record { cols, vals, .. } => {
|
||||
for (key, value) in cols.iter().zip(vals.iter()) {
|
||||
column_values
|
||||
.entry(key.clone())
|
||||
.and_modify(|v: &mut Vec<Value>| v.push(value.clone()))
|
||||
.or_insert_with(|| vec![value.clone()]);
|
||||
}
|
||||
}
|
||||
Value::Error { error } => return Err(error.clone()),
|
||||
_ => {
|
||||
//Turns out we are not dealing with a table
|
||||
return mf(values, val.expect_span(), &name);
|
||||
}
|
||||
} else {
|
||||
//Turns out we are not dealing with a table
|
||||
return mf(values, &name);
|
||||
}
|
||||
}
|
||||
// The mathematical function operates over the columns of the table
|
||||
let mut column_totals = IndexMap::new();
|
||||
for (col_name, col_vals) in column_values {
|
||||
if let Ok(out) = mf(&col_vals, &name) {
|
||||
if let Ok(out) = mf(&col_vals, val_span, &name) {
|
||||
column_totals.insert(col_name, out);
|
||||
}
|
||||
}
|
||||
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,
|
||||
));
|
||||
}
|
||||
|
||||
@ -59,17 +66,28 @@ fn helper_for_tables(
|
||||
pub fn calculate(
|
||||
values: PipelineData,
|
||||
name: Span,
|
||||
mf: impl Fn(&[Value], &Span) -> Result<Value, ShellError>,
|
||||
mf: impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError>,
|
||||
) -> Result<Value, ShellError> {
|
||||
// TODO implement spans for ListStream, thus negating the need for unwrap_or().
|
||||
let span = values.span().unwrap_or(name);
|
||||
match values {
|
||||
PipelineData::ListStream(s, ..) => helper_for_tables(&s.collect::<Vec<Value>>(), name, mf),
|
||||
PipelineData::Value(Value::List { ref vals, .. }, ..) => match &vals[..] {
|
||||
[Value::Record { .. }, _end @ ..] => helper_for_tables(vals, name, mf),
|
||||
_ => mf(vals, &name),
|
||||
PipelineData::ListStream(s, ..) => {
|
||||
helper_for_tables(&s.collect::<Vec<Value>>(), span, name, mf)
|
||||
}
|
||||
PipelineData::Value(Value::List { ref vals, span }, ..) => match &vals[..] {
|
||||
[Value::Record { .. }, _end @ ..] => helper_for_tables(
|
||||
vals,
|
||||
values.span().expect("PipelineData::Value had no span"),
|
||||
name,
|
||||
mf,
|
||||
),
|
||||
_ => mf(vals, span, &name),
|
||||
},
|
||||
PipelineData::Value(Value::Record { vals, cols, span }, ..) => {
|
||||
let new_vals: Result<Vec<Value>, ShellError> =
|
||||
vals.into_iter().map(|val| mf(&[val], &name)).collect();
|
||||
let new_vals: Result<Vec<Value>, ShellError> = vals
|
||||
.into_iter()
|
||||
.map(|val| mf(&[val], span, &name))
|
||||
.collect();
|
||||
match new_vals {
|
||||
Ok(vec) => Ok(Value::Record {
|
||||
cols,
|
||||
@ -79,18 +97,23 @@ pub fn calculate(
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
PipelineData::Value(Value::Range { val, .. }, ..) => {
|
||||
PipelineData::Value(Value::Range { val, span, .. }, ..) => {
|
||||
let new_vals: Result<Vec<Value>, ShellError> = val
|
||||
.into_range_iter(None)?
|
||||
.map(|val| mf(&[val], &name))
|
||||
.map(|val| mf(&[val], span, &name))
|
||||
.collect();
|
||||
|
||||
mf(&new_vals?, &name)
|
||||
mf(&new_vals?, span, &name)
|
||||
}
|
||||
PipelineData::Value(val, ..) => mf(&[val], &name),
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
"Input data is not supported by this command.".to_string(),
|
||||
PipelineData::Value(val, ..) => mf(&[val], span, &name),
|
||||
PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty(name)),
|
||||
val => Err(ShellError::UnsupportedInput(
|
||||
"Only integers, 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()
|
||||
.expect("non-Empty non-ListStream PipelineData had no span"),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
@ -63,14 +63,15 @@ fn sum_of_squares(values: &[Value], span: &Span) -> Result<Value, ShellError> {
|
||||
let mut sum_x2 = Value::int(0, *span);
|
||||
for value in values {
|
||||
let v = match &value {
|
||||
Value::Int { .. }
|
||||
| Value::Float { .. } => {
|
||||
Ok(value)
|
||||
},
|
||||
Value::Int { .. } | Value::Float { .. } => Ok(value),
|
||||
Value::Error { error } => Err(error.clone()),
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
"Attempted to compute the sum of squared values of a value that cannot be summed or squared.".to_string(),
|
||||
value.span().unwrap_or(*span),
|
||||
))
|
||||
"Attempted to compute the sum of squares of a non-integer, non-float value"
|
||||
.to_string(),
|
||||
"value originates from here".into(),
|
||||
*span,
|
||||
value.expect_span(),
|
||||
)),
|
||||
}?;
|
||||
let v_squared = &v.mul(*span, v, *span)?;
|
||||
sum_x2 = sum_x2.add(*span, v_squared, *span)?;
|
||||
@ -85,24 +86,19 @@ fn sum_of_squares(values: &[Value], span: &Span) -> Result<Value, ShellError> {
|
||||
Ok(ss)
|
||||
}
|
||||
|
||||
pub fn compute_variance(sample: bool) -> impl Fn(&[Value], &Span) -> Result<Value, ShellError> {
|
||||
move |values: &[Value], span: &Span| {
|
||||
pub fn compute_variance(
|
||||
sample: bool,
|
||||
) -> impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError> {
|
||||
move |values: &[Value], span: Span, head: &Span| {
|
||||
let n = if sample {
|
||||
values.len() - 1
|
||||
} else {
|
||||
values.len()
|
||||
};
|
||||
let sum_of_squares = sum_of_squares(values, span);
|
||||
let ss = match sum_of_squares {
|
||||
Err(ShellError::UnsupportedInput(_, err_span)) => Err(ShellError::UnsupportedInput(
|
||||
"Attempted to compute the variance with an item that cannot be used for that."
|
||||
.to_string(),
|
||||
err_span,
|
||||
)),
|
||||
other => other,
|
||||
}?;
|
||||
let n = Value::int(n as i64, *span);
|
||||
ss.div(*span, &n, *span)
|
||||
// sum_of_squares() needs the span of the original value, not the call head.
|
||||
let ss = sum_of_squares(values, &span)?;
|
||||
let n = Value::int(n as i64, *head);
|
||||
ss.div(*head, &n, *head)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user