markdown_builder/builders/
list.rs

1use crate::types::{
2    checkmark::CheckmarkItem,
3    list::{List, ListItem},
4};
5
6#[derive(Clone, Debug, Default)]
7pub struct ListBuilder {
8    items: Vec<ListItem>,
9}
10
11impl ListBuilder {
12    pub fn new() -> Self {
13        Self::default()
14    }
15
16    pub fn append(mut self, item: impl Into<ListItem>) -> Self {
17        self.items.push(item.into());
18        self
19    }
20
21    /// Adds a checkmark using checkmark::CheckmarkItem.
22    pub fn checkmark(mut self, item: impl Into<String>, checked: bool) -> Self {
23        self.items.push(CheckmarkItem::from(item, checked).into());
24        self
25    }
26
27    pub fn ordered(self) -> List {
28        List::ordered_with(self.items)
29    }
30
31    pub fn unordered(self) -> List {
32        List::unordered_with(self.items)
33    }
34}
35
36impl List {
37    pub fn builder() -> ListBuilder {
38        ListBuilder::new()
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::{MarkdownElement, Paragraph};
46
47    #[test]
48    fn test_ordered_paragraphs() {
49        let list = List::builder()
50            .append(Paragraph::from("Hello World"))
51            .append(Paragraph::from("John doe"))
52            .ordered();
53
54        assert_eq!(list.render(), "1. Hello World\n2. John doe\n");
55    }
56
57    #[test]
58    fn test_unordered_text() {
59        let list = List::builder()
60            .append("Hello World")
61            .append("John doe")
62            .unordered();
63
64        assert_eq!(list.render(), "- Hello World\n- John doe\n");
65    }
66
67    #[test]
68    fn test_unordered_checkmarks() {
69        let list = List::builder()
70            .checkmark("Eat spaghetti", true)
71            .checkmark("Eat pizza", false)
72            .checkmark("Eat kebab", true)
73            .unordered();
74
75        assert_eq!(
76            list.render(),
77            "- [x] Eat spaghetti\n- [ ] Eat pizza\n- [x] Eat kebab\n"
78        );
79    }
80}