mathml_rs/structs/
piecewise.rs1use super::math_node::{MathNodeType, NodeIndex};
2use std::fmt;
3
4#[derive(Default, Debug, Clone)]
5pub struct Piecewise {
6 pub children: Vec<NodeIndex>,
7 pub pieces: Vec<NodeIndex>,
8 pub otherwise: Option<NodeIndex>,
9 pub parent: Option<NodeIndex>,
10}
11
12impl Piecewise {
13 pub fn index(&mut self, tag_type: MathNodeType, location: NodeIndex) {
14 match tag_type {
15 MathNodeType::Piece => {
16 self.pieces.push(location);
17 }
18 MathNodeType::Otherwise => {
19 if self.otherwise == None {
20 self.otherwise = Some(location);
21 } else {
22 panic!("Can't have multiple otherwise expressions in an piecewise function!");
23 }
24 }
25 MathNodeType::Op
26 | MathNodeType::Ci
27 | MathNodeType::Cn
28 | MathNodeType::Root
29 | MathNodeType::BVar
30 | MathNodeType::Apply
31 | MathNodeType::Lambda
32 | MathNodeType::Piecewise
33 | MathNodeType::Constant => {
34 panic!("Can't have {} in a piecewise function!", tag_type);
35 }
36 }
37 }
38}
39
40impl fmt::Display for Piecewise {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 write!(
43 f,
44 "Pieces: {:?}, Otherwise: {:?}, Children: {:?}, Parent: {:?}",
45 self.pieces, self.otherwise, self.children, self.parent
46 )
47 }
48}
49
50#[derive(Default, Debug, Clone)]
51pub struct Piece {
52 pub children: Vec<NodeIndex>,
53 pub expr: Option<NodeIndex>,
54 pub condition: Option<NodeIndex>,
55 pub parent: Option<NodeIndex>,
56}
57
58impl Piece {
59 pub fn index(&mut self, _tag_type: MathNodeType, location: NodeIndex) {
60 if self.children.len() == 1 {
61 self.expr = Some(location);
62 } else if self.children.len() == 2 {
63 self.condition = Some(location);
64 } else {
65 panic!("A piece in a piecewise function can have only two children.");
66 }
67 }
68}
69
70impl fmt::Display for Piece {
71 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72 write!(
73 f,
74 "Expr: {:?}, Condition: {:?}, Children: {:?}, Parent: {:?}",
75 self.expr, self.condition, self.children, self.parent
76 )
77 }
78}
79
80#[derive(Default, Debug, Clone)]
81pub struct Otherwise {
82 pub children: Vec<NodeIndex>,
83 pub expr: Option<NodeIndex>,
84 pub parent: Option<NodeIndex>,
85}
86
87impl Otherwise {
88 pub fn index(&mut self, tag_type: MathNodeType, location: NodeIndex) {
89 match tag_type {
90 MathNodeType::Apply
91 | MathNodeType::Lambda
92 | MathNodeType::Ci
93 | MathNodeType::Cn
94 | MathNodeType::Piecewise
95 | MathNodeType::Constant => {
96 if self.expr == None {
97 self.expr = Some(location);
98 } else {
99 panic!("Can't have two children in an \"otherwise\" branch!");
100 }
101 }
102 MathNodeType::Root
103 | MathNodeType::Op
104 | MathNodeType::Otherwise
105 | MathNodeType::BVar
106 | MathNodeType::Piece => {
107 panic!("Can't have {} in an \"otherwise\" branch!", tag_type);
108 }
109 }
110 }
111}
112
113impl fmt::Display for Otherwise {
114 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
115 write!(
116 f,
117 "Expr: {:?}, Children: {:?}, Parent: {:?}",
118 self.expr, self.children, self.parent
119 )
120 }
121}