aboutsummaryrefslogtreecommitdiff
path: root/src/context.rs
blob: 12bea6b44934edc7ac16eb2aae5485b76576fe77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)
    }
}