use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone)]
pub struct InferenceContext {
variables: HashMap<String, String>,
start_time: u64,
metadata: HashMap<String, serde_json::Value>,
}
impl InferenceContext {
pub fn new() -> Self {
Self {
variables: HashMap::new(),
start_time: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs(),
metadata: HashMap::new(),
}
}
pub fn bind_variable(&mut self, name: String, value: String) {
self.variables.insert(name, value);
}
pub fn get_variable(&self, name: &str) -> Option<&String> {
self.variables.get(name)
}
pub fn is_bound(&self, name: &str) -> bool {
self.variables.contains_key(name)
}
pub fn get_all_variables(&self) -> &HashMap<String, String> {
&self.variables
}
pub fn set_metadata(&mut self, key: String, value: serde_json::Value) {
self.metadata.insert(key, value);
}
pub fn get_metadata(&self, key: &str) -> Option<&serde_json::Value> {
self.metadata.get(key)
}
pub fn start_time(&self) -> u64 {
self.start_time
}
pub fn reset(&mut self) {
self.variables.clear();
self.metadata.clear();
self.start_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
}
pub fn create_child(&self) -> Self {
Self {
variables: self.variables.clone(),
start_time: self.start_time,
metadata: self.metadata.clone(),
}
}
}
impl Default for InferenceContext {
fn default() -> Self {
Self::new()
}
}