mit_commit/fragment.rs
1use crate::{Body, Comment};
2
3/// A `Fragment` from the [`CommitMessage`], either a comment or body
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub enum Fragment<'a> {
6 /// A fragment that is going to appear in the git log
7 Body(Body<'a>),
8 /// A fragment that is a comment
9 Comment(Comment<'a>),
10}
11
12impl<'a> From<Body<'a>> for Fragment<'a> {
13 /// Create a Fragment from a Body
14 ///
15 /// # Arguments
16 ///
17 /// * `body` - The body to convert into a fragment
18 ///
19 /// # Returns
20 ///
21 /// A new `Fragment::Body` variant containing the provided body
22 ///
23 /// # Examples
24 ///
25 /// ```
26 /// use mit_commit::{Body, Fragment};
27 ///
28 /// let body = Body::from("Example body");
29 /// let fragment = Fragment::from(body.clone());
30 /// assert_eq!(fragment, Fragment::Body(body));
31 /// ```
32 fn from(body: Body<'a>) -> Self {
33 Self::Body(body)
34 }
35}
36
37impl<'a> From<Comment<'a>> for Fragment<'a> {
38 /// Create a Fragment from a Comment
39 ///
40 /// # Arguments
41 ///
42 /// * `comment` - The comment to convert into a fragment
43 ///
44 /// # Returns
45 ///
46 /// A new `Fragment::Comment` variant containing the provided comment
47 ///
48 /// # Examples
49 ///
50 /// ```
51 /// use mit_commit::{Comment, Fragment};
52 ///
53 /// let comment = Comment::from("# Example comment");
54 /// let fragment = Fragment::from(comment.clone());
55 /// assert_eq!(fragment, Fragment::Comment(comment));
56 /// ```
57 fn from(comment: Comment<'a>) -> Self {
58 Self::Comment(comment)
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_body_conversion_to_fragment() {
68 let body: Body<'_> = "A Body".into();
69 let fragment: Fragment<'_> = body.clone().into();
70
71 assert_eq!(
72 fragment,
73 Fragment::Body(body),
74 "Converting a Body to a Fragment should create a Fragment::Body variant with the same content"
75 );
76 }
77
78 #[test]
79 fn test_comment_conversion_to_fragment() {
80 let comment: Comment<'_> = "A Comment".into();
81 let fragment: Fragment<'_> = comment.clone().into();
82
83 assert_eq!(
84 fragment,
85 Fragment::Comment(comment),
86 "Converting a Comment to a Fragment should create a Fragment::Comment variant with the same content"
87 );
88 }
89}