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
use crate::{Body, Comment};
/// A `Fragment` from the [`CommitMessage`], either a comment or body
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Fragment<'a> {
/// A fragment that is going to appear in the git log
Body(Body<'a>),
/// A fragment that is a comment
Comment(Comment<'a>),
}
impl<'a> From<Body<'a>> for Fragment<'a> {
/// Create a Fragment from a Body
///
/// # Arguments
///
/// * `body` - The body to convert into a fragment
///
/// # Returns
///
/// A new `Fragment::Body` variant containing the provided body
///
/// # Examples
///
/// ```
/// use mit_commit::{Body, Fragment};
///
/// let body = Body::from("Example body");
/// let fragment = Fragment::from(body.clone());
/// assert_eq!(fragment, Fragment::Body(body));
/// ```
fn from(body: Body<'a>) -> Self {
Self::Body(body)
}
}
impl<'a> From<Comment<'a>> for Fragment<'a> {
/// Create a Fragment from a Comment
///
/// # Arguments
///
/// * `comment` - The comment to convert into a fragment
///
/// # Returns
///
/// A new `Fragment::Comment` variant containing the provided comment
///
/// # Examples
///
/// ```
/// use mit_commit::{Comment, Fragment};
///
/// let comment = Comment::from("# Example comment");
/// let fragment = Fragment::from(comment.clone());
/// assert_eq!(fragment, Fragment::Comment(comment));
/// ```
fn from(comment: Comment<'a>) -> Self {
Self::Comment(comment)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_body_conversion_to_fragment() {
let body: Body<'_> = "A Body".into();
let fragment: Fragment<'_> = body.clone().into();
assert_eq!(
fragment,
Fragment::Body(body),
"Converting a Body to a Fragment should create a Fragment::Body variant with the same content"
);
}
#[test]
fn test_comment_conversion_to_fragment() {
let comment: Comment<'_> = "A Comment".into();
let fragment: Fragment<'_> = comment.clone().into();
assert_eq!(
fragment,
Fragment::Comment(comment),
"Converting a Comment to a Fragment should create a Fragment::Comment variant with the same content"
);
}
}