mathml_rs/structs/
apply.rs

1use super::lambda::Lambda;
2use super::math_node::{MathNode, MathNodeType, NodeIndex};
3use super::op::Op;
4use std::fmt;
5
6#[derive(Default, Debug, Clone, Eq, PartialEq)]
7pub struct Apply {
8    pub children: Vec<NodeIndex>,
9    pub operator: Option<NodeIndex>,
10    pub operands: Vec<NodeIndex>,
11    pub parent: Option<NodeIndex>,
12}
13
14impl Apply {
15    pub fn index(&mut self, _tag_type: MathNodeType, location: NodeIndex) {
16        if self.children.len() == 1 {
17            self.operator = Some(location);
18        } else {
19            self.operands.push(location);
20        }
21    }
22
23    pub fn get_op(&self, nodes: &[MathNode]) -> Result<Op, &'static str> {
24        let operator_idx = self.operator.expect("No operator found!");
25        if let MathNode::Op(opnode) = &nodes[operator_idx] {
26            return Ok(opnode.op.clone().unwrap());
27        }
28        Err("Not a regular mathematical operator.")
29    }
30
31    pub fn get_lambda(&self, nodes: &[MathNode]) -> Result<Lambda, &'static str> {
32        let operator_idx = self.operator.expect("No operator found!");
33        if let MathNode::Lambda(lambda) = &nodes[operator_idx] {
34            return Ok(lambda.clone());
35        }
36        Err("Not a lambda function.")
37    }
38}
39
40impl fmt::Display for Apply {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        write!(
43            f,
44            "Operator: {:?}, Operands: {:?}, Children: {:?}, Parent: {:?}",
45            self.operator, self.operands, self.children, self.parent
46        )
47    }
48}