use crate::error;
use std::collections::HashMap;
pub struct Context {
vars: HashMap<String, f32>,
}
impl Default for Context {
fn default() -> Self {
Self::new()
}
}
impl Context {
pub fn new() -> Self {
Self {
vars: HashMap::new(),
}
}
pub fn with_variable(mut self, name: &str, val: f32) -> Self {
let _ = self.vars.insert(name.to_string(), val);
self
}
pub fn add_variable(&mut self, name: &str, val: f32) -> Result<(), error::ContextError> {
let name = name.to_string();
let old_val = self.vars.insert(name.clone(), val);
match old_val {
Some(val) => Err(error::ContextError::VariableAlreadyDefined(name, val)),
None => Ok(()),
}
}
pub fn get_variable(&self, name: &String) -> Result<f32, error::ContextError> {
match self.vars.get(name) {
Some(r) => Ok(*r),
None => Err(error::ContextError::VariableNotFound),
}
}
}