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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use crate::{
    lang::elements::{
        AsChildrenMutSlice, AsChildrenSlice, Element, InlineBlockElement,
        InlineElement, InlineElementContainer, IntoChildren, Located,
    },
    StrictEq,
};
use derive_more::{
    AsRef, Constructor, Deref, DerefMut, From, Index, IndexMut, Into,
    IntoIterator,
};
use serde::{Deserialize, Serialize};
use std::iter::FromIterator;

mod item;
pub use item::*;

/// Represents a regular list comprised of individual items
#[derive(
    Constructor,
    Clone,
    Debug,
    From,
    Eq,
    PartialEq,
    Index,
    IndexMut,
    IntoIterator,
    Serialize,
    Deserialize,
)]
pub struct List<'a> {
    /// Represents items contained within the list
    #[index]
    #[index_mut]
    #[into_iterator(owned, ref, ref_mut)]
    pub items: Vec<Located<ListItem<'a>>>,
}

impl List<'_> {
    pub fn to_borrowed(&self) -> List {
        self.into_iter()
            .map(|x| x.as_ref().map(ListItem::to_borrowed))
            .collect()
    }

    pub fn into_owned(self) -> List<'static> {
        self.into_iter()
            .map(|x| x.map(ListItem::into_owned))
            .collect()
    }
}

impl<'a> List<'a> {
    /// Returns iterator of references to list items
    pub fn iter(&self) -> impl Iterator<Item = &Located<ListItem<'a>>> {
        self.into_iter()
    }

    /// Returns iterator of mutable references to list items
    pub fn iter_mut(
        &mut self,
    ) -> impl Iterator<Item = &mut Located<ListItem<'a>>> {
        self.into_iter()
    }

    /// Returns total items contained in list
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Returns true if list has no items
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Returns whether or not the list represents an ordered list based on
    /// the first list item; if there are no items then this would return false
    pub fn is_ordered(&self) -> bool {
        self.iter()
            .next()
            .map_or(false, |item| item.ty.is_ordered())
    }

    /// Returns whether or not the list represents an unordered list based on
    /// the first list item; if there are no items then this would return false
    pub fn is_unordered(&self) -> bool {
        self.iter()
            .next()
            .map_or(false, |item| item.ty.is_unordered())
    }

    /// Normalizes the list by standardizing the item types based on the
    /// first list item.
    ///
    /// For example, if you have the following list:
    ///
    /// 1. Hyphen
    /// 2. Number
    /// 3. Asterisk
    ///
    /// You would get back out the following list:
    ///
    /// 1. Hyphen
    /// 2. Hyphen
    /// 3. Hyphen
    ///
    /// Note that this does NOT normalize sublists, which should be done
    /// manually.
    pub(crate) fn normalize(&mut self) -> &mut Self {
        // If we have items, we want to go through and normalize their types
        if let [head, tail @ ..] = &mut self.items[..] {
            // TODO: Need to support special case where not all item types are
            //       roman numeral but the first one is, as this can happen with
            //       alphabetic lists if for some reason starting with i and moving
            //       on to other letters like j and k
            for item in tail {
                item.ty = head.ty.clone();
            }
        }

        self
    }
}

impl<'a> AsChildrenSlice for List<'a> {
    type Child = Located<ListItem<'a>>;

    fn as_children_slice(&self) -> &[Self::Child] {
        &self.items
    }
}

impl<'a> AsChildrenMutSlice for List<'a> {
    type Child = Located<ListItem<'a>>;

    fn as_children_mut_slice(&mut self) -> &mut [Self::Child] {
        &mut self.items
    }
}

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

    fn into_children(self) -> Vec<Self::Child> {
        self.into_iter()
            .map(|x| x.map(InlineBlockElement::from))
            .collect()
    }
}

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

impl<'a> StrictEq for List<'a> {
    /// Performs a strict_eq check against list items
    fn strict_eq(&self, other: &Self) -> bool {
        self.items.strict_eq(&other.items)
    }
}

