mit_commit/comment.rs
1use std::borrow::Cow;
2
3const LEGAL_CHARACTERS: [char; 10] = ['#', ';', '@', '!', '$', '%', '^', '&', '|', ':'];
4
5/// A single comment from a `CommitMessage`
6#[derive(Debug, PartialEq, Eq, Clone)]
7pub struct Comment<'a> {
8 comment: Cow<'a, str>,
9}
10
11impl Comment<'_> {
12 /// Append one [`Comment`] onto another
13 ///
14 /// This is for concatenating multiple [`Comment`] together
15 ///
16 /// # Example
17 ///
18 /// ```
19 /// use indoc::indoc;
20 /// use mit_commit::Comment;
21 ///
22 /// assert_eq!(
23 /// Comment::from(indoc!(
24 /// "
25 /// Example 1
26 /// Example 2"
27 /// )),
28 /// Comment::from("Example 1").append(&Comment::from("Example 2"))
29 /// )
30 /// ```
31 #[must_use]
32 pub fn append(&self, additional: &Self) -> Self {
33 Self::from(format!("{}\n{}", self.comment, additional.comment))
34 }
35
36 /// Tells you if a given comment character is a potential comment character
37 ///
38 /// # Example
39 ///
40 /// ```
41 /// use mit_commit::Comment;
42 ///
43 /// assert!(!Comment::is_legal_comment_char('?'),);
44 /// assert!(Comment::is_legal_comment_char('#'),);
45 /// ```
46 #[must_use]
47 pub fn is_legal_comment_char(character: char) -> bool {
48 LEGAL_CHARACTERS.contains(&character)
49 }
50}
51
52impl<'a> From<Cow<'a, str>> for Comment<'a> {
53 fn from(comment: Cow<'a, str>) -> Self {
54 Self { comment }
55 }
56}
57
58impl From<String> for Comment<'_> {
59 fn from(comment: String) -> Self {
60 Self {
61 comment: comment.into(),
62 }
63 }
64}
65
66impl<'a> From<&'a str> for Comment<'a> {
67 fn from(comment: &'a str) -> Self {
68 Self {
69 comment: comment.into(),
70 }
71 }
72}
73
74impl<'a> From<Comment<'a>> for String {
75 fn from(comment: Comment<'a>) -> Self {
76 comment.comment.into()
77 }
78}