use std::collections::{HashMap, HashSet};
#[derive(Debug, Clone, Default)]
pub struct PipelineState {
pub applied_items: HashSet<String>,
pub rule_applied: HashSet<String>,
pub detection_item_applied: HashSet<String>,
pub state: HashMap<String, serde_json::Value>,
pub vars: HashMap<String, Vec<String>>,
}
impl PipelineState {
pub fn new(vars: HashMap<String, Vec<String>>) -> Self {
Self {
vars,
..Default::default()
}
}
pub fn mark_applied(&mut self, id: &str) {
self.applied_items.insert(id.to_string());
self.rule_applied.insert(id.to_string());
self.detection_item_applied.insert(id.to_string());
}
pub fn was_applied(&self, id: &str) -> bool {
self.applied_items.contains(id) || self.rule_applied.contains(id)
}
pub fn was_applied_to_detection_item(&self, id: &str) -> bool {
self.detection_item_applied.contains(id)
}
pub fn get_state(&self, key: &str) -> Option<&serde_json::Value> {
self.state.get(key)
}
pub fn set_state(&mut self, key: String, val: serde_json::Value) {
self.state.insert(key, val);
}
pub fn state_matches(&self, key: &str, val: &str) -> bool {
self.state
.get(key)
.and_then(|v| v.as_str())
.is_some_and(|s| s == val)
}
pub fn reset_rule(&mut self) {
self.rule_applied.clear();
self.detection_item_applied.clear();
}
pub fn reset_detection_item(&mut self) {
self.detection_item_applied.clear();
}
}