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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::borrow::Cow;
const LEGAL_CHARACTERS: [char; 10] = ['#', ';', '@', '!', '$', '%', '^', '&', '|', ':'];
/// A single comment from a `CommitMessage`
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Comment<'a> {
comment: Cow<'a, str>,
}
impl Comment<'_> {
/// Append one [`Comment`] onto another
///
/// # Arguments
///
/// * `additional` - The comment to append to this one
///
/// # Returns
///
/// A new comment with the content of both comments separated by a newline
///
/// # Examples
///
/// ```
/// 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: &Self) -> Self {
Self::from(format!("{}\n{}", self.comment, additional.comment))
}
/// Checks if a given character is a valid comment character
///
/// # Arguments
///
/// * `character` - The character to check
///
/// # Returns
///
/// `true` if the character is a valid comment character, `false` otherwise
///
/// # Examples
///
/// ```
/// use mit_commit::Comment;
///
/// assert!(!Comment::is_legal_comment_char('?'));
/// assert!(Comment::is_legal_comment_char('#'));
/// ```
#[must_use]
pub fn is_legal_comment_char(character: char) -> bool {
LEGAL_CHARACTERS.contains(&character)
}
}
impl<'a> From<Cow<'a, str>> for Comment<'a> {
/// Create a Comment from a Cow<_, str>
///
/// # Arguments
///
/// * `comment` - The string content to create the comment from
///
/// # Returns
///
/// A new Comment containing the provided string
fn from(comment: Cow<'a, str>) -> Self {
Self { comment }
}
}
impl From<String> for Comment<'_> {
/// Create a Comment from a String
///
/// # Arguments
///
/// * `comment` - The string to create the comment from
///
/// # Returns
///
/// A new Comment containing the provided string
fn from(comment: String) -> Self {
Self {
comment: comment.into(),
}
}
}
impl<'a> From<&'a str> for Comment<'a> {
/// Create a Comment from a string slice
///
/// # Arguments
///
/// * `comment` - The string slice to create the comment from
///
/// # Returns
///
/// A new Comment containing the provided string
fn from(comment: &'a str) -> Self {
Self {
comment: comment.into(),
}
}
}
impl<'a> From<Comment<'a>> for String {
/// Convert a Comment to a String
///
/// # Arguments
///
/// * `comment` - The comment to convert
///
/// # Returns
///
/// A String containing the comment's text
fn from(comment: Comment<'a>) -> Self {
comment.comment.into()
}
}
#[cfg(test)]
mod tests {
use indoc::indoc;
use super::*;
#[test]
fn test_creation_from_str() {
let comment = Comment::from("# Example Comment");
assert_eq!(
String::from(comment),
String::from("# Example Comment"),
"Comment should convert to the correct string when created from a str"
);
}
#[test]
fn test_creation_from_string() {
let comment = Comment::from(String::from("# Example Comment"));
assert_eq!(
String::from(comment),
String::from("# Example Comment"),
"Comment should convert to the correct string when created from a String"
);
}
#[test]
fn test_legal_comment_char_detection() {
assert!(
Comment::is_legal_comment_char('#'),
"# should be recognized as a legal comment character"
);
}
#[test]
fn test_illegal_comment_char_detection() {
assert!(
!Comment::is_legal_comment_char('?'),
"? should not be recognized as a legal comment character"
);
}
#[test]
fn test_append_comment_fragments() {
assert_eq!(
Comment::from(indoc!(
"
Example 1
Example 2"
)),
Comment::from("Example 1").append(&Comment::from("Example 2")),
"Appending comments should create a new comment with content separated by newline"
);
}
}