use std::collections::HashMap;
pub mod settings;
use crate::objects::Expression;
use self::settings::Rounding;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Context {
pub functions: HashMap<String, (Vec<String>, Box<Expression>)>,
pub variables: HashMap<String, Box<Expression>>,
pub rounding: settings::Rounding,
pub angle_unit: settings::AngleUnit,
pub depth_limit: settings::DepthLimit,
}
impl Context {
pub fn default() -> Self {
Self {
functions: HashMap::new(),
variables: HashMap::new(),
rounding: settings::Rounding::default(),
angle_unit: settings::AngleUnit::default(),
depth_limit: settings::DepthLimit::default(),
}
}
pub fn new(
rounding: Rounding,
angle_unit: settings::AngleUnit,
depth_limit: settings::DepthLimit,
) -> Self {
Self {
functions: HashMap::new(),
variables: HashMap::new(),
rounding,
angle_unit,
depth_limit,
}
}
pub fn join_with(&mut self, context: &Self) {
for (identifier, (params, body)) in context.functions.clone() {
self.add_function(identifier, params, body);
}
for (identifier, expression) in context.variables.clone() {
self.add_variable(identifier, expression)
}
}
pub fn add_function(&mut self, identifier: String, params: Vec<String>, body: Box<Expression>) {
self.functions.insert(identifier, (params, body));
}
pub fn add_variable(&mut self, identifier: String, expression: Box<Expression>) {
self.variables.insert(identifier, expression);
}
pub fn get_function(&self, identifier: &str) -> Option<(Vec<String>, Box<Expression>)> {
self.functions.get(identifier).cloned()
}
pub fn get_var(&self, identifier: &str) -> Option<Box<Expression>> {
self.variables.get(identifier).cloned()
}
pub fn is_function(&self, identifier: &str) -> bool {
if let Some(_) = self.get_function(identifier) {
true
} else {
false
}
}
pub fn is_var(&self, identifier: &str) -> bool {
if let Some(_) = self.get_var(identifier) {
true
} else {
false
}
}
}