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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#![allow(clippy::large_enum_variant)]

use crate::StrictEq;
use derive_more::{Constructor, From, Index, IndexMut, IntoIterator};
use serde::{Deserialize, Serialize};
use std::iter::FromIterator;

mod blocks;
pub use blocks::*;
mod utils;
pub use utils::{
    AsChildrenMutSlice, AsChildrenSlice, IntoChildren, Located, Region,
};

/// Represents a full page containing different elements
#[derive(
    Constructor,
    Clone,
    Debug,
    Default,
    Eq,
    PartialEq,
    Index,
    IndexMut,
    IntoIterator,
    Serialize,
    Deserialize,
)]
pub struct Page<'a> {
    /// Comprised of the elements within a page
    #[index]
    #[index_mut]
    #[into_iterator(owned, ref, ref_mut)]
    pub elements: Vec<Located<BlockElement<'a>>>,
}

impl<'a> Page<'a> {
    /// Returns elements within the page
    pub fn elements(&self) -> &[Located<BlockElement<'a>>] {
        &self.elements
    }

    /// Consumes the page and returns the elements within
    pub fn into_elements(self) -> Vec<Located<BlockElement<'a>>> {
        self.elements
    }
}

impl Page<'_> {
    pub fn to_borrowed(&self) -> Page {
        let elements = self
            .elements
            .iter()
            .map(|x| x.as_ref().map(BlockElement::to_borrowed))
            .collect();

        Page { elements }
    }

    pub fn into_owned(self) -> Page<'static> {
        let elements = self
            .elements
            .into_iter()
            .map(|x| x.map(BlockElement::into_owned))
            .collect();

        Page { elements }
    }
}

impl<'a> IntoChildren for Page<'a> {
    type Child = Located<BlockElement<'a>>;

    fn into_children(self) -> Vec<Self::Child> {
        self.elements
    }
}

impl<'a> FromIterator<Located<BlockElement<'a>>> for Page<'a> {
    fn from_iter<I: IntoIterator<Item = Located<BlockElement<'a>>>>(
        iter: I,
    ) -> Self {
        Self {
            elements: iter.into_iter().collect(),
        }
    }
}

impl<'a> StrictEq for Page<'a> {
    /// Performs strict_eq on page elements
    fn strict_eq(&self, other: &Self) -> bool {
        self.elements.len() == other.elements.len()
            && self
                .elements
                .iter()
                .zip(other.elements.iter())
                .all(|(x, y)| x.strict_eq(y))
    }
}

/// Represents a `BlockElement`, an `InlineElement`, or one of a handful of
/// special inbetween types like `ListItem`
#[derive(Clone, Debug, From, PartialEq, Eq, Serialize, Deserialize)]
pub enum Element<'a> {
    Block(BlockElement<'a>),
    Inline(InlineElement<'a>),
    InlineBlock(InlineBlockElement<'a>),
}

impl Element<'_> {
    pub fn to_borrowed(&self) -> Element {
        match self {
            Self::Block(x) => Element::Block(x.to_borrowed()),
            Self::Inline(x) => Element::Inline(x.to_borrowed()),
            Self::InlineBlock(x) => Element::InlineBlock(x.to_borrowed()),
        }
    }

    pub fn into_owned(self) -> Element<'static> {
        match self {
            Self::Block(x) => Element::Block(x.into_owned()),
            Self::Inline(x) => Element::Inline(x.into_owned()),
            Self::InlineBlock(x) => Element::InlineBlock(x.into_owned()),
        }
    }
}

impl<'a> IntoChildren for Element<'a> {
    type Child = Located<Element<'a>>;

    fn into_children(self) -> Vec<Self::Child> {
        match self {
            Self::Block(x) => x.into_children(),
            Self::Inline(x) => x
                .into_children()
                .into_iter()
                .map(|x| x.map(Element::from))
                .collect(),
            Self::InlineBlock(x) => x.into_children(),
        }
    }
}

impl<'a> StrictEq for Element<'a> {
    fn strict_eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Block(x), Self::Block(y)) => x.strict_eq(y),
            (Self::Inline(x), Self::Inline(y)) => x.strict_eq(y),
            (Self::InlineBlock(x), Self::InlineBlock(y)) => x.strict_eq(y),
            _ => false,
        }
    }
}

