1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! [`Created`] is an special event which starts a new `RedMaple` and should be the first event of each
//! `RedMaple`.

use std::time::SystemTime;

use redmaple::id::ID;
use redmaple::view_mode::ViewMode;

/// Creates a new instance of Story
///
/// * `id`: is of type ID.
/// * `redmaple_id`: is of type ID.
#[derive(Clone, Debug)]
pub struct Created<V: ViewMode + Sized + Clone> {
    id: ID,
    created: SystemTime,
    redmaple_id: ID,
    view_mode: V,
}

impl<V: ViewMode + Sized + Clone> Created<V> {
    /// Creates a new [`Created`] event
    ///
    /// * `view_mode`: set the view mode for this `RedMaple` `ViewMode`
    /// * `redmaple_id`: set the id of the the parent redmaple
    #[must_use]
    pub fn new(view_mode: V, redmaple_id: ID) -> Self {
        Self {
            id: ID::new(),
            created: std::time::SystemTime::now(),
            redmaple_id,
            view_mode,
        }
    }

    /// returns the view mode of the parent redmaple
    pub const fn view_mode(&self) -> &V {
        &self.view_mode
    }

    /// returns the id of event
    #[must_use]
    pub const fn id(&self) -> &ID {
        &self.id
    }

    /// returns the id of the parent redmaple
    #[must_use]
    pub const fn redmaple_id(&self) -> &ID {
        &self.redmaple_id
    }

    // pub fn apply(
    //     &self,
    //     store: &dyn crate::store::StateStorage<>,
    // ) -> Result<(), crate::store::ApplyError> {
    //     store.apply(self)
    // }

    /// returns the creation time of event
    #[must_use]
    pub const fn created(&self) -> &SystemTime {
        &self.created
    }
}

#[cfg(test)]
mod tests {
    // use redmaple::view_mode::BlogMode;

    use redmaple::id::ID;

    use crate::argument::{
        maple_created::Created,
        views::{BlogMode, Views},
    };

    #[test]
    fn could_make_event() {
        let red_maple_id = ID::new();
        let new_event = Created::new(Views::Blog(BlogMode::Text), red_maple_id.clone());

        assert_eq!(new_event.redmaple_id(), &red_maple_id);
        assert_eq!(
            new_event.id().into_inner().to_string().len(),
            red_maple_id.into_inner().to_string().len()
        );
    }
}