nu_command/strings/str_/case/
mod.rs

1mod capitalize;
2mod downcase;
3mod str_;
4mod upcase;
5
6pub use capitalize::StrCapitalize;
7pub use downcase::StrDowncase;
8pub use str_::Str;
9pub use upcase::StrUpcase;
10
11use nu_cmd_base::input_handler::{CmdArgument, operate as general_operate};
12use nu_engine::command_prelude::*;
13
14struct Arguments<F: Fn(&str) -> String + Send + Sync + 'static> {
15    case_operation: &'static F,
16    cell_paths: Option<Vec<CellPath>>,
17}
18
19impl<F: Fn(&str) -> String + Send + Sync + 'static> CmdArgument for Arguments<F> {
20    fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
21        self.cell_paths.take()
22    }
23}
24
25pub fn operate<F>(
26    engine_state: &EngineState,
27    stack: &mut Stack,
28    call: &Call,
29    input: PipelineData,
30    case_operation: &'static F,
31) -> Result<PipelineData, ShellError>
32where
33    F: Fn(&str) -> String + Send + Sync + 'static,
34{
35    let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
36    let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
37    let args = Arguments {
38        case_operation,
39        cell_paths,
40    };
41    general_operate(action, args, input, call.head, engine_state.signals())
42}
43
44fn action<F>(input: &Value, args: &Arguments<F>, head: Span) -> Value
45where
46    F: Fn(&str) -> String + Send + Sync + 'static,
47{
48    let case_operation = args.case_operation;
49    match input {
50        Value::String { val, .. } => Value::string(case_operation(val), head),
51        Value::Error { .. } => input.clone(),
52        _ => Value::error(
53            ShellError::OnlySupportsThisInputType {
54                exp_input_type: "string".into(),
55                wrong_type: input.get_type().to_string(),
56                dst_span: head,
57                src_span: input.span(),
58            },
59            head,
60        ),
61    }
62}