dfir_lang/graph/ops/
for_each.rs

1use quote::quote_spanned;
2
3use super::{
4    OperatorCategory, OperatorConstraints, OperatorWriteOutput, WriteContextArgs,
5    RANGE_0, RANGE_1,
6};
7
8/// > 1 input stream, 0 output streams
9///
10/// > Arguments: a Rust closure
11///
12/// Iterates through a stream passing each element to the closure in the
13/// argument.
14///
15/// > Note: The closure has access to the [`context` object](surface_flows.mdx#the-context-object).
16///
17/// ```dfir
18///     source_iter(vec!["Hello", "World"])
19///         -> for_each(|x| println!("{}", x));
20/// ```
21pub const FOR_EACH: OperatorConstraints = OperatorConstraints {
22    name: "for_each",
23    categories: &[OperatorCategory::Sink],
24    hard_range_inn: RANGE_1,
25    soft_range_inn: RANGE_1,
26    hard_range_out: RANGE_0,
27    soft_range_out: RANGE_0,
28    num_args: 1,
29    persistence_args: RANGE_0,
30    type_args: RANGE_0,
31    is_external_input: false,
32    has_singleton_output: false,
33    flo_type: None,
34    ports_inn: None,
35    ports_out: None,
36    input_delaytype_fn: |_| None,
37    write_fn: |&WriteContextArgs {
38                   root,
39                   op_span,
40                   ident,
41                   arguments,
42                   ..
43               },
44               _| {
45        let func = &arguments[0];
46        let write_iterator = quote_spanned! {op_span=>
47            let #ident = #root::pusherator::for_each::ForEach::new(#func);
48        };
49        Ok(OperatorWriteOutput {
50            write_iterator,
51            ..Default::default()
52        })
53    },
54};