diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-01-17 18:24:21 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-01-17 18:24:21 +0100 |
| commit | 5ccc6e5ec8a433f68bfeb17e8dcedec5b62a36a9 (patch) | |
| tree | 1101f7eeffd038a68d0a6a3e8a6cb77e39a537e5 /src/context.rs | |
First sketch of matheval
Diffstat (limited to 'src/context.rs')
| -rw-r--r-- | src/context.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/context.rs b/src/context.rs new file mode 100644 index 0000000..12bea6b --- /dev/null +++ b/src/context.rs @@ -0,0 +1,51 @@ +use crate::function::Function; +use crate::operation::Operation; +use std::collections::HashMap; + +#[derive(Default)] +pub struct Context { + ops: Vec<Operation>, + vars: HashMap<String, f64>, + funcs: HashMap<String, Box<dyn Function>>, +} + +impl Context { + pub fn new() -> Self { + Self::default() + } + + pub fn with_operations(mut self, ops: Vec<Operation>) -> Self { + self.ops = ops; + self + } + + pub fn with_variables(mut self, vars: HashMap<String, f64>) -> Self { + self.vars = vars; + self + } + + pub fn with_functions(mut self, funcs: HashMap<String, Box<dyn Function>>) -> Self { + self.funcs = funcs; + self + } + + pub fn operations(&self) -> &Vec<Operation> { + &self.ops + } + + pub fn variable_mut(&mut self, name: &str) -> Option<&mut f64> { + self.vars.get_mut(name) + } + + pub fn function_mut(&mut self, name: &str) -> Option<&mut Box<dyn Function>> { + self.funcs.get_mut(name) + } + + pub fn variable(&self, name: &str) -> Option<&f64> { + self.vars.get(name) + } + + pub fn function(&self, name: &str) -> Option<&Box<dyn Function>> { + self.funcs.get(name) + } +} |