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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use getset::{Getters, MutGetters};
use rayon::iter::{FromParallelIterator, IntoParallelIterator};
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;

use crate::{domain::activity::Activity, ActivityGuid, ActivityItem};

/// The activity log entity
///
/// The activity log entity is used to store and manage activities
#[derive(Debug, Clone, Serialize, Deserialize, Getters, MutGetters, Default, PartialEq, Eq)]
#[getset(get = "pub", get_mut = "pub")]
pub struct ActivityLog {
    /// The activities in the log
    #[serde(flatten)]
    activities: BTreeMap<ActivityGuid, Activity>,
}

impl std::ops::DerefMut for ActivityLog {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.activities
    }
}

impl std::ops::Deref for ActivityLog {
    type Target = BTreeMap<ActivityGuid, Activity>;

    fn deref(&self) -> &Self::Target {
        &self.activities
    }
}

impl FromIterator<ActivityItem> for ActivityLog {
    fn from_iter<T: IntoIterator<Item = ActivityItem>>(iter: T) -> Self {
        let iter = iter
            .into_iter()
            .map(|item| (*item.guid(), item.activity().clone()));
        let map = iter.collect::<BTreeMap<_, _>>();

        Self { activities: map }
    }
}

impl FromIterator<(ActivityGuid, Activity)> for ActivityLog {
    fn from_iter<T: IntoIterator<Item = (ActivityGuid, Activity)>>(iter: T) -> Self {
        let map = BTreeMap::from_iter(iter);

        Self { activities: map }
    }
}

impl FromParallelIterator<(ActivityGuid, Activity)> for ActivityLog {
    fn from_par_iter<T: IntoParallelIterator<Item = (ActivityGuid, Activity)>>(
        par_iter: T,
    ) -> Self {
        let map = BTreeMap::from_par_iter(par_iter);

        Self { activities: map }
    }
}

#[cfg(test)]
mod tests {

    use crate::error::TestResult;

    use super::*;
    use rstest::*;
    use std::{fs, path::PathBuf};

    #[rstest]
    fn test_parse_activity_log_passes(
        #[files("../../data/*.toml")] activity_path: PathBuf,
    ) -> TestResult<()> {
        let toml_string = fs::read_to_string(activity_path)?;
        let _ = toml::from_str::<ActivityLog>(&toml_string)?;

        Ok(())
    }

    #[rstest]
    fn test_parse_activity_log_fails() {
        let toml_string = r#"
            [01HPY70577H375FDKT9XXAT7VB]
            name = "Test Activity"
            guid = "test-activity"
            start_time = "2021-01-01T00:00:00Z"
            end_time = "2021-01-01T00:00:00Z"
            duration = -5
            tags = ["test"]
        "#;

        let result = toml::from_str::<ActivityLog>(toml_string);
        assert!(result.is_err());
    }

    #[rstest]
    fn test_parse_activity_log_empty() {
        let toml_string = r"";

        let result = toml::from_str::<ActivityLog>(toml_string);
        assert!(result.is_ok());
    }

    #[rstest]
    fn test_parse_activity_log_single_activity() {
        let toml_string = r#"
            [01HPY70577H375FDKT9XXAT7VB]
            category = "development::pace"
            description = "Implemented the login feature."
            end = "2024-02-04T10:30:00"
            begin = "2024-02-04T09:00:00"
            duration = 5400
            kind = "activity"
        "#;

        let result = toml::from_str::<ActivityLog>(toml_string);
        assert!(result.is_ok());
    }

    #[rstest]
    fn test_parse_activity_log_multiple_activities() {
        let toml_string = r#"
            [01HQH12254TEXQ16WCR95YZ1SN]
            category = "development::pace"
            description = "Implemented the login feature."
            end = "2024-02-04T10:30:00"
            begin = "2024-02-04T09:00:00"
            duration = 5400
            kind = "activity"

            [01HQH129DHAWRKQG6NM13NH6MH]
            category = "development::pace"
            description = "Implemented the login feature."
            end = "2024-02-04T10:30:00"
            begin = "2024-02-04T09:00:00"
            duration = 5400
            kind = "activity"
        "#;

        let result = toml::from_str::<ActivityLog>(toml_string);
        assert!(result.is_ok());
    }

    #[rstest]
    fn test_parse_activity_log_multiple_activities_with_same_id_fails() {
        let toml_string = r#"
            [01HPY70577H375FDKT9XXAT7VB]
            category = "development::pace"
            description = "Implemented the login feature."
            end = "2024-02-04T10:30:00"
            begin = "2024-02-04T09:00:00"
            duration = 5400
            kind = "activity"

            [01HPY70577H375FDKT9XXAT7VB]
            category = "development::pace"
            description = "Implemented the login feature."
            end = "2024-02-04T10:30:00"
            begin = "2024-02-04T09:00:00"
            duration = 5400
            kind = "activity"
        "#;

        let result = toml::from_str::<ActivityLog>(toml_string);
        assert!(result.is_err());
    }
}