Skip to main content

iter_array/
iter_array.rs

1//! Iterating an array with `iter_array`.
2//!
3//! The body closure receives the current `index` and `item` binding names —
4//! pass them to `Param::reference` to read the current element. Parent-scope
5//! variables are still in scope inside the body, so you can combine iteration
6//! state with outer values. Source and body references are resolved at
7//! `compile()` time.
8
9use panopticon_core::{params, prelude::*};
10use std::time::Duration;
11
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    // ── Basic iter_array: iterate over items and capture each ──
14    println!("=== Basic iter_array ===");
15    {
16        let mut pipe = Pipeline::default();
17        pipe.array("numbers")?.push(10)?.push(20)?.push(30)?;
18
19        pipe.iter_array(
20            "process",
21            IterSource::array("numbers"),
22            |_index, item, body| {
23                body.step::<SetVar>(
24                    "capture",
25                    params!(
26                        "name" => "doubled",
27                        "value" => Param::reference(item),
28                    ),
29                )?;
30                Ok(())
31            },
32        )?;
33
34        pipe.hook(Logger::new().writer(std::io::stdout()));
35        pipe.hook(Timeout::new(Duration::from_secs(5)));
36
37        let complete = pipe.compile()?.run().wait()?;
38        complete.debug();
39    }
40
41    // ── iter_array with parent variable references ──
42    println!("\n=== iter_array referencing parent vars ===");
43    {
44        let mut pipe = Pipeline::default();
45        pipe.var("prefix", "item_")?;
46        pipe.array("items")?
47            .push("alpha")?
48            .push("beta")?
49            .push("gamma")?;
50
51        pipe.iter_array("loop", IterSource::array("items"), |_index, item, body| {
52            body.step::<SetVar>(
53                "combine",
54                params!(
55                    "name" => "label",
56                    "value" => Param::template(vec![
57                        Param::reference("prefix"),
58                        Param::reference(item),
59                    ]),
60                ),
61            )?;
62            Ok(())
63        })?;
64
65        let complete = pipe.compile()?.run().wait()?;
66        complete.debug();
67    }
68
69    // ── iter_array with multiple body steps ──
70    println!("\n=== iter_array with multi-step body ===");
71    {
72        let mut pipe = Pipeline::default();
73        pipe.var("suffix", "_processed")?;
74        pipe.array("words")?.push("hello")?.push("world")?;
75
76        pipe.iter_array("each", IterSource::array("words"), |_index, item, body| {
77            body.step::<SetVar>(
78                "tag",
79                params!(
80                    "name" => "tagged",
81                    "value" => Param::template(vec![
82                        Param::reference(item),
83                        Param::reference("suffix"),
84                    ]),
85                ),
86            )?;
87            body.step::<SetVar>(
88                "wrap",
89                params!(
90                    "name" => "wrapped",
91                    "value" => Param::template(vec![
92                        Param::literal("["),
93                        Param::reference("tagged"),
94                        Param::literal("]"),
95                    ]),
96                ),
97            )?;
98            Ok(())
99        })?;
100
101        let complete = pipe.compile()?.run().wait()?;
102        complete.debug();
103    }
104
105    // ── Error: iter_array with unresolved source ──
106    println!("\n=== iter_array unresolved source ===");
107    {
108        let mut pipe = Pipeline::default();
109
110        pipe.iter_array("loop", IterSource::array("nonexistent"), |_, _, _| Ok(()))?;
111
112        match pipe.compile() {
113            Err(e) => println!("  Caught: {}", e),
114            Ok(_) => println!("  ERROR: should have failed!"),
115        }
116    }
117
118    // ── Error: iter_array body references unknown variable ──
119    println!("\n=== iter_array unresolved body reference ===");
120    {
121        let mut pipe = Pipeline::default();
122        pipe.array("items")?.push(1)?;
123
124        pipe.iter_array("loop", IterSource::array("items"), |_, _, body| {
125            body.step::<SetVar>(
126                "bad",
127                params!(
128                    "name" => "out",
129                    "value" => Param::reference("ghost"),
130                ),
131            )?;
132            Ok(())
133        })?;
134
135        match pipe.compile() {
136            Err(e) => println!("  Caught: {}", e),
137            Ok(_) => println!("  ERROR: should have failed!"),
138        }
139    }
140
141    Ok(())
142}