use std::fmt::Display;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::YamdNodes;
#[derive(Debug, PartialEq, Clone, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Collapsible {
pub title: String,
pub body: Vec<YamdNodes>,
}
impl Collapsible {
pub fn new<S: Into<String>>(title: S, body: Vec<YamdNodes>) -> Self {
Self {
body,
title: title.into(),
}
}
}
impl Display for Collapsible {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{% {}\n{}\n%}}",
self.title,
self.body
.iter()
.map(|node| node.to_string())
.collect::<Vec<_>>()
.join("\n\n")
)
}
}
#[cfg(test)]
mod tests {
use crate::nodes::Paragraph;
use super::*;
#[test]
fn test_collapsible() {
let collapsible = Collapsible::new(
"Collapsible title",
vec![YamdNodes::Paragraph(Paragraph::new(vec![
"Collapsible body".to_string().into(),
]))],
);
assert_eq!(
collapsible.to_string(),
"{% Collapsible title\nCollapsible body\n%}"
);
}
}