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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/// A single comment from a `CommitMessage`
#[derive(Debug, PartialEq, Clone)]
pub struct Comment {
    comment: String,
}

impl Comment {
    /// Append one comment onto another
    ///
    /// This is for concatenating multiple comments together
    ///
    /// # Example
    ///
    /// ```
    /// use indoc::indoc;
    /// use mit_commit::Comment;
    ///
    /// assert_eq!(
    ///     Comment::from(indoc!(
    ///         "
    ///         Example 1
    ///         Example 2"
    ///     )),
    ///     Comment::from("Example 1").append(&Comment::from("Example 2"))
    /// )
    /// ```
    #[must_use]
    pub fn append(&self, additional: &Comment) -> Comment {
        Comment::from(format!("{}\n{}", self.comment, additional.comment))
    }
}

impl From<String> for Comment {
    fn from(comment: String) -> Self {
        Comment { comment }
    }
}

impl From<&str> for Comment {
    fn from(comment: &str) -> Self {
        Comment {
            comment: String::from(comment),
        }
    }
}

impl From<Comment> for String {
    fn from(comment: Comment) -> Self {
        comment.comment
    }
}

#[cfg(test)]
mod tests {
    use super::Comment;
    use indoc::indoc;
    use pretty_assertions::assert_eq;

    #[test]
    fn it_can_be_created_from_a_str() {
        let comment = Comment::from("# Example Comment");

        assert_eq!(String::from(comment), String::from("# Example Comment"))
    }

    #[test]
    fn it_can_be_created_from_a_string() {
        let comment = Comment::from(String::from("# Example Comment"));

        assert_eq!(String::from(comment), String::from("# Example Comment"))
    }

    #[test]
    fn it_can_append_another_comment_fragment() {
        assert_eq!(
            Comment::from(indoc!(
                "
            Example 1
            Example 2"
            )),
            Comment::from("Example 1").append(&Comment::from("Example 2"))
        )
    }
}