2023-01-20 07:16:18 +00:00
|
|
|
use crate::grapheme_flags;
|
2024-01-11 15:19:48 +00:00
|
|
|
use crate::grapheme_flags_const;
|
2023-06-22 21:45:54 +00:00
|
|
|
use nu_cmd_base::input_handler::{operate, CmdArgument};
|
2021-11-13 21:25:55 +00:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::ast::CellPath;
|
2023-08-26 13:41:29 +00:00
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
|
2021-11-17 04:22:37 +00:00
|
|
|
use nu_protocol::Category;
|
2022-11-09 21:55:05 +00:00
|
|
|
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value};
|
2023-01-20 07:16:18 +00:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
|
|
|
|
|
|
|
struct Arguments {
|
|
|
|
cell_paths: Option<Vec<CellPath>>,
|
|
|
|
graphemes: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CmdArgument for Arguments {
|
|
|
|
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
|
|
|
|
self.cell_paths.take()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 21:25:55 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"str length"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2021-11-17 04:22:37 +00:00
|
|
|
Signature::build("str length")
|
2023-07-24 11:17:30 +00:00
|
|
|
.input_output_types(vec![
|
|
|
|
(Type::String, Type::Int),
|
|
|
|
(Type::List(Box::new(Type::String)), Type::List(Box::new(Type::Int))),
|
|
|
|
(Type::Table(vec![]), Type::Table(vec![])),
|
2023-07-26 21:13:57 +00:00
|
|
|
(Type::Record(vec![]), Type::Record(vec![])),
|
2023-07-24 11:17:30 +00:00
|
|
|
])
|
|
|
|
.allow_variants_without_examples(true)
|
2023-01-20 07:16:18 +00:00
|
|
|
.switch(
|
|
|
|
"grapheme-clusters",
|
|
|
|
"count length using grapheme clusters (all visible chars have length 1)",
|
|
|
|
Some('g'),
|
|
|
|
)
|
|
|
|
.switch(
|
|
|
|
"utf-8-bytes",
|
|
|
|
"count length using UTF-8 bytes (default; all non-ASCII chars have length 2+)",
|
|
|
|
Some('b'),
|
|
|
|
)
|
2021-11-17 04:22:37 +00:00
|
|
|
.rest(
|
|
|
|
"rest",
|
|
|
|
SyntaxShape::CellPath,
|
2023-12-15 06:32:37 +00:00
|
|
|
"For a data structure input, replace strings at the given cell paths with their length.",
|
2021-11-17 04:22:37 +00:00
|
|
|
)
|
|
|
|
.category(Category::Strings)
|
2021-11-13 21:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-03-01 05:33:02 +00:00
|
|
|
"Output the length of any strings in the pipeline."
|
2021-11-13 21:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 13:47:09 +00:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
2022-08-24 09:16:47 +00:00
|
|
|
vec!["size", "count"]
|
2022-06-06 13:47:09 +00:00
|
|
|
}
|
|
|
|
|
2023-08-26 13:41:29 +00:00
|
|
|
fn is_const(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-11-13 21:25:55 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-10-29 21:29:46 +00:00
|
|
|
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
2024-01-11 15:19:48 +00:00
|
|
|
run(
|
|
|
|
cell_paths,
|
|
|
|
engine_state,
|
|
|
|
call,
|
|
|
|
input,
|
|
|
|
grapheme_flags(engine_state, stack, call)?,
|
|
|
|
)
|
2023-08-26 13:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run_const(
|
|
|
|
&self,
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let cell_paths: Vec<CellPath> = call.rest_const(working_set, 0)?;
|
2024-01-11 15:19:48 +00:00
|
|
|
run(
|
|
|
|
cell_paths,
|
|
|
|
working_set.permanent(),
|
|
|
|
call,
|
|
|
|
input,
|
|
|
|
grapheme_flags_const(working_set, call)?,
|
|
|
|
)
|
2021-11-13 21:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
2023-01-20 07:16:18 +00:00
|
|
|
description: "Return the lengths of a string",
|
2021-11-13 21:25:55 +00:00
|
|
|
example: "'hello' | str length",
|
|
|
|
result: Some(Value::test_int(5)),
|
|
|
|
},
|
2023-01-20 07:16:18 +00:00
|
|
|
Example {
|
|
|
|
description: "Count length using grapheme clusters",
|
2023-10-19 20:08:09 +00:00
|
|
|
example: "'🇯🇵ほげ ふが ぴよ' | str length --grapheme-clusters",
|
2023-01-20 07:16:18 +00:00
|
|
|
result: Some(Value::test_int(9)),
|
|
|
|
},
|
2021-11-13 21:25:55 +00:00
|
|
|
Example {
|
|
|
|
description: "Return the lengths of multiple strings",
|
|
|
|
example: "['hi' 'there'] | str length",
|
2023-09-03 14:27:29 +00:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![Value::test_int(2), Value::test_int(5)],
|
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2021-11-13 21:25:55 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 13:41:29 +00:00
|
|
|
fn run(
|
|
|
|
cell_paths: Vec<CellPath>,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
2024-01-11 15:19:48 +00:00
|
|
|
graphemes: bool,
|
2023-08-26 13:41:29 +00:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let args = Arguments {
|
|
|
|
cell_paths: (!cell_paths.is_empty()).then_some(cell_paths),
|
2024-01-11 15:19:48 +00:00
|
|
|
graphemes,
|
2023-08-26 13:41:29 +00:00
|
|
|
};
|
|
|
|
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
|
|
|
}
|
|
|
|
|
2023-01-20 07:16:18 +00:00
|
|
|
fn action(input: &Value, arg: &Arguments, head: Span) -> Value {
|
2021-11-13 21:25:55 +00:00
|
|
|
match input {
|
2023-01-20 07:16:18 +00:00
|
|
|
Value::String { val, .. } => Value::int(
|
|
|
|
if arg.graphemes {
|
|
|
|
val.graphemes(true).count()
|
|
|
|
} else {
|
|
|
|
val.len()
|
|
|
|
} as i64,
|
|
|
|
head,
|
|
|
|
),
|
Standardise the use of ShellError::UnsupportedInput and ShellError::TypeMismatch and add spans to every instance of the former (#7217)
# Description
* I was dismayed to discover recently that UnsupportedInput and
TypeMismatch are used *extremely* inconsistently across the codebase.
UnsupportedInput is sometimes used for input type-checks (as per the
name!!), but *also* used for argument type-checks. TypeMismatch is also
used for both.
I thus devised the following standard: input type-checking *only* uses
UnsupportedInput, and argument type-checking *only* uses TypeMismatch.
Moreover, to differentiate them, UnsupportedInput now has *two* error
arrows (spans), one pointing at the command and the other at the input
origin, while TypeMismatch only has the one (because the command should
always be nearby)
* In order to apply that standard, a very large number of
UnsupportedInput uses were changed so that the input's span could be
retrieved and delivered to it.
* Additionally, I noticed many places where **errors are not propagated
correctly**: there are lots of `match` sites which take a Value::Error,
then throw it away and replace it with a new Value::Error with
less/misleading information (such as reporting the error as an
"incorrect type"). I believe that the earliest errors are the most
important, and should always be propagated where possible.
* Also, to standardise one broad subset of UnsupportedInput error
messages, who all used slightly different wordings of "expected
`<type>`, got `<type>`", I created OnlySupportsThisInputType as a
variant of it.
* Finally, a bunch of error sites that had "repeated spans" - i.e. where
an error expected two spans, but `call.head` was given for both - were
fixed to use different spans.
# Example
BEFORE
```
〉20b | str starts-with 'a'
Error: nu::shell::unsupported_input (link)
× Unsupported input
╭─[entry #31:1:1]
1 │ 20b | str starts-with 'a'
· ┬
· ╰── Input's type is filesize. This command only works with strings.
╰────
〉'a' | math cos
Error: nu::shell::unsupported_input (link)
× Unsupported input
╭─[entry #33:1:1]
1 │ 'a' | math cos
· ─┬─
· ╰── Only numerical values are supported, input type: String
╰────
〉0x[12] | encode utf8
Error: nu::shell::unsupported_input (link)
× Unsupported input
╭─[entry #38:1:1]
1 │ 0x[12] | encode utf8
· ───┬──
· ╰── non-string input
╰────
```
AFTER
```
〉20b | str starts-with 'a'
Error: nu::shell::pipeline_mismatch (link)
× Pipeline mismatch.
╭─[entry #1:1:1]
1 │ 20b | str starts-with 'a'
· ┬ ───────┬───────
· │ ╰── only string input data is supported
· ╰── input type: filesize
╰────
〉'a' | math cos
Error: nu::shell::pipeline_mismatch (link)
× Pipeline mismatch.
╭─[entry #2:1:1]
1 │ 'a' | math cos
· ─┬─ ────┬───
· │ ╰── only numeric input data is supported
· ╰── input type: string
╰────
〉0x[12] | encode utf8
Error: nu::shell::pipeline_mismatch (link)
× Pipeline mismatch.
╭─[entry #3:1:1]
1 │ 0x[12] | encode utf8
· ───┬── ───┬──
· │ ╰── only string input data is supported
· ╰── input type: binary
╰────
```
# User-Facing Changes
Various error messages suddenly make more sense (i.e. have two arrows
instead of one).
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-23 06:48:53 +00:00
|
|
|
Value::Error { .. } => input.clone(),
|
2023-09-03 14:27:29 +00:00
|
|
|
_ => Value::error(
|
|
|
|
ShellError::OnlySupportsThisInputType {
|
2023-03-01 19:34:48 +00:00
|
|
|
exp_input_type: "string".into(),
|
|
|
|
wrong_type: input.get_type().to_string(),
|
|
|
|
dst_span: head,
|
2023-08-24 20:48:05 +00:00
|
|
|
src_span: input.span(),
|
2023-09-03 14:27:29 +00:00
|
|
|
},
|
|
|
|
head,
|
|
|
|
),
|
2021-11-13 21:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
2023-01-20 07:16:18 +00:00
|
|
|
#[test]
|
|
|
|
fn use_utf8_bytes() {
|
2023-09-03 14:27:29 +00:00
|
|
|
let word = Value::string(String::from("🇯🇵ほげ ふが ぴよ"), Span::test_data());
|
2023-01-20 07:16:18 +00:00
|
|
|
|
|
|
|
let options = Arguments {
|
|
|
|
cell_paths: None,
|
|
|
|
graphemes: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let actual = action(&word, &options, Span::test_data());
|
|
|
|
assert_eq!(actual, Value::test_int(28));
|
|
|
|
}
|
|
|
|
|
2021-11-13 21:25:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
}
|