mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
Name the Value
conversion functions more clearly (#11851)
# 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.
This commit is contained in:
@ -47,7 +47,7 @@ pub fn nu_value_to_string_colored(val: &Value, cfg: &Config, style: &StyleComput
|
||||
|
||||
pub fn nu_value_to_string(val: &Value, cfg: &Config, style: &StyleComputer) -> NuText {
|
||||
let float_precision = cfg.float_precision as usize;
|
||||
let text = val.into_abbreviated_string(cfg);
|
||||
let text = val.to_abbreviated_string(cfg);
|
||||
make_styled_string(style, text, Some(val), float_precision)
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ pub fn get_value_style(value: &Value, config: &Config, style_computer: &StyleCom
|
||||
style_computer.style_primitive(value),
|
||||
),
|
||||
_ => (
|
||||
value.into_abbreviated_string(config),
|
||||
value.to_abbreviated_string(config),
|
||||
style_computer.style_primitive(value),
|
||||
),
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ fn expanded_table_list(input: &[Value], cfg: Cfg<'_>) -> TableResult {
|
||||
.as_record()
|
||||
.ok()
|
||||
.and_then(|val| val.get(INDEX_COLUMN_NAME))
|
||||
.map(|value| value.into_string("", cfg.opts.config))
|
||||
.map(|value| value.to_expanded_string("", cfg.opts.config))
|
||||
.unwrap_or_else(|| index.to_string());
|
||||
|
||||
let row = row + with_header as usize;
|
||||
|
@ -207,7 +207,7 @@ fn get_table_row_index(item: &Value, config: &Config, row: usize, offset: usize)
|
||||
match item {
|
||||
Value::Record { val, .. } => val
|
||||
.get(INDEX_COLUMN_NAME)
|
||||
.map(|value| value.into_string("", config))
|
||||
.map(|value| value.to_expanded_string("", config))
|
||||
.unwrap_or_else(|| (row + offset).to_string()),
|
||||
_ => (row + offset).to_string(),
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ fn convert_nu_value_to_table_value(value: Value, config: &Config) -> TableValue
|
||||
}
|
||||
}
|
||||
value => {
|
||||
let mut text = value.into_abbreviated_string(config);
|
||||
let mut text = value.to_abbreviated_string(config);
|
||||
if string_width(&text) > 50 {
|
||||
text = string_wrap(&text, 30, false);
|
||||
}
|
||||
|
Reference in New Issue
Block a user