nu_command/generators/
generate.rs

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure;

#[derive(Clone)]
pub struct Generate;

impl Command for Generate {
    fn name(&self) -> &str {
        "generate"
    }

    fn signature(&self) -> Signature {
        Signature::build("generate")
            .input_output_types(vec![
                (Type::Nothing, Type::list(Type::Any)),
                (Type::list(Type::Any), Type::list(Type::Any)),
                (Type::table(), Type::list(Type::Any)),
                (Type::Range, Type::list(Type::Any)),
            ])
            .required(
                "closure",
                SyntaxShape::Closure(Some(vec![SyntaxShape::Any, SyntaxShape::Any])),
                "Generator function.",
            )
            .optional("initial", SyntaxShape::Any, "Initial value.")
            .allow_variants_without_examples(true)
            .category(Category::Generators)
    }

    fn description(&self) -> &str {
        "Generate a list of values by successively invoking a closure."
    }

    fn extra_description(&self) -> &str {
        r#"The generator closure accepts a single argument and returns a record
containing two optional keys: 'out' and 'next'. Each invocation, the 'out'
value, if present, is added to the stream. If a 'next' key is present, it is
used as the next argument to the closure, otherwise generation stops.

Additionally, if an input stream is provided, the generator closure accepts two
arguments. On each invocation an element of the input stream is provided as the
first argument. The second argument is the `next` value from the last invocation.
In this case, generation also stops when the input stream stops."#
    }

    fn search_terms(&self) -> Vec<&str> {
        vec!["unfold", "stream", "yield", "expand", "state", "scan"]
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                example: "generate {|i| if $i <= 10 { {out: $i, next: ($i + 2)} }} 0",
                description: "Generate a sequence of numbers",
                result: Some(Value::list(
                    vec![
                        Value::test_int(0),
                        Value::test_int(2),
                        Value::test_int(4),
                        Value::test_int(6),
                        Value::test_int(8),
                        Value::test_int(10),
                    ],
                    Span::test_data(),
                )),
            },
            Example {
                example:
                    "generate {|fib| {out: $fib.0, next: [$fib.1, ($fib.0 + $fib.1)]} } [0, 1]",
                description: "Generate a continuous stream of Fibonacci numbers",
                result: None,
            },
            Example {
                example:
                    "generate {|fib=[0, 1]| {out: $fib.0, next: [$fib.1, ($fib.0 + $fib.1)]} }",
                description:
                    "Generate a continuous stream of Fibonacci numbers, using default parameters",
                result: None,
            },
            Example {
                example:
                    "1..5 | generate {|e, sum=0| let sum = $e + $sum; {out: $sum, next: $sum} }",
                description: "Generate a running sum of the inputs",
                result: Some(Value::test_list(vec![
                    Value::test_int(1),
                    Value::test_int(3),
                    Value::test_int(6),
                    Value::test_int(10),
                    Value::test_int(15),
                ])),
            },
        ]
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let head = call.head;
        let closure: Closure = call.req(engine_state, stack, 0)?;
        let initial: Option<Value> = call.opt(engine_state, stack, 1)?;
        let block = engine_state.get_block(closure.block_id);
        let mut closure = ClosureEval::new(engine_state, stack, closure);

        match input {
            PipelineData::Empty => {
                // A type of Option<S> is used to represent state. Invocation
                // will stop on None. Using Option<S> allows functions to output
                // one final value before stopping.
                let mut state = Some(get_initial_state(initial, &block.signature, call.head)?);
                let iter = std::iter::from_fn(move || {
                    let state_arg = state.take()?;

                    let closure_result = closure
                        .add_arg(state_arg)
                        .run_with_input(PipelineData::Empty);
                    let (output, next_input) = parse_closure_result(closure_result, head);

                    // We use `state` to control when to stop, not `output`. By wrapping
                    // it in a `Some`, we allow the generator to output `None` as a valid output
                    // value.
                    state = next_input;
                    Some(output)
                });

                Ok(iter
                    .flatten()
                    .into_pipeline_data(call.head, engine_state.signals().clone()))
            }
            input @ (PipelineData::Value(Value::Range { .. }, ..)
            | PipelineData::Value(Value::List { .. }, ..)
            | PipelineData::ListStream(..)) => {
                let mut state = Some(get_initial_state(initial, &block.signature, call.head)?);
                let iter = input.into_iter().map_while(move |item| {
                    let state_arg = state.take()?;
                    let closure_result = closure
                        .add_arg(item)
                        .add_arg(state_arg)
                        .run_with_input(PipelineData::Empty);
                    let (output, next_input) = parse_closure_result(closure_result, head);
                    state = next_input;
                    Some(output)
                });
                Ok(iter
                    .flatten()
                    .into_pipeline_data(call.head, engine_state.signals().clone()))
            }
            _ => Err(ShellError::PipelineMismatch {
                exp_input_type: "nothing".to_string(),
                dst_span: head,
                src_span: input.span().unwrap_or(head),
            }),
        }
    }
}

