Skip to main content

ui_automata/
output.rs

1use std::collections::HashMap;
2
3/// Workflow output buffer. Populated by `Extract` actions during execution
4/// and returned to the caller after the workflow completes.
5///
6/// Each key maps to an ordered list of extracted string values. A key written
7/// by a non-`multiple` Extract will have exactly one entry; a `multiple` Extract
8/// appends all matched values in document order.
9#[derive(Debug, Clone, Default)]
10pub struct Output {
11    data: HashMap<String, Vec<String>>,
12}
13
14impl Output {
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    /// Append a value under `key`.
20    pub fn push(&mut self, key: impl Into<String>, value: impl Into<String>) {
21        self.data.entry(key.into()).or_default().push(value.into());
22    }
23
24    /// All values stored under `key`, or an empty slice if the key was never written.
25    pub fn get(&self, key: &str) -> &[String] {
26        self.data.get(key).map(Vec::as_slice).unwrap_or(&[])
27    }
28
29    /// Consume and return the underlying map.
30    pub fn into_map(self) -> HashMap<String, Vec<String>> {
31        self.data
32    }
33
34    /// Reference to the underlying map.
35    pub fn as_map(&self) -> &HashMap<String, Vec<String>> {
36        &self.data
37    }
38
39    pub fn is_empty(&self) -> bool {
40        self.data.is_empty()
41    }
42
43    /// Merge all key/value entries from `other` into this output.
44    /// Values are appended in order under each key.
45    pub fn merge(&mut self, other: Output) {
46        for (key, values) in other.data {
47            self.data.entry(key).or_default().extend(values);
48        }
49    }
50}