Spanned Value step 1: span all value cases (#10042)

# Description

This doesn't really do much that the user could see, but it helps get us
ready to do the steps of the refactor to split the span off of Value, so
that values can be spanless. This allows us to have top-level values
that can hold both a Value and a Span, without requiring that all values
have them.

We expect to see significant memory reduction by removing so many
unnecessary spans from values. For example, a table of 100,000 rows and
5 columns would have a savings of ~8megs in just spans that are almost
always duplicated.

# User-Facing Changes

Nothing yet

# 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 -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `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.
-->
This commit is contained in:
JT
2023-08-25 08:48:05 +12:00
committed by GitHub
parent 8da27a1a09
commit 1e3e034021
234 changed files with 1153 additions and 868 deletions

View File

@ -77,6 +77,7 @@ impl Command for FromJson {
Ok(v) => Some(v),
Err(error) => Some(Value::Error {
error: Box::new(error),
span,
}),
}
}
@ -120,6 +121,7 @@ fn convert_nujson_to_value(value: &nu_json::Value, span: Span) -> Value {
span,
help: None,
}),
span,
}
} else {
Value::Int {

View File

@ -45,7 +45,7 @@ impl Command for FromOds {
let sel_sheets = if let Some(Value::List { vals: columns, .. }) =
call.get_flag(engine_state, stack, "sheets")?
{
convert_columns(columns.as_slice(), call.head)?
convert_columns(columns.as_slice())?
} else {
vec![]
};
@ -69,14 +69,14 @@ impl Command for FromOds {
}
}
fn convert_columns(columns: &[Value], span: Span) -> Result<Vec<String>, ShellError> {
fn convert_columns(columns: &[Value]) -> Result<Vec<String>, ShellError> {
let res = columns
.iter()
.map(|value| match &value {
Value::String { val: s, .. } => Ok(s.clone()),
_ => Err(ShellError::IncompatibleParametersSingle {
msg: "Incorrect column format, Only string as column name".to_string(),
span: value.span().unwrap_or(span),
span: value.span(),
}),
})
.collect::<Result<Vec<String>, _>>()?;
@ -93,13 +93,13 @@ fn collect_binary(input: PipelineData, span: Span) -> Result<Vec<u8>, ShellError
Some(Value::Binary { val: b, .. }) => {
bytes.extend_from_slice(&b);
}
Some(Value::Error { error }) => return Err(*error),
Some(Value::Error { error, .. }) => return Err(*error),
Some(x) => {
return Err(ShellError::UnsupportedInput(
"Expected binary from pipeline".to_string(),
"value originates from here".into(),
span,
x.expect_span(),
x.span(),
))
}
None => break,

View File

@ -45,7 +45,7 @@ impl Command for FromXlsx {
let sel_sheets = if let Some(Value::List { vals: columns, .. }) =
call.get_flag(engine_state, stack, "sheets")?
{
convert_columns(columns.as_slice(), call.head)?
convert_columns(columns.as_slice())?
} else {
vec![]
};
@ -69,14 +69,14 @@ impl Command for FromXlsx {
}
}
fn convert_columns(columns: &[Value], span: Span) -> Result<Vec<String>, ShellError> {
fn convert_columns(columns: &[Value]) -> Result<Vec<String>, ShellError> {
let res = columns
.iter()
.map(|value| match &value {
Value::String { val: s, .. } => Ok(s.clone()),
_ => Err(ShellError::IncompatibleParametersSingle {
msg: "Incorrect column format, Only string as column name".to_string(),
span: value.span().unwrap_or(span),
span: value.span(),
}),
})
.collect::<Result<Vec<String>, _>>()?;
@ -98,7 +98,7 @@ fn collect_binary(input: PipelineData, span: Span) -> Result<Vec<u8>, ShellError
"Expected binary from pipeline".to_string(),
"value originates from here".into(),
span,
x.expect_span(),
x.span(),
))
}
None => break,

View File

@ -14,8 +14,8 @@ fn from_value_to_delimited_string(
Value::Record { val, span } => record_to_delimited(val, *span, separator, config, head),
Value::List { vals, span } => table_to_delimited(vals, *span, separator, config, head),
// Propagate errors by explicitly matching them before the final case.
Value::Error { error } => Err(*error.clone()),
v => Err(make_unsupported_input_error(v, head, v.expect_span())),
Value::Error { error, .. } => Err(*error.clone()),
v => Err(make_unsupported_input_error(v, head, v.span())),
}
}
@ -119,7 +119,7 @@ fn to_string_tagged_value(
Value::Date { val, .. } => Ok(val.to_string()),
Value::Nothing { .. } => Ok(String::new()),
// Propagate existing errors
Value::Error { error } => Err(*error.clone()),
Value::Error { error, .. } => Err(*error.clone()),
_ => Err(make_unsupported_input_error(v, head, span)),
}
}
@ -161,7 +161,7 @@ pub fn to_delimited_data(
Err(_) => Err(ShellError::CantConvert {
to_type: format_name.into(),
from_type: value.get_type().to_string(),
span: value.span().unwrap_or(span),
span: value.span(),
help: None,
}),
}?;

View File

@ -76,6 +76,7 @@ impl Command for ToJson {
span,
help: None,
}),
span,
}
.into_pipeline_data()),
}
@ -126,7 +127,7 @@ pub fn value_to_json_value(v: &Value) -> Result<nu_json::Value, ShellError> {
),
Value::List { vals, .. } => nu_json::Value::Array(json_list(vals)?),
Value::Error { error } => return Err(*error.clone()),
Value::Error { error, .. } => return Err(*error.clone()),
Value::Closure { .. }
| Value::Block { .. }
| Value::Range { .. }

View File

