use super::{InlineElement, InlineElementContainer, LE};
use derive_more::{
Constructor, Deref, DerefMut, From, Index, IndexMut, Into, IntoIterator,
};
use serde::{Deserialize, Serialize};
mod item;
pub use item::*;
#[derive(
Constructor, Clone, Debug, From, Eq, PartialEq, Serialize, Deserialize,
)]
pub struct List {
pub items: Vec<LE<ListItem>>,
}
impl List {
pub(crate) fn normalize(&mut self) -> &mut Self {
if let [head, tail @ ..] = &mut self.items[..] {
for item in tail {
item.item_type = head.item_type.clone();
}
}
self
}
}
#[derive(Clone, Debug, From, Eq, PartialEq, Serialize, Deserialize)]
pub enum ListItemContent {
InlineContent(InlineElementContainer),
List(List),
}
#[derive(
Constructor,
Clone,
Debug,
Default,
Deref,
DerefMut,
From,
Index,
IndexMut,
Into,
IntoIterator,
Eq,
PartialEq,
Serialize,
Deserialize,
)]
pub struct ListItemContents {
pub contents: Vec<LE<ListItemContent>>,
}
impl ListItemContents {
pub fn inline_content_iter(
&self,
) -> impl Iterator<Item = &InlineElement> + '_ {
self.contents
.iter()
.filter_map(|c| match &c.element {
ListItemContent::InlineContent(x) => {
Some(x.elements.iter().map(|y| &y.element))
}
_ => None,
})
.flatten()
}
pub fn inline_content_iter_mut(
&mut self,
) -> impl Iterator<Item = &mut InlineElement> + '_ {
self.contents
.iter_mut()
.filter_map(|c| match &mut c.element {
ListItemContent::InlineContent(x) => {
Some(x.elements.iter_mut().map(|y| &mut y.element))
}
_ => None,
})
.flatten()
}
pub fn sublist_iter(&self) -> impl Iterator<Item = &List> + '_ {
self.contents.iter().flat_map(|c| match &c.element {
ListItemContent::List(x) => Some(x),
_ => None,
})
}
pub fn sublist_iter_mut(&mut self) -> impl Iterator<Item = &mut List> + '_ {
self.contents.iter_mut().flat_map(|c| match &mut c.element {
ListItemContent::List(x) => Some(x),
_ => None,
})
}
}