summaryrefslogtreecommitdiff
path: root/src/user/history.rs
blob: 7c06a2d13859d698347bd2e2a795ca04e6ff7185 (plain)
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
use super::{
    User,
    event::{Created, Event},
};
use crate::{
    event::{Instant, Sequence},
    login::Login,
};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct History {
    pub user: User,
    pub created: Instant,
}

// Lifecycle interface
impl History {
    pub fn begin(login: &Login, created: Instant) -> Self {
        let Login { id, name } = login.clone();

        Self {
            user: User {
                id: id.into(),
                name,
            },
            created,
        }
    }
}

// State interface
impl History {
    pub fn as_of<S>(&self, sequence: S) -> Option<User>
    where
        S: Into<Sequence>,
    {
        self.events()
            .filter(Sequence::up_to(sequence.into()))
            .collect()
    }
}

// Events interface
impl History {
    fn created(&self) -> Event {
        Created {
            instant: self.created,
            user: self.user.clone(),
        }
        .into()
    }

    pub fn events(&self) -> impl Iterator<Item = Event> + Clone + use<> {
        [self.created()].into_iter()
    }
}