Skip to main content

IterSource

Struct IterSource 

Source
pub struct IterSource { /* private fields */ }
Expand description

A draft-time reference to the store entry an iteration node should walk.

Passed to Pipeline::iter_array or Pipeline::iter_map to name the array or map the body iterates over. At runtime the reference is resolved against the runtime store; if the resolved entry is missing or has the wrong shape, the iteration fails with OperationError::IterSourceNotFound or OperationError::IterSourceTypeMismatch.

Implementations§

Source§

impl IterSource

Source

pub fn array(reference: impl Into<String>) -> Self

Builds an iter source pointing at an array entry. Use with Pipeline::iter_array.

Examples found in repository?
examples/iter_array.rs (line 21)
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}
Source

pub fn map(reference: impl Into<String>) -> Self

Builds an iter source pointing at a map entry. Use with Pipeline::iter_map.

Examples found in repository?
examples/iter_map.rs (line 23)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    // ── Basic iter_map: iterate over key-value pairs ──
13    println!("=== Basic iter_map ===");
14    {
15        let mut pipe = Pipeline::default();
16        pipe.map("config")?
17            .insert("host", "localhost")?
18            .insert("port", "8080")?
19            .insert("protocol", "https")?;
20
21        pipe.iter_map(
22            "read_config",
23            IterSource::map("config"),
24            |key, value, body| {
25                body.step::<SetVar>(
26                    "capture_key",
27                    params!(
28                        "name" => "current_key",
29                        "value" => Param::reference(key),
30                    ),
31                )?;
32                body.step::<SetVar>(
33                    "capture_value",
34                    params!(
35                        "name" => "current_value",
36                        "value" => Param::reference(value),
37                    ),
38                )?;
39                Ok(())
40            },
41        )?;
42
43        pipe.hook(Logger::new().writer(std::io::stdout()));
44        pipe.hook(Timeout::new(Duration::from_secs(5)));
45
46        let complete = pipe.compile()?.run().wait()?;
47        complete.debug();
48    }
49
50    // ── iter_map with parent variable references ──
51    println!("\n=== iter_map referencing parent vars ===");
52    {
53        let mut pipe = Pipeline::default();
54        pipe.var("env", "production")?;
55        pipe.map("settings")?
56            .insert("db_host", "db.example.com")?
57            .insert("cache_host", "cache.example.com")?;
58
59        pipe.iter_map(
60            "apply_env",
61            IterSource::map("settings"),
62            |key, value, body| {
63                body.step::<SetVar>(
64                    "label",
65                    params!(
66                        "name" => "entry",
67                        "value" => Param::template(vec![
68                            Param::reference("env"),
69                            Param::literal("."),
70                            Param::reference(key),
71                            Param::literal("="),
72                            Param::reference(value),
73                        ]),
74                    ),
75                )?;
76                Ok(())
77            },
78        )?;
79
80        let complete = pipe.compile()?.run().wait()?;
81        complete.debug();
82    }
83
84    // ── iter_map followed by regular steps ──
85    println!("\n=== iter_map then regular steps ===");
86    {
87        let mut pipe = Pipeline::default();
88        pipe.var("status", "ready")?;
89        pipe.map("headers")?
90            .insert("content-type", "application/json")?
91            .insert("accept", "text/html")?;
92
93        pipe.iter_map(
94            "scan_headers",
95            IterSource::map("headers"),
96            |_key, value, body| {
97                body.step::<SetVar>(
98                    "save",
99                    params!(
100                        "name" => "last_header",
101                        "value" => Param::reference(value),
102                    ),
103                )?;
104                Ok(())
105            },
106        )?;
107
108        // Regular step after the iteration
109        pipe.step::<SetVar>(
110            "finalize",
111            params!(
112                "name" => "done",
113                "value" => Param::reference("status"),
114            ),
115        )?;
116
117        pipe.returns(
118            "result",
119            params!(
120                "status" => Param::reference("done"),
121            ),
122        )?;
123
124        let complete = pipe.compile()?.run().wait()?;
125        complete.debug();
126    }
127
128    // ── Error: iter_map with unresolved source ──
129    println!("\n=== iter_map unresolved source ===");
130    {
131        let mut pipe = Pipeline::default();
132
133        pipe.iter_map("loop", IterSource::map("missing"), |_, _, _| Ok(()))?;
134
135        match pipe.compile() {
136            Err(e) => println!("  Caught: {}", e),
137            Ok(_) => println!("  ERROR: should have failed!"),
138        }
139    }
140
141    // ── Error: iter_map body references unknown variable ──
142    println!("\n=== iter_map unresolved body reference ===");
143    {
144        let mut pipe = Pipeline::default();
145        pipe.map("data")?.insert("k", "v")?;
146
147        pipe.iter_map("loop", IterSource::map("data"), |_, _, body| {
148            body.step::<SetVar>(
149                "bad",
150                params!(
151                    "name" => "out",
152                    "value" => Param::reference("phantom"),
153                ),
154            )?;
155            Ok(())
156        })?;
157
158        match pipe.compile() {
159            Err(e) => println!("  Caught: {}", e),
160            Ok(_) => println!("  ERROR: should have failed!"),
161        }
162    }
163
164    Ok(())
165}
Source

pub fn reference(&self) -> &str

Read-only access to the underlying store reference. Used by external callers (e.g. serialisers) that need to walk a validated execution graph.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.