use crate::Paragraph;
#[derive(Debug, Clone, Default)]
pub struct Section {
pub heading: String,
pub level: i64,
pub content: Vec<Paragraph>,
}
impl Section {
pub fn nested(&self, n: i64) -> Section {
Section {
heading: self.heading.clone(),
level: self.level + n,
content: self.content.clone(),
}
}
pub fn is_empty(&self) -> bool {
self.heading.is_empty() && self.content.is_empty()
}
}
impl std::fmt::Display for Section {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{} {}", "#".repeat(self.level as usize), self.heading)?;
if self.content.is_empty() {
Ok(())
} else {
writeln!(f)?;
self.content
.iter()
.enumerate()
.try_for_each(|(i, paragraph)| {
if i > 0 {
writeln!(f)?;
}
write!(f, "{paragraph}")
})
}
}
}