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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::borrow::Cow;

use bexpand::Expression;
use nu_plugin::{EngineInterface, EvaluatedCall, Plugin, PluginCommand, SimplePluginCommand};
use nu_protocol::{Category, ErrorLabel, Example, LabeledError, Signature, Span, Type, Value};

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 {
                        labels: vec![ErrorLabel {
                            text: "Brace expression failed to parse".into(),
                            span: span,
                        }],
                        msg: s,
                        code: None,
                        url: None,
                        help: None,
                        inner: vec![],
                    });
                }
            };

            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 {
                                    labels: vec![ErrorLabel {
                                        text: "Brace expression failed to parse".into(),
                                        span: span,
                                    }],
                                    msg: s,
                                    code: None,
                                    url: None,
                                    help: None,
                                    inner: vec![],
                                });
                            }
                        };

                        Ok(expression.into_iter())
                    }
                    v => {
                        return Err(LabeledError {
                            labels: vec![ErrorLabel {
                                text: "Input must be string".into(),
                                span: v.span(),
                            }],
                            msg: format!("Input type was {}", input.get_type()),
                            code: None,
                            url: None,
                            help: None,
                            inner: vec![],
                        });
                    }
                })
                .collect();
            exprs?.into_iter().flatten().collect()
        }

        v => {
            return Err(LabeledError {
                labels: vec![ErrorLabel {
                    text: "Input must be string".into(),
                    span: v.span(),
                }],
                msg: format!("Input type was {}", input.get_type()),
                code: None,
                url: None,
                help: None,
                inner: vec![],
            });
        }
    };

    let output = match output {
        Ok(o) => o
            .into_iter()
            .map(|s| Value::string(s.into_owned(), call.head))
            .collect(),
        Err(e) => {
            return Err(LabeledError {
                labels: vec![ErrorLabel {
                    text: "Expression failed to generate".into(),
                    span: input.span(),
                }],
                msg: e.to_string(),
                code: None,
                url: None,
                help: None,
                inner: vec![],
            });
        }
    };

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

pub struct BexpandPlugin;

impl Plugin for BexpandPlugin {
    fn commands(&self) -> Vec<Box<dyn PluginCommand<Plugin = Self>>> {
        vec![Box::new(Bexpand)]
    }
}

pub struct Bexpand;

impl SimplePluginCommand for Bexpand {
    type Plugin = BexpandPlugin;

    fn signature(&self) -> Signature {
        Signature::new("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")
            .category(Category::Strings)
    }

    fn run(
        &self,
        _plugin: &Self::Plugin,
        _engine: &EngineInterface,
        call: &EvaluatedCall,
        input: &Value,
    ) -> Result<Value, LabeledError> {
        // You can use the name to identify what plugin signature was called
        bexpand(call, input)
    }

    fn name(&self) -> &str {
        "str bexpand"
    }

    fn usage(&self) -> &str {
        "Does bash-style brace expansion"
    }
    fn examples(&self) -> Vec<Example> {
        vec![Example {
            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),
            }),
        }]
    }
}