1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::marker::PhantomData;

use crate::{
    attributes::Attribute,
    markers::{Init, Uninit},
    Element, Elements,
};

use super::IntoElements;

/// The content of `annotation` element, either text or MathML.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum AnnotationContent {
    /// Text content of the `annotation` element. Implies the `annotation` variant.
    Text(String),
    /// MathML content of the `annotation` element. Implies the `annotation-xml` variant.
    Nested(Elements),
}

impl From<String> for AnnotationContent {
    fn from(value: String) -> Self {
        Self::Text(value)
    }
}

impl<T> From<T> for AnnotationContent
where
    T: IntoElements,
{
    fn from(value: T) -> Self {
        Self::Nested(value.into_elements())
    }
}

impl From<Element> for AnnotationContent {
    fn from(value: Element) -> Self {
        Self::Nested(Elements(vec![value]))
    }
}

/// An attribute of `annotation` element. Either one of the global [`Attribute`]s, or `encode`
/// attribute.
///
/// [`Attribute`]: crate::attributes::Attribute
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum AnnotationAttr {
    /// One of the global [`Attribute`]s.
    Global(Attribute),

    /// NOTE: Authors can use the encoding attribute to distinguish annotations for HTML
    /// integration point, clipboard copy, alternative rendering, etc. In particular, CSS can be
    /// used to render alternative annotations e.g.
    ///
    /// ```css
    /// /* Hide the annotated child. */
    /// semantics > :first-child { display: none; }
    ///  /* Show all text annotations. */
    /// semantics > annotation { display: inline; }
    /// /* Show all HTML annotations. */
    /// semantics > annotation-xml[encoding="text/html" i],
    /// semantics > annotation-xml[encoding="application/xhtml+xml" i] {
    /// display: inline-block;
    /// }
    /// ```
    Encoding(String),
}

impl From<Attribute> for AnnotationAttr {
    fn from(value: Attribute) -> Self {
        Self::Global(value)
    }
}

/// The `annotation` (and `annotation-xml`) element.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Annotation {
    content: AnnotationContent,
    attributes: Vec<AnnotationAttr>,
}

crate::element_from_type!(Annotation => Annotation);

impl Annotation {
    /// Create a builder for [`Annotation`] element.
    pub fn builder() -> AnnotationBuilder<Uninit> {
        AnnotationBuilder::default()
    }

    /// Get a reference to the inner content of the [`Annotation`] element.
    pub fn content(&self) -> &AnnotationContent {
        &self.content
    }

    /// Get a reference to all attributes of the [`Annotation`] element.
    pub fn attributes(&self) -> &[AnnotationAttr] {
        &self.attributes
    }
}

/// Builder of the [`Annotation`] element.
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct AnnotationBuilder<T> {
    content: Option<AnnotationContent>,
    attr: Vec<AnnotationAttr>,

    _marker: PhantomData<(T,)>,
}

impl<T> AnnotationBuilder<T> {
    /// Set the content of the [`Annotation`] element.
    pub fn content(self, content: impl Into<AnnotationContent>) -> AnnotationBuilder<Init> {
        AnnotationBuilder {
            content: Some(content.into()),
            attr: self.attr,
            _marker: PhantomData,
        }
    }

    /// Add attributes.
    pub fn attr<I, A>(mut self, attr: I) -> Self
    where
        I: IntoIterator<Item = A>,
        A: Into<AnnotationAttr>,
    {
        self.attr.extend(attr.into_iter().map(Into::into));
        self
    }
}

impl AnnotationBuilder<Init> {
    /// Build the [`Annotation`] element.
    pub fn build(self) -> Annotation {
        Annotation {
            content: self
                .content
                .expect("Content is guaranteed to be initialized at compile time."),
            attributes: self.attr,
        }
    }
}

/// The `semantics` element is the container element that associates annotations with a MathML
/// expression. Typically, the `semantics` element has as its first child element a MathML
/// expression to be annotated while subsequent child elements represent text annotations within an
/// `annotation` element, or more complex markup annotations within an `annotation-xml` element.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Semantics {
    /// Children of the `semantics` element. Rendering is same as `mrow`.
    children: Elements,

    /// The `semantics` element accepts the global [`Attribute`]s.
    attr: Vec<Attribute>,
}

impl Semantics {
    /// Create a builder for [`Semantics`] element.
    pub fn builder() -> SemanticsBuilder<Uninit> {
        SemanticsBuilder::default()
    }

    /// Get a reference to the inner content of the [`Semantics`] element.
    pub fn children(&self) -> &[Element] {
        &self.children
    }

    /// Get a reference to all attributes of the [`Semantics`] element.
    pub fn attributes(&self) -> &[Attribute] {
        &self.attr
    }
}

crate::element_from_type!(Semantics => Semantics);

/// Builder of the [`Semantics`] element.
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct SemanticsBuilder<T> {
    content: Option<Elements>,
    attr: Vec<Attribute>,

    _marker: PhantomData<(T,)>,
}

impl<T> SemanticsBuilder<T> {
    /// Set the content of the [`Semantics`] element.
    pub fn content(self, content: impl IntoElements) -> SemanticsBuilder<Init> {
        SemanticsBuilder {
            content: Some(content.into_elements()),
            attr: self.attr,
            _marker: PhantomData,
        }
    }

    /// Add attributes.
    pub fn attr<A>(mut self, attr: A) -> Self
    where
        A: IntoIterator<Item = Attribute>,
    {
        self.attr.extend(attr);
        self
    }
}

impl SemanticsBuilder<Init> {
    /// Build the [`Semantics`] element.
    pub fn build(self) -> Semantics {
        Semantics {
            children: self
                .content
                .expect("Content is guaranteed to be initialized at compile time."),
            attr: self.attr,
        }
    }
}