mirror of
https://github.com/nushell/nushell.git
synced 2025-08-16 14:01:01 +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:
@ -85,7 +85,7 @@ fn convert_value_to_string(value: Value, engine_state: &EngineState, stack: &mut
|
||||
let has_single_value = vals.len() == 1 && vals[0].len() == 1;
|
||||
if !has_no_head && has_single_value {
|
||||
let config = engine_state.get_config();
|
||||
vals[0][0].into_abbreviated_string(config)
|
||||
vals[0][0].to_abbreviated_string(config)
|
||||
} else {
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
let config = engine_state.get_config();
|
||||
|
@ -86,7 +86,7 @@ impl ViewCommand for NuCmd {
|
||||
let (columns, values) = collect_pipeline(pipeline);
|
||||
|
||||
if let Some(value) = has_simple_value(&values) {
|
||||
let text = value.into_abbreviated_string(&engine_state.config);
|
||||
let text = value.to_abbreviated_string(&engine_state.config);
|
||||
return Ok(NuView::Preview(Preview::new(&text)));
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ fn run_pager(
|
||||
p.show_message("For help type :help");
|
||||
|
||||
if let Some(value) = has_simple_value(&data) {
|
||||
let text = value.into_abbreviated_string(config.nu_config);
|
||||
let text = value.to_abbreviated_string(config.nu_config);
|
||||
let view = Some(Page::new(Preview::new(&text), true));
|
||||
return p.run(engine_state, stack, ctrlc, view, commands);
|
||||
}
|
||||
|
@ -1008,7 +1008,7 @@ fn cmd_input_key_event(buf: &mut CommandBuf, key: &KeyEvent) -> bool {
|
||||
}
|
||||
|
||||
fn value_as_style(style: &mut nu_ansi_term::Style, value: &Value) -> bool {
|
||||
match value.as_string() {
|
||||
match value.coerce_string() {
|
||||
Ok(s) => {
|
||||
*style = lookup_ansi_color_style(&s);
|
||||
true
|
||||
|
@ -331,7 +331,7 @@ impl View for RecordView<'_> {
|
||||
if let Some(hm) = cfg.config.get("table").and_then(create_map) {
|
||||
self.theme = theme_from_config(&hm);
|
||||
|
||||
if let Some(orientation) = hm.get("orientation").and_then(|v| v.as_string().ok()) {
|
||||
if let Some(orientation) = hm.get("orientation").and_then(|v| v.coerce_string().ok()) {
|
||||
let orientation = match orientation.as_str() {
|
||||
"left" => Some(Orientation::Left),
|
||||
"top" => Some(Orientation::Top),
|
||||
@ -648,7 +648,7 @@ fn convert_records_to_string(
|
||||
.map(|row| {
|
||||
row.iter()
|
||||
.map(|value| {
|
||||
let text = value.clone().into_abbreviated_string(cfg);
|
||||
let text = value.clone().to_abbreviated_string(cfg);
|
||||
let float_precision = cfg.float_precision as usize;
|
||||
|
||||
make_styled_string(style_computer, text, Some(value), float_precision)
|
||||
@ -857,7 +857,7 @@ fn config_get_bool(config: &ConfigMap, key: &str, default: bool) -> bool {
|
||||
fn config_get_usize(config: &ConfigMap, key: &str, default: usize) -> usize {
|
||||
config
|
||||
.get(key)
|
||||
.and_then(|v| v.as_string().ok())
|
||||
.and_then(|v| v.coerce_string().ok())
|
||||
.and_then(|s| s.parse::<usize>().ok())
|
||||
.unwrap_or(default)
|
||||
}
|
||||
|
Reference in New Issue
Block a user