nushell/crates/nu-command/src/strings/str_/length.rs
Artemiy 1867bb1a88
Fix incorrect handling of boolean flags for builtin commands (#11492)
# Description
Possible fix of #11456
This PR fixes a bug where builtin commands did not respect the logic of
dynamically passed boolean flags. The reason is
[has_flag](6f59abaf43/crates/nu-protocol/src/ast/call.rs (L204C5-L212C6))
method did not evaluate and take into consideration expression used with
flag.

To address this issue a solution is proposed:
1. `has_flag` method is moved to `CallExt` and new logic to evaluate
expression and check if it is a boolean value is added
2. `has_flag_const` method is added to `CallExt` which is a constant
version of `has_flag`
3. `has_named` method is added to `Call` which is basically the old
logic of `has_flag`
4. All usages of `has_flag` in code are updated, mostly to pass
`engine_state` and `stack` to new `has_flag`. In `run_const` commands it
is replaced with `has_flag_const`. And in a few select places: parser,
`to nuon` and `into string` old logic via `has_named` is used.

# User-Facing Changes
Explicit values of boolean flags are now respected in builtin commands.
Before:

![image](https://github.com/nushell/nushell/assets/17511668/f9fbabb2-3cfd-43f9-ba9e-ece76d80043c)
After:

![image](https://github.com/nushell/nushell/assets/17511668/21867596-2075-437f-9c85-45563ac70083)

Another example:
Before:

![image](https://github.com/nushell/nushell/assets/17511668/efdbc5ca-5227-45a4-ac5b-532cdc2bbf5f)
After:

![image](https://github.com/nushell/nushell/assets/17511668/2907d5c5-aa93-404d-af1c-21cdc3d44646)


# Tests + Formatting
Added test reproducing some variants of original issue.
2024-01-11 17:19:48 +02:00

187 lines
5.2 KiB
Rust

use crate::grapheme_flags;
use crate::grapheme_flags_const;
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::Category;
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value};
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()
}
}
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str length"
}
fn signature(&self) -> Signature {
Signature::build("str length")
.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![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.allow_variants_without_examples(true)
.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'),
)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, replace strings at the given cell paths with their length.",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Output the length of any strings in the pipeline."
}
fn search_terms(&self) -> Vec<&str> {
vec!["size", "count"]
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
run(
cell_paths,
engine_state,
call,
input,
grapheme_flags(engine_state, stack, call)?,
)
}
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)?;
run(
cell_paths,
working_set.permanent(),
call,
input,
grapheme_flags_const(working_set, call)?,
)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Return the lengths of a string",
example: "'hello' | str length",
result: Some(Value::test_int(5)),
},
Example {
description: "Count length using grapheme clusters",
example: "'🇯🇵ほげ ふが ぴよ' | str length --grapheme-clusters",
result: Some(Value::test_int(9)),
},
Example {
description: "Return the lengths of multiple strings",
example: "['hi' 'there'] | str length",
result: Some(Value::list(
vec![Value::test_int(2), Value::test_int(5)],
Span::test_data(),
)),
},
]
}
}
fn run(
cell_paths: Vec<CellPath>,
engine_state: &EngineState,
call: &Call,
input: PipelineData,
graphemes: bool,
) -> Result<PipelineData, ShellError> {
let args = Arguments {
cell_paths: (!cell_paths.is_empty()).then_some(cell_paths),
graphemes,
};
operate(action, args, input, call.head, engine_state.ctrlc.clone())
}
fn action(input: &Value, arg: &Arguments, head: Span) -> Value {
match input {
Value::String { val, .. } => Value::int(
if arg.graphemes {
val.graphemes(true).count()
} else {
val.len()
} as i64,
head,
),
Value::Error { .. } => input.clone(),
_ => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "string".into(),
wrong_type: input.get_type().to_string(),
dst_span: head,
src_span: input.span(),
},
head,
),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn use_utf8_bytes() {
let word = Value::string(String::from("🇯🇵ほげ ふが ぴよ"), Span::test_data());
let options = Arguments {
cell_paths: None,
graphemes: false,
};
let actual = action(&word, &options, Span::test_data());
assert_eq!(actual, Value::test_int(28));
}
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}