@ -85,6 +85,7 @@ impl Command for ToNuon {
span,
help: None,
}),
span,
}
.into_pipeline_data()),
}
@ -136,7 +137,7 @@ pub fn value_to_string(
"could not convert binary to string".into(),
"value originates from here".into(),
span,
v.expect_span(),
v.span(),
));
}
}
@ -146,13 +147,13 @@ pub fn value_to_string(
"blocks are currently not nuon-compatible".into(),
"value originates from here".into(),
span,
v.expect_span(),
v.span(),
)),
Value::Closure { .. } => Err(ShellError::UnsupportedInput(
"closures are currently not nuon-compatible".into(),
"value originates from here".into(),
span,
v.expect_span(),
v.span(),
)),
Value::Bool { val, .. } => {
if *val {
@ -165,19 +166,19 @@ pub fn value_to_string(
"cellpaths are currently not nuon-compatible".to_string(),
"value originates from here".into(),
span,
v.expect_span(),
v.span(),
)),
Value::CustomValue { .. } => Err(ShellError::UnsupportedInput(
"custom values are currently not nuon-compatible".to_string(),
"value originates from here".into(),
span,
v.expect_span(),
v.span(),
)),
Value::Date { val, .. } => Ok(val.to_rfc3339()),
// FIXME: make durations use the shortest lossless representation.
Value::Duration { val, .. } => Ok(format!("{}ns", *val)),
// Propagate existing errors
Value::Error { error } => Err(*error.clone()),
Value::Error { error, .. } => Err(*error.clone()),
// FIXME: make filesizes use the shortest lossless representation.
Value::Filesize { val, .. } => Ok(format!("{}b", *val)),
Value::Float { val, .. } => {
@ -246,7 +247,7 @@ pub fn value_to_string(
"match patterns are currently not nuon-compatible".to_string(),
"value originates from here".into(),
span,
v.expect_span(),
v.span(),
)),
Value::Nothing { .. } => Ok("null".to_string()),
Value::Range { val, .. } => Ok(format!(

View File

@ -148,7 +148,7 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
Value::Block { val, .. } => format!("<Block {val}>"),
Value::Closure { val, .. } => format!("<Closure {val}>"),
Value::Nothing { .. } => String::new(),
Value::Error { error } => format!("{error:?}"),
Value::Error { error, .. } => format!("{error:?}"),
Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.into_string(),
Value::CustomValue { val, .. } => val.value_string(),

View File

@ -77,7 +77,7 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
toml::Value::String(code)
}
Value::Nothing { .. } => toml::Value::String("<Nothing>".to_string()),
Value::Error { error } => return Err(*error.clone()),
Value::Error { error, .. } => return Err(*error.clone()),
Value::Binary { val, .. } => toml::Value::Array(
val.iter()
.map(|x| toml::Value::Integer(*x as i64))
@ -125,6 +125,7 @@ fn toml_into_pipeline_data(
span,
help: None,
}),
span,
}
.into_pipeline_data()),
}
@ -138,12 +139,12 @@ fn value_to_toml_value(
match v {
Value::Record { .. } => helper(engine_state, v),
// Propagate existing errors
Value::Error { error } => Err(*error.clone()),
Value::Error { error, .. } => Err(*error.clone()),
_ => Err(ShellError::UnsupportedInput(
format!("{:?} is not valid top-level TOML", v.get_type()),
"value originates from here".into(),
head,
v.expect_span(),
v.span(),
)),
}
}

View File

@ -97,7 +97,7 @@ fn to_xml_entry<W: Write>(
top_level: bool,
writer: &mut quick_xml::Writer<W>,
) -> Result<(), ShellError> {
let entry_span = entry.span()?;
let entry_span = entry.span();
// Allow using strings directly as content.
// So user can write
@ -197,7 +197,7 @@ fn to_tag_like<W: Write>(
return Err(ShellError::CantConvert {
to_type: "XML".into(),
from_type: Type::Record(vec![]).to_string(),
span: content.span()?,
span: content.span(),
help: Some("PI content expected to be a string".into()),
});
}
@ -215,7 +215,7 @@ fn to_tag_like<W: Write>(
return Err(ShellError::CantConvert {
to_type: "XML".into(),
from_type: attrs.get_type().to_string(),
span: attrs.span()?,
span: attrs.span(),
help: Some("Tag attributes expected to be a record".into()),
});
}
@ -228,7 +228,7 @@ fn to_tag_like<W: Write>(
return Err(ShellError::CantConvert {
to_type: "XML".into(),
from_type: content.get_type().to_string(),
span: content.span()?,
span: content.span(),
help: Some("Tag content expected to be a list".into()),
});
}
@ -350,7 +350,7 @@ fn parse_attributes(attrs: Record) -> Result<IndexMap<String, String>, ShellErro
return Err(ShellError::CantConvert {
to_type: "XML".to_string(),
from_type: v.get_type().to_string(),
span: v.span()?,
span: v.span(),
help: Some("Attribute value expected to be a string".into()),
});
}

View File

@ -79,7 +79,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
Value::Block { .. } => serde_yaml::Value::Null,
Value::Closure { .. } => serde_yaml::Value::Null,
Value::Nothing { .. } => serde_yaml::Value::Null,
Value::Error { error } => return Err(*error.clone()),
Value::Error { error, .. } => return Err(*error.clone()),
Value::Binary { val, .. } => serde_yaml::Value::Sequence(
val.iter()
.map(|x| serde_yaml::Value::Number(serde_yaml::Number::from(*x)))
@ -118,6 +118,7 @@ fn to_yaml(input: PipelineData, head: Span) -> Result<PipelineData, ShellError>
span: head,
help: None,
}),
span: head,
}
.into_pipeline_data()),
}