Skip to main content

panopticon_core/execution/
context.rs

1/// The current position inside an iteration — either a numeric array
2/// index or a borrowed map key.
3///
4/// Paired with [`IterContext`] in [`HookEvent`](crate::extend::HookEvent)
5/// variants so hooks can report or filter on iteration progress. The
6/// `Display` impl renders the index for log output.
7#[derive(Debug, Clone)]
8pub enum IterIndex<'a> {
9    /// The current element's zero-based index in an array iteration.
10    Array(usize),
11    /// The current entry's key in a map iteration.
12    Map(&'a str),
13}
14
15/// Per-iteration context surfaced to hooks for steps running inside an
16/// [`iter_array`](crate::prelude::Pipeline#method.iter_array) or
17/// [`iter_map`](crate::prelude::Pipeline#method.iter_map) body.
18///
19/// Exposes the enclosing iteration's name, the current index or key,
20/// and the nesting depth (outermost iteration is `0`). Hooks can use
21/// the nesting depth for indentation and the index for filtering or
22/// profiling per element.
23#[derive(Debug, Clone)]
24pub struct IterContext<'a> {
25    /// The name of the enclosing iteration node.
26    pub iter_name: &'a str,
27    /// The current iteration's index or key.
28    pub index: IterIndex<'a>,
29    /// Zero-based depth: `0` for the outermost iteration, `1` for one
30    /// level of nesting, and so on.
31    pub depth: usize,
32}
33
34impl std::fmt::Display for IterIndex<'_> {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        match self {
37            IterIndex::Array(i) => write!(f, "{}", i),
38            IterIndex::Map(k) => write!(f, "{}", k),
39        }
40    }
41}
42
43impl std::fmt::Display for IterContext<'_> {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        write!(f, "{}[{}]", self.iter_name, self.index)
46    }
47}