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:
JT
2023-09-04 02:27:29 +12:00
committed by GitHub
parent af79eb2943
commit 6cdfee3573
372 changed files with 5811 additions and 7448 deletions

View File

@ -84,8 +84,9 @@ impl Command for PluginDeclaration {
})?;
let input = input.into_value(call.head);
let span = input.span();
let input = match input {
Value::CustomValue { val, span } => {
Value::CustomValue { val, .. } => {
match val.as_any().downcast_ref::<PluginCustomValue>() {
Some(plugin_data) if plugin_data.filename == self.filename => {
CallInput::Data(PluginData {
@ -145,16 +146,16 @@ impl Command for PluginDeclaration {
Ok(PipelineData::Value(value.as_ref().clone(), None))
}
Ok(PluginResponse::PluginData(name, plugin_data)) => Ok(PipelineData::Value(
Value::CustomValue {
val: Box::new(PluginCustomValue {
Value::custom_value(
Box::new(PluginCustomValue {
name,
data: plugin_data.data,
filename: self.filename.clone(),
shell: self.shell.clone(),
source: engine_state.get_decl(call.decl_id).name().to_owned(),
}),
span: plugin_data.span,
},
plugin_data.span,
),
None,
)),
Ok(PluginResponse::Error(err)) => Err(err.into()),

View File

@ -214,10 +214,7 @@ pub fn get_signature(
/// call: &EvaluatedCall,
/// input: &Value,
/// ) -> Result<Value, LabeledError> {
/// Ok(Value::String {
/// val: "Hello, World!".to_owned(),
/// span: call.head,
/// })
/// Ok(Value::string("Hello, World!".to_owned(), call.head))
/// }
/// }
/// ```
@ -320,9 +317,8 @@ pub fn serve_plugin(plugin: &mut impl Plugin, encoder: impl PluginEncoder) {
CallInput::Value(value) => Ok(value),
CallInput::Data(plugin_data) => {
bincode::deserialize::<Box<dyn CustomValue>>(&plugin_data.data)
.map(|custom_value| Value::CustomValue {
val: custom_value,
span: plugin_data.span,
.map(|custom_value| {
Value::custom_value(custom_value, plugin_data.span)
})
.map_err(|err| ShellError::PluginFailedToDecode(err.to_string()))
}
@ -334,16 +330,21 @@ pub fn serve_plugin(plugin: &mut impl Plugin, encoder: impl PluginEncoder) {
};
let response = match value {
Ok(Value::CustomValue { val, span }) => match bincode::serialize(&val) {
Ok(data) => {
let name = val.value_string();
PluginResponse::PluginData(name, PluginData { data, span })
Ok(value) => {
let span = value.span();
match value {
Value::CustomValue { val, .. } => match bincode::serialize(&val) {
Ok(data) => {
let name = val.value_string();
PluginResponse::PluginData(name, PluginData { data, span })
}
Err(err) => PluginResponse::Error(
ShellError::PluginFailedToEncode(err.to_string()).into(),
),
},
value => PluginResponse::Value(Box::new(value)),
}
Err(err) => PluginResponse::Error(
ShellError::PluginFailedToEncode(err.to_string()).into(),
),
},
Ok(value) => PluginResponse::Value(Box::new(value)),
}
Err(err) => PluginResponse::Error(err),
};
encoder

View File

@ -115,7 +115,7 @@ impl EvaluatedCall {
/// # positional: Vec::new(),
/// # named: vec![(
/// # Spanned { item: "foo".to_owned(), span: null_span},
/// # Some(Value::Int { val: 123, span: null_span })
/// # Some(Value::int(123, null_span))
/// # )],
/// # };
/// let opt_foo = match call.get_flag_value("foo") {
@ -164,9 +164,9 @@ impl EvaluatedCall {
/// # let call = EvaluatedCall {
/// # head: null_span,
/// # positional: vec![
/// # Value::String { val: "a".to_owned(), span: null_span },
/// # Value::String { val: "b".to_owned(), span: null_span },
/// # Value::String { val: "c".to_owned(), span: null_span },
/// # Value::string("a".to_owned(), null_span),
/// # Value::string("b".to_owned(), null_span),
/// # Value::string("c".to_owned(), null_span),
/// # ],
/// # named: vec![],
/// # };
@ -196,7 +196,7 @@ impl EvaluatedCall {
/// # positional: Vec::new(),
/// # named: vec![(
/// # Spanned { item: "foo".to_owned(), span: null_span},
/// # Some(Value::Int { val: 123, span: null_span })
/// # Some(Value::int(123, null_span))
/// # )],
/// # };
/// let foo = call.get_flag::<i64>("foo");
@ -213,7 +213,7 @@ impl EvaluatedCall {
/// # positional: Vec::new(),
/// # named: vec![(
/// # Spanned { item: "bar".to_owned(), span: null_span},
/// # Some(Value::Int { val: 123, span: null_span })
/// # Some(Value::int(123, null_span))
/// # )],
/// # };
/// let foo = call.get_flag::<i64>("foo");
@ -230,7 +230,7 @@ impl EvaluatedCall {
/// # positional: Vec::new(),
/// # named: vec![(
/// # Spanned { item: "foo".to_owned(), span: null_span},
/// # Some(Value::String { val: "abc".to_owned(), span: null_span })
/// # Some(Value::string("abc".to_owned(), null_span))
/// # )],
/// # };
/// let foo = call.get_flag::<i64>("foo");
@ -255,10 +255,10 @@ impl EvaluatedCall {
/// # let call = EvaluatedCall {
/// # head: null_span,
/// # positional: vec![
/// # Value::String { val: "zero".to_owned(), span: null_span },
/// # Value::String { val: "one".to_owned(), span: null_span },
/// # Value::String { val: "two".to_owned(), span: null_span },
/// # Value::String { val: "three".to_owned(), span: null_span },
/// # Value::string("zero".to_owned(), null_span),
/// # Value::string("one".to_owned(), null_span),
/// # Value::string("two".to_owned(), null_span),
/// # Value::string("three".to_owned(), null_span),
/// # ],
/// # named: Vec::new(),
/// # };
@ -318,14 +318,8 @@ mod test {
let call = EvaluatedCall {
head: Span::new(0, 10),
positional: vec![
Value::Float {
val: 1.0,
span: Span::new(0, 10),
},
Value::String {
val: "something".into(),
span: Span::new(0, 10),
},
Value::float(1.0, Span::new(0, 10)),
Value::string("something", Span::new(0, 10)),
],
named: vec![
(
@ -333,10 +327,7 @@ mod test {
item: "name".to_string(),
span: Span::new(0, 10),
},
Some(Value::Float {
val: 1.0,
span: Span::new(0, 10),
}),
Some(Value::float(1.0, Span::new(0, 10))),
),
(
Spanned {

View File

@ -34,10 +34,7 @@ pub struct PluginCustomValue {
impl CustomValue for PluginCustomValue {
fn clone_value(&self, span: nu_protocol::Span) -> nu_protocol::Value {
Value::CustomValue {
val: Box::new(self.clone()),
span,
}
Value::custom_value(Box::new(self.clone()), span)
}
fn value_string(&self) -> String {

View File

@ -79,32 +79,20 @@ mod tests {
fn callinfo_round_trip_callinfo() {
let name = "test".to_string();
let input = Value::Bool {
val: false,
span: Span::new(1, 20),
};
let input = Value::bool(false, Span::new(1, 20));
let call = EvaluatedCall {
head: Span::new(0, 10),
positional: vec![
Value::Float {
val: 1.0,
span: Span::new(0, 10),
},
Value::String {
val: "something".into(),
span: Span::new(0, 10),
},
Value::float(1.0, Span::new(0, 10)),
Value::string("something", Span::new(0, 10)),
],
named: vec![(
Spanned {
item: "name".to_string(),
span: Span::new(0, 10),
},
Some(Value::Float {
val: 1.0,
span: Span::new(0, 10),
}),
Some(Value::float(1.0, Span::new(0, 10))),
)],
};
@ -253,10 +241,7 @@ mod tests {
#[test]
fn response_round_trip_value() {
let value = Value::Int {
val: 10,
span: Span::new(2, 30),
};
let value = Value::int(10, Span::new(2, 30));
let response = PluginResponse::Value(Box::new(value.clone()));

View File

@ -78,32 +78,20 @@ mod tests {
fn callinfo_round_trip_callinfo() {
let name = "test".to_string();
let input = Value::Bool {
val: false,
span: Span::new(1, 20),
};
let input = Value::bool(false, Span::new(1, 20));
let call = EvaluatedCall {
head: Span::new(0, 10),
positional: vec![
Value::Float {
val: 1.0,
span: Span::new(0, 10),
},
Value::String {
val: "something".into(),
span: Span::new(0, 10),
},
Value::float(1.0, Span::new(0, 10)),
Value::string("something", Span::new(0, 10)),
],
named: vec![(
Spanned {
item: "name".to_string(),
span: Span::new(0, 10),
},
Some(Value::Float {
val: 1.0,
span: Span::new(0, 10),
}),
Some(Value::float(1.0, Span::new(0, 10))),
)],
};
@ -252,10 +240,7 @@ mod tests {
#[test]
fn response_round_trip_value() {
let value = Value::Int {
val: 10,
span: Span::new(2, 30),
};
let value = Value::int(10, Span::new(2, 30));
let response = PluginResponse::Value(Box::new(value.clone()));