mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 08:31:29 +02:00
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:
@@ -204,8 +204,9 @@ pub fn action(input: &Value, _args: &Arguments, span: Span) -> Value {
|
||||
.into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: span,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span,
|
||||
},
|
||||
};
|
||||
|
||||
|
@@ -161,6 +161,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
Ok(val) => Value::Bool { val, span },
|
||||
Err(error) => Value::Error {
|
||||
error: Box::new(error),
|
||||
span,
|
||||
},
|
||||
},
|
||||
// Propagate errors by explicitly matching them before the final case.
|
||||
@@ -170,8 +171,9 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
exp_input_type: "bool, integer, float or string".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: span,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@@ -264,8 +264,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
exp_input_type: "string and integer".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -308,6 +309,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
input.debug_value(),
|
||||
*span,
|
||||
)),
|
||||
span: *span,
|
||||
},
|
||||
},
|
||||
Zone::West(i) => match FixedOffset::west_opt((*i as i32) * HOUR) {
|
||||
@@ -323,6 +325,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
input.debug_value(),
|
||||
*span,
|
||||
)),
|
||||
span: *span,
|
||||
},
|
||||
},
|
||||
Zone::Error => Value::Error {
|
||||
@@ -331,6 +334,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
err_message: "Invalid timezone or offset".to_string(),
|
||||
span: *span,
|
||||
}),
|
||||
span: *span,
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -346,6 +350,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
Err(reason) => {
|
||||
Value::Error {
|
||||
error: Box::new(ShellError::CantConvert { to_type: format!("could not parse as datetime using format '{}'", dt.0), from_type: reason.to_string(), span: head, help: Some("you can use `into datetime` without a format string to enable flexible parsing".to_string()) }),
|
||||
span: head,
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -369,8 +374,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
exp_input_type: "string".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@@ -107,6 +107,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value {
|
||||
span: *span,
|
||||
help: None,
|
||||
}),
|
||||
span: *span,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -125,8 +126,9 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value {
|
||||
exp_input_type: "string, integer or bool".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@@ -156,6 +156,7 @@ fn into_duration(
|
||||
if let Err(error) = r {
|
||||
return Value::Error {
|
||||
error: Box::new(error),
|
||||
span,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -235,6 +236,7 @@ fn action(input: &Value, span: Span) -> Value {
|
||||
Ok(val) => Value::Duration { val, span },
|
||||
Err(error) => Value::Error {
|
||||
error: Box::new(error),
|
||||
span,
|
||||
},
|
||||
},
|
||||
// Propagate errors by explicitly matching them before the final case.
|
||||
@@ -244,8 +246,9 @@ fn action(input: &Value, span: Span) -> Value {
|
||||
exp_input_type: "string or duration".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: span,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -270,7 +273,7 @@ mod test {
|
||||
#[case("4\u{00B5}s", 4*1000)] // micro sign
|
||||
#[case("4\u{03BC}s", 4*1000)] // mu symbol
|
||||
#[case("5ms", 5 * 1000 * 1000)]
|
||||
#[case("1sec", 1 * NS_PER_SEC)]
|
||||
#[case("1sec", NS_PER_SEC)]
|
||||
#[case("7min", 7 * 60 * NS_PER_SEC)]
|
||||
#[case("42hr", 42 * 60 * 60 * NS_PER_SEC)]
|
||||
#[case("123day", 123 * 24 * 60 * 60 * NS_PER_SEC)]
|
||||
|
@@ -148,42 +148,40 @@ impl Command for SubCommand {
|
||||
}
|
||||
|
||||
pub fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
if let Ok(value_span) = input.span() {
|
||||
match input {
|
||||
Value::Filesize { .. } => input.clone(),
|
||||
Value::Int { val, .. } => Value::Filesize {
|
||||
val: *val,
|
||||
let value_span = input.span();
|
||||
match input {
|
||||
Value::Filesize { .. } => input.clone(),
|
||||
Value::Int { val, .. } => Value::Filesize {
|
||||
val: *val,
|
||||
span: value_span,
|
||||
},
|
||||
Value::Float { val, .. } => Value::Filesize {
|
||||
val: *val as i64,
|
||||
span: value_span,
|
||||
},
|
||||
Value::String { val, .. } => match int_from_string(val, value_span) {
|
||||
Ok(val) => Value::Filesize {
|
||||
val,
|
||||
span: value_span,
|
||||
},
|
||||
Value::Float { val, .. } => Value::Filesize {
|
||||
val: *val as i64,
|
||||
Err(error) => Value::Error {
|
||||
error: Box::new(error),
|
||||
span: value_span,
|
||||
},
|
||||
Value::String { val, .. } => match int_from_string(val, value_span) {
|
||||
Ok(val) => Value::Filesize {
|
||||
val,
|
||||
span: value_span,
|
||||
},
|
||||
Err(error) => Value::Error {
|
||||
error: Box::new(error),
|
||||
},
|
||||
},
|
||||
Value::Nothing { .. } => Value::Filesize {
|
||||
val: 0,
|
||||
span: value_span,
|
||||
},
|
||||
other => Value::Error {
|
||||
error: Box::new(ShellError::OnlySupportsThisInputType {
|
||||
exp_input_type: "string and integer".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: span,
|
||||
src_span: value_span,
|
||||
}),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
// Propagate existing errors
|
||||
input.clone()
|
||||
},
|
||||
Value::Nothing { .. } => Value::Filesize {
|
||||
val: 0,
|
||||
span: value_span,
|
||||
},
|
||||
other => Value::Error {
|
||||
error: Box::new(ShellError::OnlySupportsThisInputType {
|
||||
exp_input_type: "string and integer".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: span,
|
||||
src_span: value_span,
|
||||
}),
|
||||
span,
|
||||
},
|
||||
}
|
||||
}
|
||||
fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
||||
|
@@ -250,6 +250,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
span,
|
||||
help: None,
|
||||
}),
|
||||
span,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -263,6 +264,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
Ok(val) => Value::Int { val, span },
|
||||
Err(error) => Value::Error {
|
||||
error: Box::new(error),
|
||||
span,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
@@ -297,6 +299,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
val_span: *val_span,
|
||||
call_span: span,
|
||||
}),
|
||||
span,
|
||||
}
|
||||
} else {
|
||||
Value::Int {
|
||||
@@ -335,8 +338,9 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
.into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: span,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -353,7 +357,12 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
|
||||
{
|
||||
match int_from_string(val, head) {
|
||||
Ok(x) => return Value::int(x, head),
|
||||
Err(e) => return Value::Error { error: Box::new(e) },
|
||||
Err(e) => {
|
||||
return Value::Error {
|
||||
error: Box::new(e),
|
||||
span: head,
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if val.starts_with("00") {
|
||||
// It's a padded string
|
||||
@@ -367,6 +376,7 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
|
||||
span: head,
|
||||
help: Some(e.to_string()),
|
||||
}),
|
||||
span: head,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -381,8 +391,9 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
|
||||
exp_input_type: "string and integer".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -395,6 +406,7 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
|
||||
span: head,
|
||||
help: None,
|
||||
}),
|
||||
span: head,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -583,7 +595,7 @@ mod test {
|
||||
},
|
||||
Span::test_data(),
|
||||
);
|
||||
if let Value::Error { error } = actual {
|
||||
if let Value::Error { error, .. } = actual {
|
||||
if let ShellError::IncorrectValue { msg: e, .. } = *error {
|
||||
assert!(
|
||||
e.contains(err_expected),
|
||||
|
@@ -176,8 +176,9 @@ fn into_record(
|
||||
exp_input_type: "string".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: call.head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: call.head,
|
||||
},
|
||||
};
|
||||
Ok(res.into_pipeline_data())
|
||||
|
@@ -249,7 +249,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
span,
|
||||
},
|
||||
|
||||
Value::Error { error } => Value::String {
|
||||
Value::Error { error, .. } => Value::String {
|
||||
val: into_code(error).unwrap_or_default(),
|
||||
span,
|
||||
},
|
||||
@@ -265,6 +265,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
span,
|
||||
help: Some("try using the `to nuon` command".into()),
|
||||
}),
|
||||
span,
|
||||
},
|
||||
Value::Binary { .. } => Value::Error {
|
||||
error: Box::new(ShellError::CantConvert {
|
||||
@@ -273,6 +274,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
span,
|
||||
help: Some("try using the `decode` command".into()),
|
||||
}),
|
||||
span,
|
||||
},
|
||||
x => Value::Error {
|
||||
error: Box::new(ShellError::CantConvert {
|
||||
@@ -281,6 +283,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
span,
|
||||
help: None,
|
||||
}),
|
||||
span,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user