1use std::borrow::Cow;
2
3use libxml::tree::Node;
4
5use super::SizingClosure;
6use crate::{
7 Digested,
8 common::{
9 arena::SymHashMap as HashMap, error::*, font::LettersClass, object::Object, store::Stored,
10 },
11 definition::{
12 BeforeDigestClosure, ConstructionClosure, Definition, DigestionClosure, FontDirective,
13 PrimitiveClosure, Reversion,
14 },
15 document::Document,
16 parameter::Parameters,
17 state::Scope,
18 token::*,
19 tokens::Tokens,
20 whatsit::Whatsit,
21};
22
23#[derive(Clone, Default)]
39pub struct MathPrimitiveOptions {
40 pub bounded: bool,
41 pub mode: Option<String>,
42 pub before_digest: Vec<BeforeDigestClosure>,
43 pub after_digest: Vec<DigestionClosure>,
44 pub before_construct: Vec<ConstructionClosure>,
45 pub after_construct: Vec<ConstructionClosure>,
46 pub is_prefix: bool,
47 pub scope: Option<Scope>,
48 pub font: Option<FontDirective>,
49 pub require_math: bool,
50 pub forbid_math: bool,
51 pub locked: bool,
52 pub alias: Option<String>,
53 pub decl_id: Option<String>,
54 pub replace: Option<String>,
55 pub protected: bool,
56 pub robust: bool,
57
58 pub name: Option<String>,
60 pub meaning: Option<String>,
61 pub omcd: Option<String>,
62 pub reversion: Option<Reversion>,
63 pub sizer: Option<SizingClosure>,
64 pub role: Option<String>,
65 pub operator_role: Option<String>,
66 pub reorder: bool,
67 pub dual: bool,
68 pub mathstyle: Option<String>,
69 pub variablesize_op: bool,
74 pub scriptpos: Option<String>,
75 pub dynamic_scriptpos: bool,
78 pub operator_scriptpos: Option<usize>,
79 pub stretchy: Option<bool>,
80 pub operator_stretchy: Option<bool>,
81 pub nogroup: bool,
82 pub hide_content_reversion: bool,
83 pub revert_as: Option<Cow<'static, str>>,
84 pub lpadding: Option<usize>,
85 pub rpadding: Option<usize>,
86 pub letters_class: Option<LettersClass>,
89 pub absorb_warning: Option<Stored>,
96}
97impl PartialEq for MathPrimitiveOptions {
98 fn eq(&self, other: &MathPrimitiveOptions) -> bool {
99 self.name == other.name && self.meaning == other.meaning && self.role == other.role
100 }
101}
102
103impl MathPrimitiveOptions {
104 pub fn to_hash_stored(&self) -> HashMap<Stored> {
105 let mut h = HashMap::default();
106 if let Some(ref meaning) = self.meaning {
107 h.insert("meaning", meaning.into());
108 }
109 if let Some(ref name) = self.name {
110 h.insert("name", name.into());
111 }
112 if let Some(ref omcd) = self.omcd {
113 h.insert("omcd", omcd.into());
114 }
115 if let Some(ref role) = self.role {
116 h.insert("role", role.into());
117 }
118 if let Some(ref decl_id) = self.decl_id {
119 h.insert("decl_id", decl_id.into());
120 }
121 if let Some(ref warning) = self.absorb_warning {
122 h.insert(crate::document::ABSORB_WARNING_PROPERTY, warning.clone());
123 }
124 if let Some(ref operator_role) = self.operator_role {
125 h.insert("operator_role", operator_role.into());
126 }
127 if let Some(ref mathstyle) = self.mathstyle {
128 h.insert("mathstyle", mathstyle.into());
129 }
130 if let Some(ref scriptpos) = self.scriptpos {
131 h.insert("scriptpos", scriptpos.into());
132 }
133 if let Some(ref operator_scriptpos) = self.operator_scriptpos {
134 h.insert(
135 "operator_scriptpos",
136 Stored::Int(*operator_scriptpos as i64),
137 );
138 }
139 if let Some(ref stretchy) = self.stretchy {
140 h.insert("stretchy", (*stretchy).into());
141 }
142 if let Some(ref stretchy) = self.operator_stretchy {
143 h.insert("operator_stretchy", (*stretchy).into());
144 }
145 if let Some(ref mode) = self.mode {
146 h.insert("mode", mode.into());
147 }
148 if let Some(ref font_directive) = self.font {
150 h.insert("font", Stored::FontDirective(font_directive.clone()));
151 }
152 if let Some(ref lpadding) = self.lpadding {
153 h.insert("lpadding", (*lpadding).into());
154 }
155 if let Some(ref rpadding) = self.rpadding {
156 h.insert("rpadding", (*rpadding).into());
157 }
158
159 h
160 }
161
162 pub fn to_hash_stored_with_overrides(
166 &self,
167 mode_override: Option<&'static str>,
168 mathstyle_override: Option<&'static str>,
169 scriptpos_override: Option<&'static str>,
170 ) -> HashMap<Stored> {
171 let mut h = self.to_hash_stored();
172 if let Some(m) = mode_override {
173 h.insert("mode", Stored::String(crate::common::arena::pin_static(m)));
174 }
175 if let Some(ms) = mathstyle_override {
176 h.insert(
177 "mathstyle",
178 Stored::String(crate::common::arena::pin_static(ms)),
179 );
180 }
181 if let Some(sp) = scriptpos_override {
182 h.insert(
183 "scriptpos",
184 Stored::String(crate::common::arena::pin_static(sp)),
185 );
186 }
187 h
188 }
189
190 pub fn has_complex_option(&self) -> bool {
199 self.bounded
201 || self.nogroup
202 || self.mode.is_some()
203 || !self.before_digest.is_empty()
204 || !self.after_digest.is_empty()
205 || self.is_prefix
206 || self.require_math
207 || self.forbid_math
208 || self.decl_id.is_some()
209 || self.replace.is_some()
210 || self.reversion.is_some()
211 || self.sizer.is_some()
212 || self.operator_role.is_some()
213 || self.reorder
214 || self.dual
215 || self.operator_scriptpos.is_some()
216 || self.operator_stretchy.is_some()
217 || self.hide_content_reversion
218 || self.revert_as.is_some()
219 || !self.before_construct.is_empty()
220 || !self.after_construct.is_empty()
221 }
222}
223
224#[derive(Clone)]
225pub struct MathPrimitive {
226 pub cs: Token,
227 pub paramlist: Option<Parameters>,
228 pub nargs: Option<usize>,
229 pub replacement: Option<PrimitiveClosure>,
230 pub options: MathPrimitiveOptions,
231 pub alias: Option<String>,
232 pub origin: crate::definition::origin::DefinitionOrigin,
233}
234impl Default for MathPrimitive {
235 fn default() -> Self {
236 MathPrimitive {
237 cs: T_CS!("MathPrimitive"),
238 paramlist: None,
239 nargs: None,
240 replacement: None,
241 options: MathPrimitiveOptions::default(),
242 alias: None,
243 origin: crate::definition::origin::current_origin(),
244 }
245 }
246}
247impl PartialEq for MathPrimitive {
248 fn eq(&self, other: &MathPrimitive) -> bool { self.cs == other.cs }
249}
250
251impl Object for MathPrimitive {
257 fn stringify(&self) -> String { <Self as Definition>::stringify_type(self, "MathPrimitive") }
258}
259impl Definition for MathPrimitive {
260 fn before_digest(&self) -> Option<&Vec<BeforeDigestClosure>> { Some(&self.options.before_digest) }
261 fn after_digest(&self) -> Option<&Vec<DigestionClosure>> { Some(&self.options.after_digest) }
262 fn invoke(&self, _once_only: bool) -> Result<Tokens> { Ok(Tokens!()) }
263 fn invoke_primitive(&self) -> Result<Vec<Digested>> {
264 let mut result: Vec<Digested> = self.execute_before_digest()?;
270 let args = self.read_arguments()?;
271 let replacement_result = match self.replacement {
273 None => Vec::new(),
274 Some(ref closure) => closure(args)?,
275 };
276 result.extend(replacement_result);
277 let mut w = Whatsit::default();
278 let after_result = self.execute_after_digest(&mut w)?;
279 result.extend(after_result);
280
281 Ok(result)
283 }
284
285 fn do_absorption(&self, _document: &mut Document, _whatsit: &Whatsit) -> Result<Vec<Node>> {
286 fatal!(
287 Definition,
288 Unexpected,
289 "do_absorption on MathPrimitive should never be called!"
290 );
291 }
292
293 fn get_cs(&self) -> Cow<'_, Token> { Cow::Borrowed(&self.cs) }
294 fn get_cs_name(&self) -> Cow<'_, str> { Cow::Owned(self.cs.with_cs_name(ToString::to_string)) }
295 fn get_alias(&self) -> Option<&String> { self.alias.as_ref() }
296 fn get_parameters(&self) -> Option<&Parameters> { self.paramlist.as_ref() }
297 fn get_num_args(&self) -> usize {
298 match self.nargs {
299 Some(n) => n,
300 None => match self.paramlist {
301 Some(ref params) => params.get_num_args(),
302 None => 0,
303 },
304 }
305 }
308}
309
310#[cfg(test)]
311mod tests {
312 use super::*;
313
314 #[test]
315 fn math_primitive_options_default_fields() {
316 let o = MathPrimitiveOptions::default();
317 assert!(!o.bounded);
319 assert!(!o.is_prefix);
320 assert!(!o.require_math);
321 assert!(!o.forbid_math);
322 assert!(!o.locked);
323 assert!(!o.robust);
324 assert!(!o.protected);
325 assert!(!o.reorder);
326 assert!(!o.dual);
327 assert!(!o.variablesize_op);
328 assert!(!o.dynamic_scriptpos);
329 assert!(
330 !o.nogroup,
331 "a DefMath constructor digests in its own group unless nogroup (Package.pm:1707)"
332 );
333 assert!(!o.hide_content_reversion);
334 assert!(o.name.is_none());
335 assert!(o.meaning.is_none());
336 assert!(o.role.is_none());
337 assert!(o.operator_role.is_none());
338 assert!(o.mathstyle.is_none());
339 assert!(o.scriptpos.is_none());
340 assert!(o.operator_scriptpos.is_none());
341 assert!(o.stretchy.is_none());
342 assert!(o.operator_stretchy.is_none());
343 assert!(o.revert_as.is_none());
344 assert!(o.lpadding.is_none());
345 assert!(o.rpadding.is_none());
346 assert!(o.before_digest.is_empty());
347 assert!(o.after_digest.is_empty());
348 }
349
350 #[test]
351 fn math_primitive_options_partial_eq_by_subset() {
352 let mut a = MathPrimitiveOptions::default();
355 let mut b = MathPrimitiveOptions::default();
356 a.name = Some("plus".into());
357 b.name = Some("plus".into());
358 assert!(a == b, "same name equal");
359 a.locked = true;
361 assert!(a == b);
362 b.name = Some("times".into());
364 assert!(!(a == b));
365 }
366
367 #[test]
368 fn math_primitive_options_to_hash_stored_empty_default() {
369 let o = MathPrimitiveOptions::default();
372 let h = o.to_hash_stored();
373 assert_eq!(h.len(), 0);
374 }
375
376 #[test]
377 fn math_primitive_options_to_hash_stored_with_fields() {
378 let o = MathPrimitiveOptions {
379 meaning: Some("plus".into()),
380 name: Some("+".into()),
381 ..Default::default()
382 };
383 let h = o.to_hash_stored();
384 assert!(h.contains_key("meaning"));
385 assert!(h.contains_key("name"));
386 assert!(
387 !h.contains_key("role"),
388 "role=None shouldn't populate the hash"
389 );
390 }
391
392 #[test]
393 fn math_primitive_default_fields() {
394 let m = MathPrimitive::default();
395 assert!(m.paramlist.is_none());
396 assert!(m.replacement.is_none());
397 assert!(m.alias.is_none());
398 assert!(m.nargs.is_none());
399 assert!(m.options.reversion.is_none());
401 assert!(!m.options.is_prefix);
402 }
403
404 #[test]
405 fn math_primitive_partial_eq_by_cs() {
406 let a = MathPrimitive::default();
407 let b = MathPrimitive::default();
408 assert!(a == b);
410 }
411}