mathml_core/blocks/
constructors.rs

1use 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    /// Create a simple math space without any attributes, the unit is `rem`.
37    pub fn new<I>(children: I) -> Self
38    where
39        I: Iterator<Item = MathML>,
40    {
41        Self { children: children.collect(), ..Default::default() }
42    }
43    /// Create a simple math space without any attributes, the unit is `rem`.
44    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    /// Create a simple math space without any attributes, the unit is `rem`.
49    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    /// Create a simple math space without any attributes, the unit is `rem`.
68    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    /// Create a simple math space without any attributes, the unit is `rem`.
75    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    /// Create a simple math space without any attributes, the unit is `rem`.
82    pub fn get_items(&self) -> &[MathML] {
83        &self.children
84    }
85    /// Create a simple math space without any attributes, the unit is `rem`.
86    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    /// Create a simple math space without any attributes, the unit is `rem`.
107    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    /// Create a simple math space without any attributes, the unit is `rem`.
114    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    /// Create a simple math space without any attributes, the unit is `rem`.
123    pub fn new(inner: MathML) -> Self {
124        Self { inner }
125    }
126    /// Create a simple math space without any attributes, the unit is `rem`.
127    pub fn get_inner(&self) -> &MathML {
128        &self.inner
129    }
130    /// Create a simple math space without any attributes, the unit is `rem`.
131    pub fn mut_inner(&mut self) -> &mut MathML {
132        &mut self.inner
133    }
134}
135
136impl MathFunction {
137    /// Create a simple math space without any attributes, the unit is `rem`.
138    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    /// Create a simple math space without any attributes, the unit is `rem`.
146    pub fn add_argument(&mut self, argument: MathML) {
147        self.body.push(argument);
148    }
149    /// Create a simple math space without any attributes, the unit is `rem`.
150    pub fn get_arguments(&self) -> &[MathML] {
151        &self.body
152    }
153    /// Create a simple math space without any attributes, the unit is `rem`.
154    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    /// Create a simple math space without any attributes, the unit is `rem`.
175    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}