noob_commit/
lib.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Deserialize, JsonSchema, Serialize)]
5pub struct Commit {
6    /// The title of the commit.
7    pub title: String,
8    /// An exhaustive description of the changes.
9    pub description: String,
10}
11
12#[derive(Debug, Deserialize, JsonSchema, Serialize)]
13pub struct CommitAdvice {
14    /// Friendly message to the noob developer.
15    pub message: String,
16    /// The actual commit information.
17    pub commit: Commit,
18}
19
20impl ToString for Commit {
21    fn to_string(&self) -> String {
22        format!("{}\n\n{}", self.title, self.description)
23    }
24}
25
26impl ToString for CommitAdvice {
27    fn to_string(&self) -> String {
28        format!("{}\n\n{}", self.message, self.commit.to_string())
29    }
30}
31
32impl Commit {
33    pub fn new(title: String, description: String) -> Self {
34        Self { title, description }
35    }
36}
37
38impl CommitAdvice {
39    pub fn new(message: String, commit: Commit) -> Self {
40        Self { message, commit }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_commit_creation() {
50        let commit = Commit::new(
51            "Add noob-friendly features".to_string(),
52            "Added self-deprecating humor and alias setup functionality for developers who are great at coding but terrible at git.".to_string(),
53        );
54
55        assert_eq!(commit.title, "Add noob-friendly features");
56        assert!(commit.description.contains("self-deprecating"));
57    }
58
59    #[test]
60    fn test_commit_to_string() {
61        let commit = Commit::new("Fix stuff".to_string(), "idk it works now".to_string());
62
63        let result = commit.to_string();
64        assert_eq!(result, "Fix stuff\n\nidk it works now");
65    }
66
67    #[test]
68    fn test_commit_with_empty_description() {
69        let commit = Commit::new("Update README".to_string(), "".to_string());
70
71        let result = commit.to_string();
72        assert_eq!(result, "Update README\n\n");
73    }
74
75    #[test]
76    fn test_commit_with_multiline_description() {
77        let commit = Commit::new(
78            "Refactor code".to_string(),
79            "Line 1\nLine 2\nLine 3".to_string(),
80        );
81
82        let result = commit.to_string();
83        assert_eq!(result, "Refactor code\n\nLine 1\nLine 2\nLine 3");
84    }
85
86    #[test]
87    fn test_commit_serialization() {
88        let commit = Commit::new("Test commit".to_string(), "This is a test".to_string());
89
90        let json = serde_json::to_string(&commit).unwrap();
91        assert!(json.contains("Test commit"));
92        assert!(json.contains("This is a test"));
93    }
94
95    #[test]
96    fn test_commit_deserialization() {
97        let json = r#"{"title":"Test commit","description":"This is a test"}"#;
98        let commit: Commit = serde_json::from_str(json).unwrap();
99
100        assert_eq!(commit.title, "Test commit");
101        assert_eq!(commit.description, "This is a test");
102    }
103
104    #[test]
105    fn test_commit_advice_to_string() {
106        let commit = Commit::new("Init".to_string(), "First commit".to_string());
107        let advice = CommitAdvice::new("Be careful".to_string(), commit);
108        let result = advice.to_string();
109        assert!(result.contains("Be careful"));
110        assert!(result.contains("Init"));
111        assert!(result.contains("First commit"));
112    }
113}