Skip to main content

stratum/domain/
actor.rs

1use chrono::{DateTime, Utc};
2use git2::Signature;
3
4/// A git actor who exists for the inspected repository
5pub struct Actor {
6    inner: Signature<'static>,
7}
8
9impl Actor {
10    /// Instantiate a new Actor from their signature
11    pub fn new(signature: Signature<'_>) -> Self {
12        Self {
13            inner: signature.to_owned(),
14        }
15    }
16
17    /// Return the actors name if it exists
18    pub fn name(&self) -> Option<String> {
19        self.inner.name().map(|s| s.to_string())
20    }
21
22    /// Return the actors email if it exists
23    pub fn email(&self) -> Option<String> {
24        self.inner.email().map(|s| s.to_string())
25    }
26
27    /// Return the timestamp of actor action if it exists
28    pub fn timestamp(&self) -> Option<DateTime<Utc>> {
29        DateTime::from_timestamp_secs(self.inner.when().seconds())
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn test_actor() {
39        let sig = Signature::new(
40            "test",
41            "test@example.com",
42            &git2::Time::new(1_600_000_000, 0),
43        )
44        .unwrap();
45
46        let actor = Actor::new(sig);
47
48        assert_eq!(actor.name(), Some("test".to_string()));
49        assert_eq!(actor.email(), Some("test@example.com".to_string()));
50        assert_eq!(actor.timestamp().unwrap().timestamp(), 1_600_000_000);
51    }
52}