1use super::tag::TagIndex;
2use mathml_rs::evaluate_node;
3pub use mathml_rs::MathNode;
4use std::collections::HashMap;
5use std::fmt;
6
7#[derive(Clone, Debug, Default)]
8pub struct MathTag {
9 pub nodes: Vec<MathNode>,
10 pub parent: Option<TagIndex>,
11}
12
13impl fmt::Display for MathTag {
14 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15 let mut count = 0;
16 for node in &self.nodes {
17 writeln!(f, "{:2}: {}", count, node)?;
18 count += 1;
19 }
20 Ok(())
21 }
22}
23
24#[allow(dead_code)]
25impl MathTag {
26 pub fn with_nodes(mut self, nodes: Vec<MathNode>) -> Self {
27 self.nodes = nodes;
28 self
29 }
30
31 pub fn with_parent(mut self, parent: TagIndex) -> Self {
32 self.parent = Some(parent);
33 self
34 }
35
36 pub fn evaluate(
37 &self,
38 assignments: &HashMap<String, f64>,
39 functions: &HashMap<String, Vec<MathNode>>,
40 ) -> Result<f64, String> {
41 evaluate_node(&self.nodes, 0, assignments, functions)
42 }
43}