Skip to main content

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