nushell/crates/nu-command/src/strings/split/row.rs
Ian Manske 1c49ca503a
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.
2024-02-17 18:14:16 +00:00

191 lines
5.8 KiB
Rust

use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
Value,
};
use regex::Regex;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"split row"
}
fn signature(&self) -> Signature {
Signature::build("split row")
.input_output_types(vec![
(Type::String, Type::List(Box::new(Type::String))),
(
Type::List(Box::new(Type::String)),
(Type::List(Box::new(Type::String))),
),
])
.allow_variants_without_examples(true)
.required(
"separator",
SyntaxShape::String,
"A character or regex that denotes what separates rows.",
)
.named(
"number",
SyntaxShape::Int,
"Split into maximum number of items",
Some('n'),
)
.switch("regex", "use regex syntax for separator", Some('r'))
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Split a string into multiple rows using a separator."
}
fn search_terms(&self) -> Vec<&str> {
vec!["separate", "divide", "regex"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
split_row(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Split a string into rows of char",
example: "'abc' | split row ''",
result: Some(Value::list(
vec![
Value::test_string(""),
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
Value::test_string(""),
],
Span::test_data(),
)),
},
Example {
description: "Split a string into rows by the specified separator",
example: "'a--b--c' | split row '--'",
result: Some(Value::list(
vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
Span::test_data(),
)),
},
Example {
description: "Split a string by '-'",
example: "'-a-b-c-' | split row '-'",
result: Some(Value::list(
vec![
Value::test_string(""),
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
Value::test_string(""),
],
Span::test_data(),
)),
},
Example {
description: "Split a string by regex",
example: r"'a b c' | split row -r '\s+'",
result: Some(Value::list(
vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
Span::test_data(),
)),
},
]
}
}
fn split_row(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let name_span = call.head;
let separator: Spanned<String> = call.req(engine_state, stack, 0)?;
let regex = if call.has_flag(engine_state, stack, "regex")? {
Regex::new(&separator.item)
} else {
let escaped = regex::escape(&separator.item);
Regex::new(&escaped)
}
.map_err(|e| ShellError::GenericError {
error: "Error with regular expression".into(),
msg: e.to_string(),
span: Some(separator.span),
help: None,
inner: vec![],
})?;
let max_split: Option<usize> = call.get_flag(engine_state, stack, "number")?;
input.flat_map(
move |x| split_row_helper(&x, &regex, max_split, name_span),
engine_state.ctrlc.clone(),
)
}
fn split_row_helper(v: &Value, regex: &Regex, max_split: Option<usize>, name: Span) -> Vec<Value> {
let span = v.span();
match v {
Value::Error { error, .. } => {
vec![Value::error(*error.clone(), span)]
}
v => {
let v_span = v.span();
if let Ok(s) = v.coerce_string() {
match max_split {
Some(max_split) => regex
.splitn(&s, max_split)
.map(|x: &str| Value::string(x, v_span))
.collect(),
None => regex
.split(&s)
.map(|x: &str| Value::string(x, v_span))
.collect(),
}
} else {
vec![Value::error(
ShellError::PipelineMismatch {
exp_input_type: "string".into(),
dst_span: name,
src_span: v_span,
},
name,
)]
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}