1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::borrow::Cow;

use bexpand::Expression;
use nu_plugin::{EvaluatedCall, LabeledError, Plugin};
use nu_protocol::{Category, PluginExample, PluginSignature, Span, Type, Value};

pub struct Bexpand;

fn bexpand(call: &EvaluatedCall, input: &Value) -> Result<Value, LabeledError> {
    let span = input.span();
    let output: Result<Vec<Cow<str>>, _> = match input {
        Value::String { val, .. } => {
            let val = val.as_str();
            let expression: Expression = match val.try_into() {
                Ok(e) => e,
                Err(s) => {
                    return Err(LabeledError {
                        label: "Brace expression failed to parse".into(),
                        msg: s,
                        span: Some(span),
                    })
                }
            };

            expression.into_iter().collect()
        }
        Value::List { vals, .. } => {
            let exprs: Result<Vec<_>, _> = vals
                .into_iter()
                .map(|val| match val {
                    Value::String { val, .. } => {
                        let val = val.as_str();
                        let expression: Expression = match val.try_into() {
                            Ok(e) => e,
                            Err(s) => {
                                return Err(LabeledError {
                                    label: "Brace expression failed to parse".into(),
                                    msg: s,
                                    span: Some(span),
                                })
                            }
                        };

                        Ok(expression.into_iter())
                    }
                    v => {
                        return Err(LabeledError {
                            label: "Input must be string".into(),
                            msg: format!("Input type was {}", input.get_type()),
                            span: Some(v.span()),
                        })
                    }
                })
                .collect();
            exprs?.into_iter().flatten().collect()
        }

        v => {
            return Err(LabeledError {
                label: "Input must be string".into(),
                msg: format!("Input type was {}", input.get_type()),
                span: Some(v.span()),
            })
        }
    };

    let output = match output {
        Ok(o) => o
            .into_iter()
            .map(|s| Value::string(s.into_owned(), call.head))
            .collect(),
        Err(e) => {
            return Err(LabeledError {
                label: "Expression failed to generate".into(),
                msg: e.to_string(),
                span: Some(input.span()),
            })
        }
    };

    Ok(Value::list(output, call.head))
}

impl Plugin for Bexpand {
    fn signature(&self) -> Vec<PluginSignature> {
        vec![PluginSignature::build("str bexpand")
            .input_output_types(vec![
                (Type::String, Type::List(Box::new(Type::String))),
                (
                    Type::List(Box::new(Type::String)),
                    Type::List(Box::new(Type::String)),
                ),
            ])
            .usage("Bash-style brace expansion")
            .plugin_examples(vec![PluginExample {
                example: "'~/.config/nushell/{env,config,plugin}.nu' | str bexpand".into(),
                description: "Get a list of standard nushell config items".into(),
                result: Some(Value::List {
                    vals: vec![
                        Value::String {
                            val: "~/.config/nushell/env.nu".into(),
                            internal_span: Span::new(0, 0),
                        },
                        Value::String {
                            val: "~/.config/nushell/config.nu".into(),
                            internal_span: Span::new(0, 0),
                        },
                        Value::String {
                            val: "~/.config/nushell/plugin.nu".into(),
                            internal_span: Span::new(0, 0),
                        },
                    ],
                    internal_span: Span::new(0, 0),
                }),
            }])
            .category(Category::Strings)]
    }

    fn run(
        &mut self,
        name: &str,
        _: &Option<Value>,
        call: &EvaluatedCall,
        input: &Value,
    ) -> Result<Value, LabeledError> {
        // You can use the name to identify what plugin signature was called
        match name {
            "str bexpand" => bexpand(call, input),
            _ => Err(LabeledError {
                label: "Plugin call with wrong name signature".into(),
                msg: "the signature used to call the plugin does not match any name in the plugin signature vector".into(),
                span: Some(call.head),
            }),
        }
    }
}