dfir_lang/graph/ops/
map.rs

1use quote::quote_spanned;
2
3use super::{
4    OperatorCategory, OperatorConstraints, OperatorWriteOutput,
5    WriteContextArgs, RANGE_0, RANGE_1,
6};
7
8/// > 1 input stream, 1 output stream
9///
10/// > Arguments: A Rust closure
11///
12/// For each item passed in, apply the closure to generate an item to emit.
13///
14/// If you do not want to modify the item stream and instead only want to view
15/// each item use the [`inspect`](#inspect) operator instead.
16///
17/// > Note: The closure has access to the [`context` object](surface_flows.mdx#the-context-object).
18///
19/// ```dfir
20/// source_iter(vec!["hello", "world"]) -> map(|x| x.to_uppercase())
21///     -> assert_eq(["HELLO", "WORLD"]);
22/// ```
23pub const MAP: OperatorConstraints = OperatorConstraints {
24    name: "map",
25    categories: &[OperatorCategory::Map],
26    hard_range_inn: RANGE_1,
27    soft_range_inn: RANGE_1,
28    hard_range_out: RANGE_1,
29    soft_range_out: RANGE_1,
30    num_args: 1,
31    persistence_args: RANGE_0,
32    type_args: RANGE_0,
33    is_external_input: false,
34    has_singleton_output: false,
35    flo_type: None,
36    ports_inn: None,
37    ports_out: None,
38    input_delaytype_fn: |_| None,
39    write_fn: |&WriteContextArgs {
40                   root,
41                   op_span,
42                   ident,
43                   inputs,
44                   outputs,
45                   is_pull,
46                   arguments,
47                   ..
48               },
49               _| {
50        let func = &arguments[0];
51        let write_iterator = if is_pull {
52            let input = &inputs[0];
53            quote_spanned! {op_span=>
54                #[allow(clippy::map_clone, reason = "dfir has no explicit `cloned`/`copied` operator")]
55                let #ident = #input.map(#func);
56            }
57        } else {
58            let output = &outputs[0];
59            quote_spanned! {op_span=>
60                let #ident = #root::pusherator::map::Map::new(#func, #output);
61            }
62        };
63        Ok(OperatorWriteOutput {
64            write_iterator,
65            ..Default::default()
66        })
67    },
68};