mirror of
https://github.com/nushell/nushell.git
synced 2025-08-16 03:37:51 +02:00
Move Value to helpers, separate span call (#10121)
# 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>
This commit is contained in:
@ -168,8 +168,5 @@ fn create_default_value() -> Value {
|
||||
)
|
||||
};
|
||||
|
||||
Value::List {
|
||||
vals: vec![record(0), record(1), record(2)],
|
||||
span,
|
||||
}
|
||||
Value::list(vec![record(0), record(1), record(2)], span)
|
||||
}
|
||||
|
@ -172,10 +172,7 @@ fn help_frame_data(
|
||||
Value::record(Record { cols, vals }, NuSpan::unknown())
|
||||
})
|
||||
.collect();
|
||||
let commands = Value::List {
|
||||
vals: commands,
|
||||
span: NuSpan::unknown(),
|
||||
};
|
||||
let commands = Value::list(commands, NuSpan::unknown());
|
||||
|
||||
collect_input(commands)
|
||||
}
|
||||
@ -199,10 +196,7 @@ fn help_manual_data(manual: &HelpManual, aliases: &[String]) -> (Vec<String>, Ve
|
||||
})
|
||||
.collect();
|
||||
|
||||
let arguments = Value::List {
|
||||
vals: arguments,
|
||||
span: NuSpan::unknown(),
|
||||
};
|
||||
let arguments = Value::list(arguments, NuSpan::unknown());
|
||||
|
||||
let examples = manual
|
||||
.examples
|
||||
@ -217,10 +211,7 @@ fn help_manual_data(manual: &HelpManual, aliases: &[String]) -> (Vec<String>, Ve
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let examples = Value::List {
|
||||
vals: examples,
|
||||
span: NuSpan::unknown(),
|
||||
};
|
||||
let examples = Value::list(examples, NuSpan::unknown());
|
||||
|
||||
let inputs = manual
|
||||
.input
|
||||
@ -236,10 +227,7 @@ fn help_manual_data(manual: &HelpManual, aliases: &[String]) -> (Vec<String>, Ve
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let inputs = Value::List {
|
||||
vals: inputs,
|
||||
span: NuSpan::unknown(),
|
||||
};
|
||||
let inputs = Value::list(inputs, NuSpan::unknown());
|
||||
|
||||
let configuration = manual
|
||||
.config_options
|
||||
@ -258,10 +246,7 @@ fn help_manual_data(manual: &HelpManual, aliases: &[String]) -> (Vec<String>, Ve
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let values = Value::List {
|
||||
vals: values,
|
||||
span: NuSpan::unknown(),
|
||||
};
|
||||
let values = Value::list(values, NuSpan::unknown());
|
||||
|
||||
Value::record(
|
||||
record! {
|
||||
@ -274,10 +259,7 @@ fn help_manual_data(manual: &HelpManual, aliases: &[String]) -> (Vec<String>, Ve
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let configuration = Value::List {
|
||||
vals: configuration,
|
||||
span: NuSpan::unknown(),
|
||||
};
|
||||
let configuration = Value::list(configuration, NuSpan::unknown());
|
||||
|
||||
let name = nu_str(&manual.name);
|
||||
let aliases = nu_str(&aliases.join(", "));
|
||||
|
@ -98,10 +98,7 @@ impl Command for Explore {
|
||||
Ok(Some(value)) => Ok(PipelineData::Value(value, None)),
|
||||
Ok(None) => Ok(PipelineData::Value(Value::default(), None)),
|
||||
Err(err) => Ok(PipelineData::Value(
|
||||
Value::Error {
|
||||
error: Box::new(err.into()),
|
||||
span: call.head,
|
||||
},
|
||||
Value::error(err.into(), call.head),
|
||||
None,
|
||||
)),
|
||||
}
|
||||
|
@ -15,9 +15,10 @@ pub fn try_build_table(
|
||||
style_computer: &StyleComputer,
|
||||
value: Value,
|
||||
) -> String {
|
||||
let span = value.span();
|
||||
match value {
|
||||
Value::List { vals, span } => try_build_list(vals, ctrlc, config, span, style_computer),
|
||||
Value::Record { val, span } => try_build_map(val, span, style_computer, ctrlc, config),
|
||||
Value::List { vals, .. } => try_build_list(vals, ctrlc, config, span, style_computer),
|
||||
Value::Record { val, .. } => try_build_map(val, span, style_computer, ctrlc, config),
|
||||
val if matches!(val, Value::String { .. }) => {
|
||||
nu_value_to_string_clean(&val, config, style_computer).0
|
||||
}
|
||||
@ -71,7 +72,7 @@ fn try_build_list(
|
||||
Ok(Some(out)) => out,
|
||||
Ok(None) | Err(_) => {
|
||||
// it means that the list is empty
|
||||
nu_value_to_string(&Value::List { vals, span }, config, style_computer).0
|
||||
nu_value_to_string(&Value::list(vals, span), config, style_computer).0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -54,10 +54,7 @@ fn collect_external_stream(
|
||||
let mut data = vec![];
|
||||
if let Some(stdout) = stdout {
|
||||
let value = stdout.into_string().map_or_else(
|
||||
|error| Value::Error {
|
||||
error: Box::new(error),
|
||||
span,
|
||||
},
|
||||
|error| Value::error(error, span),
|
||||
|string| Value::string(string.item, span),
|
||||
);
|
||||
|
||||
@ -66,10 +63,7 @@ fn collect_external_stream(
|
||||
}
|
||||
if let Some(stderr) = stderr {
|
||||
let value = stderr.into_string().map_or_else(
|
||||
|error| Value::Error {
|
||||
error: Box::new(error),
|
||||
span,
|
||||
},
|
||||
|error| Value::error(error, span),
|
||||
|string| Value::string(string.item, span),
|
||||
);
|
||||
|
||||
@ -78,7 +72,7 @@ fn collect_external_stream(
|
||||
}
|
||||
if let Some(exit_code) = exit_code {
|
||||
let list = exit_code.collect::<Vec<_>>();
|
||||
let val = Value::List { vals: list, span };
|
||||
let val = Value::list(list, span);
|
||||
|
||||
columns.push(String::from("exit_code"));
|
||||
data.push(val);
|
||||
@ -94,6 +88,7 @@ fn collect_external_stream(
|
||||
|
||||
/// Try to build column names and a table grid.
|
||||
pub fn collect_input(value: Value) -> (Vec<String>, Vec<Vec<Value>>) {
|
||||
let span = value.span();
|
||||
match value {
|
||||
Value::Record { val: record, .. } => (record.cols, vec![record.vals]),
|
||||
Value::List { vals, .. } => {
|
||||
@ -106,23 +101,20 @@ pub fn collect_input(value: Value) -> (Vec<String>, Vec<Vec<Value>>) {
|
||||
|
||||
(columns, data)
|
||||
}
|
||||
Value::String { val, span } => {
|
||||
Value::String { val, .. } => {
|
||||
let lines = val
|
||||
.lines()
|
||||
.map(|line| Value::String {
|
||||
val: line.to_string(),
|
||||
span,
|
||||
})
|
||||
.map(|line| Value::string(line, span))
|
||||
.map(|val| vec![val])
|
||||
.collect();
|
||||
|
||||
(vec![String::from("")], lines)
|
||||
}
|
||||
Value::LazyRecord { val, span } => match val.collect() {
|
||||
Value::LazyRecord { val, .. } => match val.collect() {
|
||||
Ok(value) => collect_input(value),
|
||||
Err(_) => (
|
||||
vec![String::from("")],
|
||||
vec![vec![Value::LazyRecord { val, span }]],
|
||||
vec![vec![Value::lazy_record(val, span)]],
|
||||
),
|
||||
},
|
||||
Value::Nothing { .. } => (vec![], vec![]),
|
||||
|
@ -734,10 +734,7 @@ fn build_table_as_list(v: &RecordView) -> Value {
|
||||
})
|
||||
.collect();
|
||||
|
||||
Value::List {
|
||||
vals,
|
||||
span: NuSpan::unknown(),
|
||||
}
|
||||
Value::list(vals, NuSpan::unknown())
|
||||
}
|
||||
|
||||
fn build_table_as_record(v: &RecordView) -> Value {
|
||||
|
Reference in New Issue
Block a user