impl<'a> Element<'a> {
    pub fn as_block_element(&self) -> Option<&BlockElement<'a>> {
        match self {
            Self::Block(ref x) => Some(x),
            _ => None,
        }
    }

    pub fn into_block_element(self) -> Option<BlockElement<'a>> {
        match self {
            Self::Block(x) => Some(x),
            _ => None,
        }
    }

    pub fn as_inline_element(&self) -> Option<&InlineElement<'a>> {
        match self {
            Self::Inline(ref x) => Some(x),
            _ => None,
        }
    }

    pub fn into_inline_element(self) -> Option<InlineElement<'a>> {
        match self {
            Self::Inline(x) => Some(x),
            _ => None,
        }
    }

    pub fn as_inline_block_element(&self) -> Option<&InlineBlockElement<'a>> {
        match self {
            Self::InlineBlock(ref x) => Some(x),
            _ => None,
        }
    }

    pub fn into_inline_block_element(self) -> Option<InlineBlockElement<'a>> {
        match self {
            Self::InlineBlock(x) => Some(x),
            _ => None,
        }
    }
}

/// Represents a some element that is a descendant of a `BlockElement`, but
/// is not an `InlineElement` such as `ListItem`
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum InlineBlockElement<'a> {
    ListItem(ListItem<'a>),
    Term(Term<'a>),
    Definition(Definition<'a>),
}

impl<'a> From<ListItem<'a>> for InlineBlockElement<'a> {
    fn from(list_item: ListItem<'a>) -> Self {
        Self::ListItem(list_item)
    }
}

impl InlineBlockElement<'_> {
    pub fn to_borrowed(&self) -> InlineBlockElement {
        match self {
            Self::ListItem(x) => InlineBlockElement::ListItem(x.to_borrowed()),
            Self::Term(x) => InlineBlockElement::Term(x.to_borrowed()),
            Self::Definition(x) => {
                InlineBlockElement::Definition(x.to_borrowed())
            }
        }
    }

    pub fn into_owned(self) -> InlineBlockElement<'static> {
        match self {
            Self::ListItem(x) => InlineBlockElement::ListItem(x.into_owned()),
            Self::Term(x) => InlineBlockElement::Term(x.into_owned()),
            Self::Definition(x) => {
                InlineBlockElement::Definition(x.into_owned())
            }
        }
    }
}

impl<'a> IntoChildren for InlineBlockElement<'a> {
    type Child = Located<Element<'a>>;

    fn into_children(self) -> Vec<Self::Child> {
        match self {
            Self::ListItem(x) => x.into_children(),
            Self::Term(x) => x
                .into_children()
                .into_iter()
                .map(|x| x.map(Element::from))
                .collect(),
            Self::Definition(x) => x
                .into_children()
                .into_iter()
                .map(|x| x.map(Element::from))
                .collect(),
        }
    }
}

impl<'a> StrictEq for InlineBlockElement<'a> {
    fn strict_eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::ListItem(x), Self::ListItem(y)) => x.strict_eq(y),
            (Self::Term(x), Self::Term(y)) => x.strict_eq(y),
            (Self::Definition(x), Self::Definition(y)) => x.strict_eq(y),
            _ => false,
        }
    }
}

macro_rules! element_impl_from {
    ($type:ty, $class:ident) => {
        impl<'a> From<$type> for Element<'a> {
            fn from(value: $type) -> Self {
                Self::from($class::from(value))
            }
        }
    };
}

element_impl_from!(Blockquote<'a>, BlockElement);
element_impl_from!(DefinitionList<'a>, BlockElement);
element_impl_from!(Divider, BlockElement);
element_impl_from!(Header<'a>, BlockElement);
element_impl_from!(List<'a>, BlockElement);
element_impl_from!(MathBlock<'a>, BlockElement);
element_impl_from!(Paragraph<'a>, BlockElement);
element_impl_from!(Placeholder<'a>, BlockElement);
element_impl_from!(CodeBlock<'a>, BlockElement);
element_impl_from!(Table<'a>, BlockElement);

element_impl_from!(Text<'a>, InlineElement);
element_impl_from!(DecoratedText<'a>, InlineElement);
element_impl_from!(Keyword, InlineElement);
element_impl_from!(Link<'a>, InlineElement);
element_impl_from!(Tags<'a>, InlineElement);
element_impl_from!(CodeInline<'a>, InlineElement);
element_impl_from!(MathInline<'a>, InlineElement);

element_impl_from!(ListItem<'a>, InlineBlockElement);