daa_rules/
context.rs

1//! Execution context for rules
2
3use std::collections::HashMap;
4use chrono::{DateTime, Utc};
5use serde::{Deserialize, Serialize};
6
7/// Execution context containing variables and state for rule evaluation
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct ExecutionContext {
10    /// Variables available to rules
11    variables: HashMap<String, String>,
12    
13    /// Execution timestamp
14    timestamp: DateTime<Utc>,
15    
16    /// Context metadata
17    metadata: HashMap<String, String>,
18}
19
20impl ExecutionContext {
21    /// Create a new execution context
22    pub fn new() -> Self {
23        Self {
24            variables: HashMap::new(),
25            timestamp: Utc::now(),
26            metadata: HashMap::new(),
27        }
28    }
29
30    /// Set a variable value
31    pub fn set_variable(&mut self, key: String, value: String) {
32        self.variables.insert(key, value);
33    }
34
35    /// Get a variable value
36    pub fn get_variable(&self, key: &str) -> Option<&String> {
37        self.variables.get(key)
38    }
39
40    /// Get all variables
41    pub fn get_variables(&self) -> &HashMap<String, String> {
42        &self.variables
43    }
44
45    /// Get execution timestamp
46    pub fn timestamp(&self) -> DateTime<Utc> {
47        self.timestamp
48    }
49
50    /// Set metadata
51    pub fn set_metadata(&mut self, key: String, value: String) {
52        self.metadata.insert(key, value);
53    }
54
55    /// Get metadata
56    pub fn get_metadata(&self, key: &str) -> Option<&String> {
57        self.metadata.get(key)
58    }
59}
60
61impl Default for ExecutionContext {
62    fn default() -> Self {
63        Self::new()
64    }
65}