Skip to main content

mit_commit_message_lints/mit/lib/
author.rs

1use std::borrow::Cow;
2
3use serde::{Deserialize, Serialize};
4
5/// An author that might be developing
6#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
7pub struct Author<'a> {
8    name: Cow<'a, str>,
9    email: Cow<'a, str>,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    signingkey: Option<Cow<'a, str>>,
12}
13
14impl<'a> Author<'a> {
15    /// Create a new author
16    #[must_use]
17    pub const fn new(
18        name: Cow<'a, str>,
19        email: Cow<'a, str>,
20        signingkey: Option<Cow<'a, str>>,
21    ) -> Self {
22        Self {
23            name,
24            email,
25            signingkey,
26        }
27    }
28
29    /// The author name
30    #[must_use]
31    pub fn name(&self) -> &str {
32        &self.name
33    }
34
35    /// The authors email
36    #[must_use]
37    pub fn email(&self) -> &str {
38        &self.email
39    }
40
41    /// The authors gpg key
42    #[must_use]
43    pub fn signingkey(&self) -> Option<&str> {
44        self.signingkey.as_deref()
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    #![allow(clippy::wildcard_imports)]
51
52    use super::*;
53
54    #[test]
55    fn test_new_author_creation() {
56        let author = Author::new("The Name".into(), "email@example.com".into(), None);
57
58        assert_eq!(
59            author.name(),
60            "The Name",
61            "Expected the author's name to be 'The Name'"
62        );
63        assert_eq!(
64            author.email(),
65            "email@example.com",
66            "Expected the author's email to be 'email@example.com'"
67        );
68        assert_eq!(
69            author.signingkey(),
70            None,
71            "Expected the author's signing key to be None when not set"
72        );
73    }
74
75    #[test]
76    fn test_author_with_signing_key() {
77        let author = Author::new(
78            "The Name".into(),
79            "email@example.com".into(),
80            Some("0A46826A".into()),
81        );
82
83        assert_eq!(
84            author.signingkey(),
85            Some("0A46826A"),
86            "Expected the author's signing key to be '0A46826A'"
87        );
88    }
89}