forked from extern/nushell
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:
@@ -65,18 +65,40 @@ impl HashableValue {
|
||||
///
|
||||
/// If the given value is not hashable(mainly because of it is structured data), an error will returned.
|
||||
pub fn from_value(value: Value, span: Span) -> Result<Self, ShellError> {
|
||||
let val_span = value.span();
|
||||
match value {
|
||||
Value::Bool { val, span } => Ok(HashableValue::Bool { val, span }),
|
||||
Value::Int { val, span } => Ok(HashableValue::Int { val, span }),
|
||||
Value::Filesize { val, span } => Ok(HashableValue::Filesize { val, span }),
|
||||
Value::Duration { val, span } => Ok(HashableValue::Duration { val, span }),
|
||||
Value::Date { val, span } => Ok(HashableValue::Date { val, span }),
|
||||
Value::Float { val, span } => Ok(HashableValue::Float {
|
||||
val: val.to_ne_bytes(),
|
||||
span,
|
||||
Value::Bool { val, .. } => Ok(HashableValue::Bool {
|
||||
val,
|
||||
span: val_span,
|
||||
}),
|
||||
Value::Int { val, .. } => Ok(HashableValue::Int {
|
||||
val,
|
||||
span: val_span,
|
||||
}),
|
||||
Value::Filesize { val, .. } => Ok(HashableValue::Filesize {
|
||||
val,
|
||||
span: val_span,
|
||||
}),
|
||||
Value::Duration { val, .. } => Ok(HashableValue::Duration {
|
||||
val,
|
||||
span: val_span,
|
||||
}),
|
||||
Value::Date { val, .. } => Ok(HashableValue::Date {
|
||||
val,
|
||||
span: val_span,
|
||||
}),
|
||||
Value::Float { val, .. } => Ok(HashableValue::Float {
|
||||
val: val.to_ne_bytes(),
|
||||
span: val_span,
|
||||
}),
|
||||
Value::String { val, .. } => Ok(HashableValue::String {
|
||||
val,
|
||||
span: val_span,
|
||||
}),
|
||||
Value::Binary { val, .. } => Ok(HashableValue::Binary {
|
||||
val,
|
||||
span: val_span,
|
||||
}),
|
||||
Value::String { val, span } => Ok(HashableValue::String { val, span }),
|
||||
Value::Binary { val, span } => Ok(HashableValue::Binary { val, span }),
|
||||
|
||||
// Explicitly propagate errors instead of dropping them.
|
||||
Value::Error { error, .. } => Err(*error),
|
||||
@@ -92,17 +114,14 @@ impl HashableValue {
|
||||
/// Convert from self to nu's core data type `Value`.
|
||||
pub fn into_value(self) -> Value {
|
||||
match self {
|
||||
HashableValue::Bool { val, span } => Value::Bool { val, span },
|
||||
HashableValue::Int { val, span } => Value::Int { val, span },
|
||||
HashableValue::Filesize { val, span } => Value::Filesize { val, span },
|
||||
HashableValue::Duration { val, span } => Value::Duration { val, span },
|
||||
HashableValue::Date { val, span } => Value::Date { val, span },
|
||||
HashableValue::Float { val, span } => Value::Float {
|
||||
val: f64::from_ne_bytes(val),
|
||||
span,
|
||||
},
|
||||
HashableValue::String { val, span } => Value::String { val, span },
|
||||
HashableValue::Binary { val, span } => Value::Binary { val, span },
|
||||
HashableValue::Bool { val, span } => Value::bool(val, span),
|
||||
HashableValue::Int { val, span } => Value::int(val, span),
|
||||
HashableValue::Filesize { val, span } => Value::filesize(val, span),
|
||||
HashableValue::Duration { val, span } => Value::duration(val, span),
|
||||
HashableValue::Date { val, span } => Value::date(val, span),
|
||||
HashableValue::Float { val, span } => Value::float(f64::from_ne_bytes(val), span),
|
||||
HashableValue::String { val, span } => Value::string(val, span),
|
||||
HashableValue::Binary { val, span } => Value::binary(val, span),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,29 +186,24 @@ mod test {
|
||||
let span = Span::test_data();
|
||||
let values = vec![
|
||||
(
|
||||
Value::Bool { val: true, span },
|
||||
Value::bool(true, span),
|
||||
HashableValue::Bool { val: true, span },
|
||||
),
|
||||
(Value::int(1, span), HashableValue::Int { val: 1, span }),
|
||||
(
|
||||
Value::Int { val: 1, span },
|
||||
HashableValue::Int { val: 1, span },
|
||||
),
|
||||
(
|
||||
Value::Filesize { val: 1, span },
|
||||
Value::filesize(1, span),
|
||||
HashableValue::Filesize { val: 1, span },
|
||||
),
|
||||
(
|
||||
Value::Duration { val: 1, span },
|
||||
Value::duration(1, span),
|
||||
HashableValue::Duration { val: 1, span },
|
||||
),
|
||||
(
|
||||
Value::Date {
|
||||
val: DateTime::<FixedOffset>::parse_from_rfc2822(
|
||||
"Wed, 18 Feb 2015 23:16:09 GMT",
|
||||
)
|
||||
.unwrap(),
|
||||
Value::date(
|
||||
DateTime::<FixedOffset>::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 GMT")
|
||||
.unwrap(),
|
||||
span,
|
||||
},
|
||||
),
|
||||
HashableValue::Date {
|
||||
val: DateTime::<FixedOffset>::parse_from_rfc2822(
|
||||
"Wed, 18 Feb 2015 23:16:09 GMT",
|
||||
@@ -199,17 +213,14 @@ mod test {
|
||||
},
|
||||
),
|
||||
(
|
||||
Value::String {
|
||||
val: "1".to_string(),
|
||||
span,
|
||||
},
|
||||
Value::string("1".to_string(), span),
|
||||
HashableValue::String {
|
||||
val: "1".to_string(),
|
||||
span,
|
||||
},
|
||||
),
|
||||
(
|
||||
Value::Binary { val: vec![1], span },
|
||||
Value::binary(vec![1], span),
|
||||
HashableValue::Binary { val: vec![1], span },
|
||||
),
|
||||
];
|
||||
@@ -225,22 +236,12 @@ mod test {
|
||||
fn from_unhashable_value() {
|
||||
let span = Span::test_data();
|
||||
let values = [
|
||||
Value::List {
|
||||
vals: vec![Value::Bool { val: true, span }],
|
||||
span,
|
||||
},
|
||||
Value::Closure {
|
||||
val: 0,
|
||||
captures: HashMap::new(),
|
||||
span,
|
||||
},
|
||||
Value::Nothing { span },
|
||||
Value::Error {
|
||||
error: Box::new(ShellError::DidYouMean("what?".to_string(), span)),
|
||||
span,
|
||||
},
|
||||
Value::CellPath {
|
||||
val: CellPath {
|
||||
Value::list(vec![Value::bool(true, span)], span),
|
||||
Value::closure(0, HashMap::new(), span),
|
||||
Value::nothing(span),
|
||||
Value::error(ShellError::DidYouMean("what?".to_string(), span), span),
|
||||
Value::cell_path(
|
||||
CellPath {
|
||||
members: vec![PathMember::Int {
|
||||
val: 0,
|
||||
span,
|
||||
@@ -248,7 +249,7 @@ mod test {
|
||||
}],
|
||||
},
|
||||
span,
|
||||
},
|
||||
),
|
||||
];
|
||||
for v in values {
|
||||
assert!(HashableValue::from_value(v, Span::unknown()).is_err())
|
||||
@@ -259,15 +260,12 @@ mod test {
|
||||
fn from_to_tobe_same() {
|
||||
let span = Span::test_data();
|
||||
let values = vec![
|
||||
Value::Bool { val: true, span },
|
||||
Value::Int { val: 1, span },
|
||||
Value::Filesize { val: 1, span },
|
||||
Value::Duration { val: 1, span },
|
||||
Value::String {
|
||||
val: "1".to_string(),
|
||||
span,
|
||||
},
|
||||
Value::Binary { val: vec![1], span },
|
||||
Value::bool(true, span),
|
||||
Value::int(1, span),
|
||||
Value::filesize(1, span),
|
||||
Value::duration(1, span),
|
||||
Value::string("1".to_string(), span),
|
||||
Value::binary(vec![1], span),
|
||||
];
|
||||
for val in values.into_iter() {
|
||||
let expected_val = val.clone();
|
||||
|
@@ -51,8 +51,8 @@ impl Command for Histogram {
|
||||
Example {
|
||||
description: "Compute a histogram for a list of numbers",
|
||||
example: "[1 2 1] | histogram",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::test_record(Record {
|
||||
result: Some(Value::list (
|
||||
vec![Value::test_record(Record {
|
||||
cols: vec!["value".to_string(), "count".to_string(), "quantile".to_string(), "percentage".to_string(), "frequency".to_string()],
|
||||
vals: vec![
|
||||
Value::test_int(1),
|
||||
@@ -72,8 +72,8 @@ impl Command for Histogram {
|
||||
Value::test_string("*********************************"),
|
||||
],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
}
|
||||
Span::test_data(),
|
||||
)
|
||||
),
|
||||
},
|
||||
Example {
|
||||
@@ -274,16 +274,10 @@ fn histogram_impl(
|
||||
cols: result_cols.clone(),
|
||||
vals: vec![
|
||||
val.into_value(),
|
||||
Value::Int { val: count, span },
|
||||
Value::Float {
|
||||
val: quantile,
|
||||
span,
|
||||
},
|
||||
Value::String {
|
||||
val: percentage,
|
||||
span,
|
||||
},
|
||||
Value::String { val: freq, span },
|
||||
Value::int(count, span),
|
||||
Value::float(quantile, span),
|
||||
Value::string(percentage, span),
|
||||
Value::string(freq, span),
|
||||
],
|
||||
},
|
||||
span,
|
||||
@@ -291,11 +285,7 @@ fn histogram_impl(
|
||||
));
|
||||
}
|
||||
result.sort_by(|a, b| b.0.cmp(&a.0));
|
||||
Value::List {
|
||||
vals: result.into_iter().map(|x| x.1).collect(),
|
||||
span,
|
||||
}
|
||||
.into_pipeline_data()
|
||||
Value::list(result.into_iter().map(|x| x.1).collect(), span).into_pipeline_data()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
Reference in New Issue
Block a user