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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use std::{
borrow::Cow,
fmt,
fmt::{Display, Formatter},
};
/// A single contiguous block of [`CommitMessage`] text
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Body<'a> {
text: Cow<'a, str>,
}
impl Body<'_> {
/// Append one [`Body`] onto another
///
/// # Arguments
///
/// * `additional` - The body to append to this one
///
/// # Returns
///
/// A new body with the content of both bodies separated by a newline
///
/// # Examples
///
/// ```
/// use indoc::indoc;
/// use mit_commit::Body;
///
/// assert_eq!(
/// Body::from(indoc!(
/// "
/// Example 1
/// Example 2"
/// )),
/// Body::from("Example 1").append(&Body::from("Example 2"))
/// )
/// ```
#[must_use]
pub fn append(&self, additional: &Self) -> Self {
Self::from(format!("{}\n{}", self.text, additional.text))
}
/// Checks if this [`Body`] is empty
///
/// # Returns
///
/// `true` if the body is empty, `false` otherwise
///
/// # Examples
///
/// ```
/// use mit_commit::Body;
///
/// assert_eq!(Body::from("").is_empty(), true);
/// assert_eq!(Body::from("not empty").is_empty(), false);
/// ```
#[must_use]
pub fn is_empty(&self) -> bool {
self.text.is_empty()
}
}
impl<'a> From<Cow<'a, str>> for Body<'a> {
/// Create a Body from a Cow<_, str>
///
/// # Arguments
///
/// * `body` - The string content to create the body from
///
/// # Returns
///
/// A new Body containing the provided string
///
/// # Examples
///
/// ```
/// use std::borrow::Cow;
///
/// use mit_commit::Body;
///
/// let expected = "a string";
/// let input = Cow::from(expected);
/// assert_eq!(Body::from(input).to_string(), expected)
/// ```
fn from(body: Cow<'a, str>) -> Self {
Self { text: body }
}
}
impl<'a> From<&'a str> for Body<'a> {
/// Create a Body from a string slice
///
/// # Arguments
///
/// * `body` - The string slice to create the body from
///
/// # Returns
///
/// A new Body containing the provided string
fn from(body: &'a str) -> Self {
Self::from(Cow::Borrowed(body))
}
}
impl From<String> for Body<'_> {
/// Create a Body from a String
///
/// # Arguments
///
/// * `body` - The string to create the body from
///
/// # Returns
///
/// A new Body containing the provided string
fn from(body: String) -> Self {
Self::from(Cow::from(body))
}
}
impl From<Body<'_>> for String {
/// Convert a Body to a String
///
/// # Arguments
///
/// * `body` - The body to convert
///
/// # Returns
///
/// A String containing the body's text
fn from(body: Body<'_>) -> Self {
body.text.into()
}
}
impl<'a> From<Body<'a>> for Cow<'a, str> {
/// Convert a Body to a Cow<_, str>
///
/// # Arguments
///
/// * `body` - The body to convert
///
/// # Returns
///
/// A Cow<_, str> containing the body's text
///
/// # Examples
///
/// ```
/// use std::borrow::Cow;
///
/// use mit_commit::Body;
///
/// let expected = Cow::from("a string");
/// let input = Body::from(expected.clone());
/// assert_eq!(Cow::from(input), expected)
/// ```
fn from(body: Body<'a>) -> Self {
body.text
}
}
impl Display for Body<'_> {
/// Format the Body for display
///
/// # Arguments
///
/// * `f` - The formatter to write to
///
/// # Returns
///
/// A Result indicating whether the operation succeeded
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", String::from(self.clone()))
}
}
#[cfg(test)]
mod tests {
use indoc::indoc;
use super::*;
#[test]
fn test_string_conversion_from_str() {
let body = Body::from("Example Body");
assert_eq!(
String::from(body),
String::from("Example Body"),
"Body should convert to the correct string when created from a str"
);
}
#[test]
fn test_string_conversion_from_string() {
let body = Body::from(String::from("Example Body"));
assert_eq!(
String::from(body),
String::from("Example Body"),
"Body should convert to the correct string when created from a String"
);
}
#[test]
fn test_display_implementation() {
let body = Body::from("Example Body");
assert_eq!(
format!("{body}"),
"Example Body",
"Display implementation should format the body correctly"
);
}
#[test]
fn test_append_body_fragments() {
assert_eq!(
Body::from(indoc!(
"
Example 1
Example 2"
)),
Body::from("Example 1").append(&Body::from("Example 2")),
"Appending bodies should create a new body with content separated by newline"
);
}
#[test]
fn test_is_empty_with_empty_body() {
assert!(
Body::from("").is_empty(),
"Empty body should be identified as empty"
);
}
#[test]
fn test_is_empty_with_non_empty_body() {
assert!(
!Body::from("something").is_empty(),
"Non-empty body should not be identified as empty"
);
}
}