Skip to main content

gitfox_client/models/
commit.rs

1use serde::{Deserialize, Serialize};
2
3/// A commit, as GitFox reports it in commit and pull request listings.
4#[derive(Debug, Clone, Default, Serialize, Deserialize)]
5pub struct Commit {
6    #[serde(default)]
7    pub sha: String,
8    /// The subject line.
9    #[serde(default)]
10    pub title: String,
11    /// The whole message, subject included.
12    #[serde(default)]
13    pub message: String,
14    #[serde(default)]
15    pub parent_shas: Vec<String>,
16    #[serde(default)]
17    pub author: Option<Signature>,
18    #[serde(default)]
19    pub committer: Option<Signature>,
20}
21
22impl Commit {
23    /// The message with its subject line removed.
24    pub fn body(&self) -> String {
25        match self.message.split_once('\n') {
26            Some((_, rest)) => rest.trim().to_string(),
27            None => String::new(),
28        }
29    }
30}
31
32#[derive(Debug, Clone, Default, Serialize, Deserialize)]
33pub struct Signature {
34    #[serde(default)]
35    pub identity: Identity,
36    /// RFC 3339, as GitFox sends it.
37    #[serde(default)]
38    pub when: Option<String>,
39}
40
41#[derive(Debug, Clone, Default, Serialize, Deserialize)]
42pub struct Identity {
43    #[serde(default)]
44    pub name: String,
45    #[serde(default)]
46    pub email: String,
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn the_body_is_the_message_without_its_subject() {
55        let commit = Commit {
56            message: "feat: add OAuth\n\nCloses #4\n".into(),
57            ..Default::default()
58        };
59        assert_eq!(commit.body(), "Closes #4");
60        let subject_only = Commit {
61            message: "fix: typo".into(),
62            ..Default::default()
63        };
64        assert_eq!(subject_only.body(), "");
65    }
66}