mirror of
https://github.com/nushell/nushell
synced 2025-01-06 18:29:02 +00: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.
66 lines
1.8 KiB
Rust
66 lines
1.8 KiB
Rust
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Type, Value};
|
|
use reedline::Highlighter;
|
|
|
|
#[derive(Clone)]
|
|
pub struct NuHighlight;
|
|
|
|
impl Command for NuHighlight {
|
|
fn name(&self) -> &str {
|
|
"nu-highlight"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("nu-highlight")
|
|
.category(Category::Strings)
|
|
.input_output_types(vec![(Type::String, Type::String)])
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Syntax highlight the input string."
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["syntax", "color", "convert"]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let head = call.head;
|
|
|
|
let ctrlc = engine_state.ctrlc.clone();
|
|
let engine_state = std::sync::Arc::new(engine_state.clone());
|
|
let config = engine_state.get_config().clone();
|
|
|
|
let highlighter = crate::NuHighlighter {
|
|
engine_state,
|
|
stack: std::sync::Arc::new(stack.clone()),
|
|
config,
|
|
};
|
|
|
|
input.map(
|
|
move |x| match x.coerce_into_string() {
|
|
Ok(line) => {
|
|
let highlights = highlighter.highlight(&line, line.len());
|
|
Value::string(highlights.render_simple(), head)
|
|
}
|
|
Err(err) => Value::error(err, head),
|
|
},
|
|
ctrlc,
|
|
)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Describe the type of a string",
|
|
example: "'let x = 3' | nu-highlight",
|
|
result: None,
|
|
}]
|
|
}
|
|
}
|