/// Represents some content associated with a list item, either being
/// an inline element or a new sublist
#[derive(Clone, Debug, From, Eq, PartialEq, Serialize, Deserialize)]
pub enum ListItemContent<'a> {
    InlineContent(InlineElementContainer<'a>),
    List(List<'a>),
}

impl ListItemContent<'_> {
    pub fn to_borrowed(&self) -> ListItemContent {
        match self {
            Self::InlineContent(ref x) => {
                ListItemContent::from(x.to_borrowed())
            }
            Self::List(ref x) => ListItemContent::from(x.to_borrowed()),
        }
    }

    pub fn into_owned(self) -> ListItemContent<'static> {
        match self {
            Self::InlineContent(x) => ListItemContent::from(x.into_owned()),
            Self::List(x) => ListItemContent::from(x.into_owned()),
        }
    }
}

impl<'a> StrictEq for ListItemContent<'a> {
    /// Performs a strict_eq check against eqivalent variants
    fn strict_eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::InlineContent(x), Self::InlineContent(y)) => x.strict_eq(y),
            (Self::List(x), Self::List(y)) => x.strict_eq(y),
            _ => false,
        }
    }
}

/// Represents a collection of list item content
#[derive(
    AsRef,
    Constructor,
    Clone,
    Debug,
    Default,
    Deref,
    DerefMut,
    Index,
    IndexMut,
    Into,
    IntoIterator,
    Eq,
    PartialEq,
    Serialize,
    Deserialize,
)]
#[as_ref(forward)]
#[into_iterator(owned, ref, ref_mut)]
pub struct ListItemContents<'a>(Vec<Located<ListItemContent<'a>>>);

impl ListItemContents<'_> {
    pub fn to_borrowed(&self) -> ListItemContents {
        self.iter()
            .map(|x| x.as_ref().map(ListItemContent::to_borrowed))
            .collect()
    }

    pub fn into_owned(self) -> ListItemContents<'static> {
        self.into_iter()
            .map(|x| x.map(ListItemContent::into_owned))
            .collect()
    }
}

impl<'a> ListItemContents<'a> {
    pub fn inline_content_iter(
        &self,
    ) -> impl Iterator<Item = &InlineElement> + '_ {
        self.iter()
            .filter_map(|c| match c.as_inner() {
                ListItemContent::InlineContent(x) => {
                    Some(x.iter().map(|y| y.as_inner()))
                }
                _ => None,
            })
            .flatten()
    }

    pub fn inline_content_iter_mut(
        &mut self,
    ) -> impl Iterator<Item = &mut InlineElement<'a>> + '_ {
        self.iter_mut()
            .filter_map(|c| match c.as_mut_inner() {
                ListItemContent::InlineContent(x) => {
                    Some(x.iter_mut().map(|y| y.as_mut_inner()))
                }
                _ => None,
            })
            .flatten()
    }

    pub fn sublist_iter(&self) -> impl Iterator<Item = &List> + '_ {
        self.iter().flat_map(|c| match c.as_inner() {
            ListItemContent::List(x) => Some(x),
            _ => None,
        })
    }

    pub fn sublist_iter_mut(
        &mut self,
    ) -> impl Iterator<Item = &mut List<'a>> + '_ {
        self.iter_mut().flat_map(|c| match c.as_mut_inner() {
            ListItemContent::List(x) => Some(x),
            _ => None,
        })
    }
}

impl<'a> AsChildrenSlice for ListItemContents<'a> {
    type Child = Located<ListItemContent<'a>>;

    fn as_children_slice(&self) -> &[Self::Child] {
        &self.0
    }
}

impl<'a> AsChildrenMutSlice for ListItemContents<'a> {
    type Child = Located<ListItemContent<'a>>;

    fn as_children_mut_slice(&mut self) -> &mut [Self::Child] {
        &mut self.0
    }
}

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

    fn into_children(self) -> Vec<Self::Child> {
        self.into_iter()
            .flat_map(|x| {
                let region = x.region();
                match x.into_inner() {
                    ListItemContent::InlineContent(content) => content
                        .into_children()
                        .into_iter()
                        .map(|x| x.map(Element::from))
                        .collect(),
                    ListItemContent::List(list) => {
                        vec![Located::new(Element::from(list), region)]
                    }
                }
            })
            .collect()
    }
}

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

impl<'a> StrictEq for ListItemContents<'a> {
    /// Performs a strict_eq check against inner contents
    fn strict_eq(&self, other: &Self) -> bool {
        self.0.strict_eq(&other.0)
    }
}