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