markdown_builder/types/
checkmark.rs

1use std::fmt;
2
3/// A checkmark list item.
4#[derive(Clone, Debug, Default)]
5pub struct CheckmarkItem {
6    /// The state of the item.
7    ///
8    /// `true` if the item is checked, `false` otherwise.
9    pub checked: bool,
10    /// The text of the checkmark item.
11    pub text: String,
12}
13
14impl CheckmarkItem {
15    /// Creates a new default checkmark item.
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Creates a mew checkmark item with the given values.
21    pub fn from(text: impl Into<String>, checked: bool) -> Self {
22        Self {
23            text: text.into(),
24            checked,
25        }
26    }
27}
28
29impl fmt::Display for CheckmarkItem {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        let checkmark = if self.checked { "x" } else { " " };
32        write!(f, "[{}] {}", checkmark, self.text)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::MarkdownElement;
40
41    #[test]
42    fn test_display() {
43        assert_eq!(
44            CheckmarkItem::from("Eat spaghetti", false).render(),
45            "[ ] Eat spaghetti"
46        );
47        assert_eq!(
48            CheckmarkItem::from("Eat spaghetti", true).render(),
49            "[x] Eat spaghetti"
50        );
51    }
52}