nu_command/strings/str_/
reverse.rs

1use nu_cmd_base::input_handler::{CellPathOnlyArgs, operate};
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct StrReverse;
6
7impl Command for StrReverse {
8    fn name(&self) -> &str {
9        "str reverse"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("str reverse")
14            .input_output_types(vec![
15                (Type::String, Type::String),
16                (
17                    Type::List(Box::new(Type::String)),
18                    Type::List(Box::new(Type::String)),
19                ),
20                (Type::table(), Type::table()),
21                (Type::record(), Type::record()),
22            ])
23            .allow_variants_without_examples(true)
24            .rest(
25                "rest",
26                SyntaxShape::CellPath,
27                "For a data structure input, reverse strings at the given cell paths.",
28            )
29            .category(Category::Strings)
30    }
31
32    fn description(&self) -> &str {
33        "Reverse every string in the pipeline."
34    }
35
36    fn search_terms(&self) -> Vec<&str> {
37        vec!["convert", "inverse", "flip"]
38    }
39
40    fn is_const(&self) -> bool {
41        true
42    }
43
44    fn run(
45        &self,
46        engine_state: &EngineState,
47        stack: &mut Stack,
48        call: &Call,
49        input: PipelineData,
50    ) -> Result<PipelineData, ShellError> {
51        let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
52        let args = CellPathOnlyArgs::from(cell_paths);
53        operate(action, args, input, call.head, engine_state.signals())
54    }
55
56    fn run_const(
57        &self,
58        working_set: &StateWorkingSet,
59        call: &Call,
60        input: PipelineData,
61    ) -> Result<PipelineData, ShellError> {
62        let cell_paths: Vec<CellPath> = call.rest_const(working_set, 0)?;
63        let args = CellPathOnlyArgs::from(cell_paths);
64        operate(
65            action,
66            args,
67            input,
68            call.head,
69            working_set.permanent().signals(),
70        )
71    }
72
73    fn examples(&self) -> Vec<Example> {
74        vec![
75            Example {
76                description: "Reverse a single string",
77                example: "'Nushell' | str reverse",
78                result: Some(Value::test_string("llehsuN")),
79            },
80            Example {
81                description: "Reverse multiple strings in a list",
82                example: "['Nushell' 'is' 'cool'] | str reverse",
83                result: Some(Value::list(
84                    vec![
85                        Value::test_string("llehsuN"),
86                        Value::test_string("si"),
87                        Value::test_string("looc"),
88                    ],
89                    Span::test_data(),
90                )),
91            },
92        ]
93    }
94}
95
96fn action(input: &Value, _arg: &CellPathOnlyArgs, head: Span) -> Value {
97    match input {
98        Value::String { val, .. } => Value::string(val.chars().rev().collect::<String>(), head),
99        Value::Error { .. } => input.clone(),
100        _ => Value::error(
101            ShellError::OnlySupportsThisInputType {
102                exp_input_type: "string".into(),
103                wrong_type: input.get_type().to_string(),
104                dst_span: head,
105                src_span: input.span(),
106            },
107            head,
108        ),
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn test_examples() {
118        use crate::test_examples;
119
120        test_examples(StrReverse {})
121    }
122}