fn get_initial_state(
    initial: Option<Value>,
    signature: &Signature,
    span: Span,
) -> Result<Value, ShellError> {
    match initial {
        Some(v) => Ok(v),
        None => {
            // the initial state should be referred from signature
            if !signature.optional_positional.is_empty()
                && signature.optional_positional[0].default_value.is_some()
            {
                Ok(signature.optional_positional[0]
                    .default_value
                    .clone()
                    .expect("Already checked default value"))
            } else {
                Err(ShellError::GenericError {
                    error: "The initial value is missing".to_string(),
                    msg: "Missing initial value".to_string(),
                    span: Some(span),
                    help: Some(
                        "Provide an <initial> value as an argument to generate, or assign a default value to the closure parameter"
                            .to_string(),
                    ),
                    inner: vec![],
                })
            }
        }
    }
}

fn parse_closure_result(
    closure_result: Result<PipelineData, ShellError>,
    head: Span,
) -> (Option<Value>, Option<Value>) {
    match closure_result {
        // no data -> output nothing and stop.
        Ok(PipelineData::Empty) => (None, None),

        Ok(PipelineData::Value(value, ..)) => {
            let span = value.span();
            match value {
                // {out: ..., next: ...} -> output and continue
                Value::Record { val, .. } => {
                    let iter = val.into_owned().into_iter();
                    let mut out = None;
                    let mut next = None;
                    let mut err = None;

                    for (k, v) in iter {
                        if k.eq_ignore_ascii_case("out") {
                            out = Some(v);
                        } else if k.eq_ignore_ascii_case("next") {
                            next = Some(v);
                        } else {
                            let error = ShellError::GenericError {
                                error: "Invalid block return".into(),
                                msg: format!("Unexpected record key '{}'", k),
                                span: Some(span),
                                help: None,
                                inner: vec![],
                            };
                            err = Some(Value::error(error, head));
                            break;
                        }
                    }

                    if err.is_some() {
                        (err, None)
                    } else {
                        (out, next)
                    }
                }

                // some other value -> error and stop
                _ => {
                    let error = ShellError::GenericError {
                        error: "Invalid block return".into(),
                        msg: format!("Expected record, found {}", value.get_type()),
                        span: Some(span),
                        help: None,
                        inner: vec![],
                    };

                    (Some(Value::error(error, head)), None)
                }
            }
        }

        Ok(other) => {
            let error = other
                .into_value(head)
                .map(|val| ShellError::GenericError {
                    error: "Invalid block return".into(),
                    msg: format!("Expected record, found {}", val.get_type()),
                    span: Some(val.span()),
                    help: None,
                    inner: vec![],
                })
                .unwrap_or_else(|err| err);

            (Some(Value::error(error, head)), None)
        }

        // error -> error and stop
        Err(error) => (Some(Value::error(error, head)), None),
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_examples() {
        use crate::test_examples;

        test_examples(Generate {})
    }
}