1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! Markdown extensions defined by GitHub.
//!
//! The full specification can be found at https://github.github.com/gfm/.

use std::fmt;

/// A checkmark list item.
#[derive(Clone, Debug, Default)]
pub struct CheckmarkItem<'a> {
    /// The state of the item.
    ///
    /// `True` if the item is checked, `false` otherwise.
    pub checked: bool,
    /// The text of the checkmark item.
    pub text: &'a str,
}

impl<'a> CheckmarkItem<'a> {
    /// Creates a new default checkmark item.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a mew checkmark item with the given values.
    pub fn from(text: &'a str, checked: bool) -> Self {
        Self { text, checked }
    }
}

impl fmt::Display for CheckmarkItem<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let checkmark = if self.checked { "x" } else { " " };
        write!(f, "[{}] {}", checkmark, self.text)
    }
}

/// An extension trait for strikethrough transformations.
pub trait Strikethrough {
    /// Transforms the given text to be strikethrough.
    ///
    /// # Example
    ///
    /// ```rust
    /// use markdown_composer::extensions::github::Strikethrough;
    ///
    /// let text = "text";
    /// let striked = text.to_strikethrough();
    /// assert_eq!(striked, "~text~");
    /// ```
    fn to_strikethrough(&self) -> String;
}

impl<T> Strikethrough for T
where
    T: AsRef<str>,
{
    fn to_strikethrough(&self) -> String {
        format!("~{}~", self.as_ref())
    }
}