mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 02:57:53 +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:
@ -738,7 +738,7 @@ fn heavy_lifting(code: Value, escape: bool, osc: bool, call: &Call) -> Result<St
|
||||
});
|
||||
}
|
||||
let code_string = if param_is_string {
|
||||
code.as_string().expect("error getting code as string")
|
||||
code.coerce_string().expect("error getting code as string")
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
@ -798,9 +798,10 @@ fn heavy_lifting(code: Value, escape: bool, osc: bool, call: &Call) -> Result<St
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let span = code.span();
|
||||
// This is a record that should look like
|
||||
// { fg: "#ff0000" bg: "#00ff00" attr: bli }
|
||||
let record = code.as_record()?;
|
||||
let record = code.into_record()?;
|
||||
// create a NuStyle to parse the information into
|
||||
let mut nu_style = nu_color_config::NuStyle {
|
||||
fg: None,
|
||||
@ -810,13 +811,13 @@ fn heavy_lifting(code: Value, escape: bool, osc: bool, call: &Call) -> Result<St
|
||||
// Iterate and populate NuStyle with real values
|
||||
for (k, v) in record {
|
||||
match k.as_str() {
|
||||
"fg" => nu_style.fg = Some(v.as_string()?),
|
||||
"bg" => nu_style.bg = Some(v.as_string()?),
|
||||
"attr" => nu_style.attr = Some(v.as_string()?),
|
||||
"fg" => nu_style.fg = Some(v.coerce_into_string()?),
|
||||
"bg" => nu_style.bg = Some(v.coerce_into_string()?),
|
||||
"attr" => nu_style.attr = Some(v.coerce_into_string()?),
|
||||
_ => {
|
||||
return Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: format!("unknown ANSI format key: expected one of ['fg', 'bg', 'attr'], found '{k}'"),
|
||||
span: code.span(),
|
||||
span,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ fn action(input: &Value, args: &Arguments, _span: Span) -> Value {
|
||||
other => {
|
||||
// Fake stripping ansi for other types and just show the abbreviated string
|
||||
// instead of showing an error message
|
||||
Value::string(other.into_abbreviated_string(&args.config), span)
|
||||
Value::string(other.to_abbreviated_string(&args.config), span)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -95,9 +95,9 @@ impl Command for InputList {
|
||||
let display_value = if let Some(ref cellpath) = display_path {
|
||||
val.clone()
|
||||
.follow_cell_path(&cellpath.members, false)?
|
||||
.into_string(", ", engine_state.get_config())
|
||||
.to_expanded_string(", ", engine_state.get_config())
|
||||
} else {
|
||||
val.into_string(", ", engine_state.get_config())
|
||||
val.to_expanded_string(", ", engine_state.get_config())
|
||||
};
|
||||
Ok(Options {
|
||||
name: display_value,
|
||||
|
Reference in New Issue
Block a user