mathml_core/blocks/
constructors.rs1use super::*;
2
3impl Default for MathRoot {
4 fn default() -> Self {
5 Self { children: Vec::new(), attributes: BTreeMap::new() }
6 }
7}
8
9impl MathElement for MathRoot {
10 fn tag_name(&self) -> &'static str {
11 "math"
12 }
13
14 fn get_attributes(&self) -> &BTreeMap<String, String> {
15 &self.attributes
16 }
17
18 fn mut_attributes(&mut self) -> &mut BTreeMap<String, String> {
19 &mut self.attributes
20 }
21}
22
23impl<T> FromIterator<T> for MathRoot
24where
25 T: Into<MathML>,
26{
27 fn from_iter<I>(iter: I) -> Self
28 where
29 I: IntoIterator<Item = T>,
30 {
31 Self { children: iter.into_iter().map(|x| x.into()).collect(), attributes: BTreeMap::new() }
32 }
33}
34
35impl MathRoot {
36 pub fn new<I>(children: I) -> Self
38 where
39 I: Iterator<Item = MathML>,
40 {
41 Self { children: children.collect(), ..Default::default() }
42 }
43 pub fn with_display_style(self, display: bool) -> Self {
45 let display = if display { "block" } else { "inline" };
46 self.with_attribute("display", display)
47 }
48 pub fn with_namespace(self) -> Self {
50 self.with_attribute("xmlns", "http://www.w3.org/1998/Math/MathML")
51 }
52}
53
54impl<T> FromIterator<T> for MathRow
55where
56 T: Into<MathML>,
57{
58 fn from_iter<I>(iter: I) -> Self
59 where
60 I: IntoIterator<Item = T>,
61 {
62 Self { children: iter.into_iter().map(|x| x.into()).collect(), grouped: false }
63 }
64}
65
66impl MathRow {
67 pub fn new<I>(items: I) -> Self
69 where
70 I: IntoIterator<Item = MathML>,
71 {
72 Self { children: items.into_iter().collect(), grouped: false }
73 }
74 pub fn group<I>(items: I) -> Self
76 where
77 I: IntoIterator<Item = MathML>,
78 {
79 Self { children: items.into_iter().collect(), grouped: true }
80 }
81 pub fn get_items(&self) -> &[MathML] {
83 &self.children
84 }
85 pub fn mut_items(&mut self) -> &mut Vec<MathML> {
87 &mut self.children
88 }
89}
90
91impl MathElement for MathStyle {
92 fn tag_name(&self) -> &'static str {
93 "mstyle"
94 }
95
96 fn get_attributes(&self) -> &BTreeMap<String, String> {
97 &self.attributes
98 }
99
100 fn mut_attributes(&mut self) -> &mut BTreeMap<String, String> {
101 &mut self.attributes
102 }
103}
104
105impl MathStyle {
106 pub fn display<M>(base: M) -> Self
108 where
109 M: Into<MathML>,
110 {
111 Self { base: base.into(), attributes: Default::default() }.with_attribute("displaystyle", "true")
112 }
113 pub fn inline<M>(base: M) -> Self
115 where
116 M: Into<MathML>,
117 {
118 Self { base: base.into(), attributes: Default::default() }.with_attribute("displaystyle", "false")
119 }
120}
121impl MathPhantom {
122 pub fn new(inner: MathML) -> Self {
124 Self { inner }
125 }
126 pub fn get_inner(&self) -> &MathML {
128 &self.inner
129 }
130 pub fn mut_inner(&mut self) -> &mut MathML {
132 &mut self.inner
133 }
134}
135
136impl MathFunction {
137 pub fn new<S, I>(name: S, body: I) -> Self
139 where
140 S: ToString,
141 I: IntoIterator<Item = MathML>,
142 {
143 Self { name: name.to_string(), body: body.into_iter().collect() }
144 }
145 pub fn add_argument(&mut self, argument: MathML) {
147 self.body.push(argument);
148 }
149 pub fn get_arguments(&self) -> &[MathML] {
151 &self.body
152 }
153 pub fn mut_arguments(&mut self) -> &mut Vec<MathML> {
155 &mut self.body
156 }
157}
158
159impl MathElement for MathTable {
160 fn tag_name(&self) -> &'static str {
161 "mtable"
162 }
163
164 fn get_attributes(&self) -> &BTreeMap<String, String> {
165 &self.attributes
166 }
167
168 fn mut_attributes(&mut self) -> &mut BTreeMap<String, String> {
169 &mut self.attributes
170 }
171}
172
173impl MathTable {
174 pub fn matrix<I>(stream: I) -> Self
176 where
177 I: IntoIterator<Item = MathML>,
178 {
179 Self { stream: stream.into_iter().collect(), attributes: BTreeMap::new() }
180 }
181}