mirror of
https://github.com/nushell/nushell.git
synced 2024-11-28 03:13:44 +01:00
1c49ca503a
# Description This PR renames the conversion functions on `Value` to be more consistent. It follows the Rust [API guidelines](https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv) for ad-hoc conversions. The conversion functions on `Value` now come in a few forms: - `coerce_{type}` takes a `&Value` and attempts to convert the value to `type` (e.g., `i64` are converted to `f64`). This is the old behavior of some of the `as_{type}` functions -- these functions have simply been renamed to better reflect what they do. - The new `as_{type}` functions take a `&Value` and returns an `Ok` result only if the value is of `type` (no conversion is attempted). The returned value will be borrowed if `type` is non-`Copy`, otherwise an owned value is returned. - `into_{type}` exists for non-`Copy` types, but otherwise does not attempt conversion just like `as_type`. It takes an owned `Value` and always returns an owned result. - `coerce_into_{type}` has the same relationship with `coerce_{type}` as `into_{type}` does with `as_{type}`. - `to_{kind}_string`: conversion to different string formats (debug, abbreviated, etc.). Only two of the old string conversion functions were removed, the rest have been renamed only. - `to_{type}`: other conversion functions. Currently, only `to_path` exists. (And `to_string` through `Display`.) This table summaries the above: | Form | Cost | Input Ownership | Output Ownership | Converts `Value` case/`type` | | ---------------------------- | ----- | --------------- | ---------------- | -------- | | `as_{type}` | Cheap | Borrowed | Borrowed/Owned | No | | `into_{type}` | Cheap | Owned | Owned | No | | `coerce_{type}` | Cheap | Borrowed | Borrowed/Owned | Yes | | `coerce_into_{type}` | Cheap | Owned | Owned | Yes | | `to_{kind}_string` | Expensive | Borrowed | Owned | Yes | | `to_{type}` | Expensive | Borrowed | Owned | Yes | # User-Facing Changes Breaking API change for `Value` in `nu-protocol` which is exposed as part of the plugin API.
49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
use nu_protocol::{Config, Span, Value};
|
|
use rstest::rstest;
|
|
|
|
#[test]
|
|
fn test_comparison_nothing() {
|
|
let values = vec![
|
|
Value::test_int(1),
|
|
Value::test_string("string"),
|
|
Value::test_float(1.0),
|
|
];
|
|
|
|
let nothing = Value::nothing(Span::test_data());
|
|
|
|
for value in values {
|
|
assert!(matches!(
|
|
value.eq(Span::test_data(), ¬hing, Span::test_data()),
|
|
Ok(Value::Bool { val: false, .. })
|
|
));
|
|
|
|
assert!(matches!(
|
|
value.ne(Span::test_data(), ¬hing, Span::test_data()),
|
|
Ok(Value::Bool { val: true, .. })
|
|
));
|
|
|
|
assert!(matches!(
|
|
nothing.eq(Span::test_data(), &value, Span::test_data()),
|
|
Ok(Value::Bool { val: false, .. })
|
|
));
|
|
|
|
assert!(matches!(
|
|
nothing.ne(Span::test_data(), &value, Span::test_data()),
|
|
Ok(Value::Bool { val: true, .. })
|
|
));
|
|
}
|
|
}
|
|
|
|
#[rstest]
|
|
#[case(365 * 24 * 3600 * 1_000_000_000, "52wk 1day")]
|
|
#[case( (((((((7 + 2) * 24 + 3) * 60 + 4) * 60) + 5) * 1000 + 6) * 1000 + 7) * 1000 + 8,
|
|
"1wk 2day 3hr 4min 5sec 6ms 7µs 8ns")]
|
|
fn test_duration_to_string(#[case] in_ns: i64, #[case] expected: &str) {
|
|
let dur = Value::test_duration(in_ns);
|
|
assert_eq!(
|
|
expected,
|
|
dur.to_expanded_string("", &Config::default()),
|
|
"expected != observed"
|
|
);
|
|
}
|