mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:06:03 +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:
@ -6,7 +6,6 @@ use nu_protocol::{
|
||||
PipelineIterator, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use std::iter::FromIterator;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct UpdateCells;
|
||||
@ -114,14 +113,12 @@ impl Command for UpdateCells {
|
||||
// the columns to update
|
||||
let columns: Option<Value> = call.get_flag(&engine_state, &mut stack, "columns")?;
|
||||
let columns: Option<HashSet<String>> = match columns {
|
||||
Some(val) => {
|
||||
let cols = val
|
||||
.as_list()?
|
||||
.iter()
|
||||
.map(|val| val.as_string())
|
||||
.collect::<Result<Vec<String>, ShellError>>()?;
|
||||
Some(HashSet::from_iter(cols))
|
||||
}
|
||||
Some(val) => Some(
|
||||
val.into_list()?
|
||||
.into_iter()
|
||||
.map(Value::coerce_into_string)
|
||||
.collect::<Result<HashSet<String>, ShellError>>()?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
|
||||
|
@ -432,7 +432,7 @@ fn html_value(value: Value, config: &Config) -> String {
|
||||
output_string.push_str("</pre>");
|
||||
}
|
||||
other => output_string.push_str(
|
||||
&v_htmlescape::escape(&other.into_abbreviated_string(config))
|
||||
&v_htmlescape::escape(&other.to_abbreviated_string(config))
|
||||
.to_string()
|
||||
.replace('\n', "<br>"),
|
||||
),
|
||||
|
@ -104,7 +104,7 @@ impl Command for SubCommand {
|
||||
fn value_to_color(v: Option<Value>) -> Result<Option<Rgb>, ShellError> {
|
||||
let s = match v {
|
||||
None => return Ok(None),
|
||||
Some(x) => x.as_string()?,
|
||||
Some(x) => x.coerce_into_string()?,
|
||||
};
|
||||
Ok(Some(Rgb::from_hex_string(s)))
|
||||
}
|
||||
|
@ -54,8 +54,8 @@ impl Command for FormatPattern {
|
||||
match specified_pattern {
|
||||
Err(e) => Err(e),
|
||||
Ok(pattern) => {
|
||||
let string_pattern = pattern.as_string()?;
|
||||
let string_span = pattern.span();
|
||||
let string_pattern = pattern.coerce_into_string()?;
|
||||
// the string span is start as `"`, we don't need the character
|
||||
// to generate proper span for sub expression.
|
||||
let ops = extract_formatting_operations(
|
||||
@ -287,7 +287,7 @@ fn format_record(
|
||||
.collect();
|
||||
match data_as_value.clone().follow_cell_path(&path_members, false) {
|
||||
Ok(value_at_column) => {
|
||||
output.push_str(value_at_column.into_string(", ", config).as_str())
|
||||
output.push_str(value_at_column.to_expanded_string(", ", config).as_str())
|
||||
}
|
||||
Err(se) => return Err(se),
|
||||
}
|
||||
@ -298,7 +298,7 @@ fn format_record(
|
||||
None => {
|
||||
let parsed_result = eval_expression(engine_state, stack, &exp);
|
||||
if let Ok(val) = parsed_result {
|
||||
output.push_str(&val.into_abbreviated_string(config))
|
||||
output.push_str(&val.to_abbreviated_string(config))
|
||||
}
|
||||
}
|
||||
Some(err) => {
|
||||
|
Reference in New Issue
Block a user