mathml_core/ast/mod.rs
1mod display;
2
3use crate::{
4 MathError, MathFenced, MathFraction, MathFunction, MathIdentifier, MathMultiScript, MathNumber, MathOperator, MathPhantom,
5 MathRoot, MathRow, MathSpace, MathSqrt, MathStyle, MathTable, MathText, MathUnderOver,
6};
7use std::fmt::{Display, Formatter};
8
9/// Represent the [MathML](https://w3c.github.io/mathml/) AST node, For semantic considerations, not exactly the same as the standard.
10#[derive(Debug, Clone, PartialEq)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub enum MathML {
13 /// [`<math>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/math)
14 Root(Box<MathRoot>),
15 /// [`<mrow>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mrow)
16 Row(Box<MathRow>),
17 /// [`<mspace>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mspace)
18 Space(Box<MathSpace>),
19 /// [`<mn>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mn)
20 Number(Box<MathNumber>),
21 /// [`<mi>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mi)
22 Identifier(Box<MathIdentifier>),
23 /// [`<ms>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/ms) / [`<mtext>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mtext)
24 Text(Box<MathText>),
25 /// [`<mo>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mo)
26 Operator(Box<MathOperator>),
27 /// [`<msub>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msub)
28 /// / [`<msup>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msup)
29 /// / [`<msubsup>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msubsup)
30 /// / [`<mmultiscripts>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mmultiscripts)
31 MultiScripts(Box<MathMultiScript>),
32 /// [`<munder>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/munder) / [`<mover>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mover) / [`<munderover>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/munderover)
33 UnderOver(Box<MathUnderOver>),
34 /// Used for compatibility of [`\operatorname`]() in LaTeX
35 Function(Box<MathFunction>),
36 /// [`<msqrt>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msqrt)
37 Sqrt(Box<MathSqrt>),
38 /// [`<mfrac>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mfrac)
39 Frac(Box<MathFraction>),
40 /// [`<mphantom>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mphantom)
41 Phantom(Box<MathPhantom>),
42 /// [`<mstyle>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mstyle)
43 Style(Box<MathStyle>),
44 /// [`<mfenced>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mfenced), but polyfill as [`<mrow>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mrow)
45 Fenced(Box<MathFenced>),
46 /// [`<mtable>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mtable)
47 Table(Box<MathTable>),
48 /// Used for unknown element
49 Undefined(Box<MathError>),
50 /// Used for compatibility of `&` in LaTeX
51 Ampersand,
52 /// Used for compatibility of `\\` in LaTeX
53 NewLine,
54 /// Used for compatibility of ` ` in HTML
55 Nothing,
56}
57
58macro_rules! make_mathml {
59 ($($name:ident => $variant:ident),*) => {
60 $(
61 impl From<$name> for MathML {
62 fn from(value: $name) -> Self {
63 MathML::$variant(Box::new(value))
64 }
65 }
66 )*
67 };
68}
69
70macro_rules! make_number {
71 ($($name:ident),*) => {
72 $(
73 impl From<$name> for MathML {
74 fn from(value: $name) -> Self {
75 MathML::Number(Box::new(value.into()))
76 }
77 }
78 )*
79 };
80}
81
82impl From<char> for MathML {
83 fn from(value: char) -> Self {
84 MathML::identifier(value)
85 }
86}
87
88make_number![i8, i16, i32, i64, i128, isize];
89make_number![u8, u16, u32, u64, u128, usize];
90make_number![f32, f64];
91
92#[rustfmt::skip]
93make_mathml! {
94 MathRoot => Root,
95 MathRow => Row,
96 MathTable => Table,
97
98 MathSpace => Space,
99 MathText => Text,
100 MathNumber => Number,
101
102 MathFunction => Function,
103 MathUnderOver => UnderOver,
104
105 MathIdentifier => Identifier,
106 MathOperator => Operator,
107 MathMultiScript => MultiScripts,
108 MathSqrt => Sqrt,
109 MathFraction => Frac,
110 MathPhantom => Phantom,
111 MathStyle => Style,
112 MathFenced => Fenced,
113
114 MathError => Undefined
115}