mirror of
https://github.com/nushell/nushell.git
synced 2025-05-17 00:10:53 +02:00
# Description As part of the refactor to split spans off of Value, this moves to using helper functions to create values, and using `.span()` instead of matching span out of Value directly. Hoping to get a few more helping hands to finish this, as there are a lot of commands to update :) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # 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. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> Co-authored-by: WindSoilder <windsoilder@outlook.com>
120 lines
4.2 KiB
Rust
120 lines
4.2 KiB
Rust
use indexmap::map::IndexMap;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::{IntoPipelineData, PipelineData, Record, ShellError, Span, Value};
|
|
|
|
pub fn run_with_function(
|
|
call: &Call,
|
|
input: PipelineData,
|
|
mf: impl Fn(&[Value], Span, Span) -> Result<Value, ShellError>,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let name = call.head;
|
|
let res = calculate(input, name, mf);
|
|
match res {
|
|
Ok(v) => Ok(v.into_pipeline_data()),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
fn helper_for_tables(
|
|
values: &[Value],
|
|
val_span: Span,
|
|
name: Span,
|
|
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 {
|
|
match val {
|
|
Value::Record { val, .. } => {
|
|
for (key, value) in val {
|
|
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.span(), 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, 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,
|
|
));
|
|
}
|
|
|
|
Ok(Value::record(column_totals.into_iter().collect(), name))
|
|
}
|
|
|
|
pub fn calculate(
|
|
values: PipelineData,
|
|
name: Span,
|
|
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>>(), span, name, mf)
|
|
}
|
|
PipelineData::Value(Value::List { ref vals, .. }, ..) => 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 { val: record, .. }, ..) => {
|
|
let new_vals: Result<Vec<Value>, ShellError> = record
|
|
.vals
|
|
.into_iter()
|
|
.map(|val| mf(&[val], span, name))
|
|
.collect();
|
|
match new_vals {
|
|
Ok(vec) => Ok(Value::record(
|
|
Record {
|
|
cols: record.cols,
|
|
vals: vec,
|
|
},
|
|
span,
|
|
)),
|
|
Err(err) => Err(err),
|
|
}
|
|
}
|
|
PipelineData::Value(Value::Range { val, .. }, ..) => {
|
|
let new_vals: Result<Vec<Value>, ShellError> = val
|
|
.into_range_iter(None)?
|
|
.map(|val| mf(&[val], span, name))
|
|
.collect();
|
|
|
|
mf(&new_vals?, span, name)
|
|
}
|
|
PipelineData::Value(val, ..) => mf(&[val], span, name),
|
|
PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty { dst_span: 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"),
|
|
)),
|
|
}
|
|
}
|