rstdev_domain/
entity.rs

1use rst_common::standard::chrono::Timelike;
2
3/// `UID` is a value object used as unique identity that must have by each of identity
4///
5/// This trait using associated type of [`UID::Value`] to fill any possible types. Usually
6/// it will either a string (`UUID`) or an integer (`AUTO_INCREMENT`), but it also be able
7/// to used an id like `MongoDB ID Object Hash`
8pub trait UID {
9    type Value;
10
11    fn uid(&self) -> Self::Value;
12}
13
14pub trait Timestamp {
15    type CreatedAt: Timelike;
16    type UpdatedAt: Timelike;
17
18    fn created_at(&self) -> Self::CreatedAt;
19    fn updated_at(&self) -> Self::UpdatedAt;
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25    use rst_common::standard::chrono::{DateTime, Utc};
26
27    struct FakeTimestamp {
28        created_at: DateTime<Utc>,
29        updated_at: DateTime<Utc>
30    }
31
32    impl Default for FakeTimestamp {
33        fn default() -> Self {
34            Self { created_at: Utc::now(), updated_at: Utc::now() }
35        }
36    }
37
38    impl Timestamp for FakeTimestamp {
39        type CreatedAt = DateTime<Utc>;
40        type UpdatedAt = DateTime<Utc>;
41
42        fn created_at(&self) -> Self::CreatedAt {
43            self.created_at
44        }
45
46        fn updated_at(&self) -> Self::UpdatedAt {
47            self.updated_at
48        }
49    }
50
51    #[test]
52    fn test_timestamp() {
53        let fts = FakeTimestamp::default();
54        assert!(!fts.created_at().to_string().is_empty());
55        assert!(!fts.updated_at().to_string().is_empty())
56    }
57}