use std::slice::Iter;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Paragraph {
pub elements: Vec<ParagraphElement>,
}
impl Paragraph {
pub fn new() -> Self {
Default::default()
}
pub fn push<P>(&mut self, elem: P) -> &mut Self
where
P: Into<ParagraphElement>,
{
self.elements.push(elem.into());
self
}
pub fn push_text(&mut self, text: &str) -> &mut Self {
self.push(ParagraphElement::Plain(text.to_string()))
}
pub fn iter(&self) -> Iter<ParagraphElement> {
self.elements.iter()
}
}
impl<'a> From<&'a str> for Paragraph {
fn from(other: &'a str) -> Paragraph {
let mut para = Paragraph::new();
para.push_text(other);
para
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum ParagraphElement {
Plain(String),
Bold(Box<ParagraphElement>),
Italic(Box<ParagraphElement>),
InlineMath(String),
}
impl ParagraphElement {
pub fn italic<E>(elem: E) -> ParagraphElement
where
E: Into<ParagraphElement>,
{
ParagraphElement::Italic(Box::new(elem.into()))
}
pub fn bold<E>(elem: E) -> ParagraphElement
where
E: Into<ParagraphElement>,
{
ParagraphElement::Bold(Box::new(elem.into()))
}
}
impl<'a> From<&'a str> for ParagraphElement {
fn from(other: &'a str) -> Self {
ParagraphElement::Plain(other.to_string())
}
}