mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +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:
@ -106,11 +106,7 @@ fn run(call: &Call, input: PipelineData) -> Result<PipelineData, ShellError> {
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Value::String {
|
||||
val: description,
|
||||
span: head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::string(description, head).into_pipeline_data())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -100,10 +100,7 @@ impl Command for Do {
|
||||
param
|
||||
.var_id
|
||||
.expect("Internal error: rest positional parameter lacks var_id"),
|
||||
Value::List {
|
||||
vals: rest_items,
|
||||
span,
|
||||
},
|
||||
Value::list(rest_items, span),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -61,10 +61,10 @@ little reason to use this over just writing the values as-is."#
|
||||
Example {
|
||||
description: "Put a list of numbers in the pipeline. This is the same as [1 2 3].",
|
||||
example: "echo 1 2 3",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::list(
|
||||
vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
|
||||
Span::test_data(),
|
||||
)),
|
||||
},
|
||||
Example {
|
||||
description:
|
||||
|
@ -65,16 +65,16 @@ impl Command for ErrorMake {
|
||||
Example {
|
||||
description: "Create a simple custom error",
|
||||
example: r#"error make {msg: "my custom error message"}"#,
|
||||
result: Some(Value::Error {
|
||||
error: Box::new(ShellError::GenericError(
|
||||
result: Some(Value::error(
|
||||
ShellError::GenericError(
|
||||
"my custom error message".to_string(),
|
||||
"".to_string(),
|
||||
None,
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
span: Span::unknown(),
|
||||
}),
|
||||
),
|
||||
Span::unknown(),
|
||||
)),
|
||||
},
|
||||
Example {
|
||||
description: "Create a more complex custom error",
|
||||
@ -86,16 +86,16 @@ impl Command for ErrorMake {
|
||||
end: 456 # not mandatory unless $.label.start is set
|
||||
}
|
||||
}"#,
|
||||
result: Some(Value::Error {
|
||||
error: Box::new(ShellError::GenericError(
|
||||
result: Some(Value::error(
|
||||
ShellError::GenericError(
|
||||
"my custom error message".to_string(),
|
||||
"my custom label text".to_string(),
|
||||
Some(Span::new(123, 456)),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
span: Span::unknown(),
|
||||
}),
|
||||
),
|
||||
Span::unknown(),
|
||||
)),
|
||||
},
|
||||
Example {
|
||||
description:
|
||||
@ -118,7 +118,8 @@ impl Command for ErrorMake {
|
||||
}
|
||||
|
||||
fn make_error(value: &Value, throw_span: Option<Span>) -> Option<ShellError> {
|
||||
if let Value::Record { span, .. } = &value {
|
||||
let span = value.span();
|
||||
if let Value::Record { .. } = &value {
|
||||
let msg = value.get_data_by_key("msg");
|
||||
let label = value.get_data_by_key("label");
|
||||
|
||||
@ -203,7 +204,7 @@ fn make_error(value: &Value, throw_span: Option<Span>) -> Option<ShellError> {
|
||||
(None, _) => Some(ShellError::GenericError(
|
||||
"Unable to parse error format.".into(),
|
||||
"missing required member `$.msg`".into(),
|
||||
Some(*span),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
|
@ -39,16 +39,16 @@ impl Command for ExportCommand {
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
Ok(Value::String {
|
||||
val: get_full_help(
|
||||
Ok(Value::string(
|
||||
get_full_help(
|
||||
&ExportCommand.signature(),
|
||||
&ExportCommand.examples(),
|
||||
engine_state,
|
||||
stack,
|
||||
self.is_parser_keyword(),
|
||||
),
|
||||
span: call.head,
|
||||
}
|
||||
call.head,
|
||||
)
|
||||
.into_pipeline_data())
|
||||
}
|
||||
|
||||
|
@ -65,8 +65,8 @@ impl Command for LazyMake {
|
||||
.get_flag(engine_state, stack, "get-value")?
|
||||
.expect("required flag");
|
||||
|
||||
Ok(Value::LazyRecord {
|
||||
val: Box::new(NuLazyRecord {
|
||||
Ok(Value::lazy_record(
|
||||
Box::new(NuLazyRecord {
|
||||
engine_state: engine_state.clone(),
|
||||
stack: Arc::new(Mutex::new(stack.clone())),
|
||||
columns,
|
||||
@ -74,7 +74,7 @@ impl Command for LazyMake {
|
||||
span,
|
||||
}),
|
||||
span,
|
||||
}
|
||||
)
|
||||
.into_pipeline_data())
|
||||
}
|
||||
|
||||
@ -118,10 +118,7 @@ impl<'a> LazyRecord<'a> for NuLazyRecord {
|
||||
fn get_column_value(&self, column: &str) -> Result<Value, ShellError> {
|
||||
let block = self.engine_state.get_block(self.get_value.block_id);
|
||||
let mut stack = self.stack.lock().expect("lock must not be poisoned");
|
||||
let column_value = Value::String {
|
||||
val: column.into(),
|
||||
span: self.span,
|
||||
};
|
||||
let column_value = Value::string(column, self.span);
|
||||
|
||||
if let Some(var) = block.signature.get_positional(0) {
|
||||
if let Some(var_id) = &var.var_id {
|
||||
@ -141,7 +138,7 @@ impl<'a> LazyRecord<'a> for NuLazyRecord {
|
||||
pipeline_result.map(|data| match data {
|
||||
PipelineData::Value(value, ..) => value,
|
||||
// TODO: Proper error handling.
|
||||
_ => Value::Nothing { span: self.span },
|
||||
_ => Value::nothing(self.span),
|
||||
})
|
||||
}
|
||||
|
||||
@ -150,9 +147,6 @@ impl<'a> LazyRecord<'a> for NuLazyRecord {
|
||||
}
|
||||
|
||||
fn clone_value(&self, span: Span) -> Value {
|
||||
Value::LazyRecord {
|
||||
val: Box::new((*self).clone()),
|
||||
span,
|
||||
}
|
||||
Value::lazy_record(Box::new((*self).clone()), span)
|
||||
}
|
||||
}
|
||||
|
@ -41,16 +41,16 @@ impl Command for Overlay {
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
Ok(Value::String {
|
||||
val: get_full_help(
|
||||
Ok(Value::string(
|
||||
get_full_help(
|
||||
&Overlay.signature(),
|
||||
&[],
|
||||
engine_state,
|
||||
stack,
|
||||
self.is_parser_keyword(),
|
||||
),
|
||||
span: call.head,
|
||||
}
|
||||
call.head,
|
||||
)
|
||||
.into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
@ -39,11 +39,7 @@ impl Command for OverlayList {
|
||||
.map(|s| Value::string(s, call.head))
|
||||
.collect();
|
||||
|
||||
Ok(Value::List {
|
||||
vals: active_overlays_engine,
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::list(active_overlays_engine, call.head).into_pipeline_data())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -35,16 +35,16 @@ impl Command for Scope {
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
Ok(Value::String {
|
||||
val: get_full_help(
|
||||
Ok(Value::string(
|
||||
get_full_help(
|
||||
&Scope.signature(),
|
||||
&[],
|
||||
engine_state,
|
||||
stack,
|
||||
self.is_parser_keyword(),
|
||||
),
|
||||
span: call.head,
|
||||
}
|
||||
call.head,
|
||||
)
|
||||
.into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user