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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use crate::Tag;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use uuid::Uuid;

pub type PatchRef = Uuid;
type EventRef = String;
type Set<T> = std::collections::HashSet<T>;

#[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Patch {
    pub id: Uuid,

    #[serde(default, skip_serializing_if = "Set::is_empty")]
    pub add_start: Set<AddStart>,

    #[serde(default, skip_serializing_if = "Set::is_empty")]
    pub remove_start: Set<RemoveStart>,

    #[serde(default, skip_serializing_if = "Set::is_empty")]
    pub add_tag: Set<AddTag>,

    #[serde(default, skip_serializing_if = "Set::is_empty")]
    pub remove_tag: Set<RemoveTag>,

    #[serde(default, skip_serializing_if = "Set::is_empty")]
    pub create_event: Set<CreateEvent>,
}

#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct AddStart {
    #[serde(default)]
    pub parents: BTreeSet<PatchRef>,
    pub event: EventRef,
    pub time: DateTime<Utc>,
}

#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct RemoveStart {
    #[serde(default)]
    pub parents: Option<BTreeSet<PatchRef>>,
    pub patch: PatchRef,
    pub event: EventRef,
    pub time: DateTime<Utc>,
}

#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct AddTag {
    #[serde(default)]
    pub parents: BTreeSet<PatchRef>,
    pub event: EventRef,
    pub tag: Tag,
}

#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct RemoveTag {
    #[serde(default)]
    pub parents: Option<BTreeSet<PatchRef>>,
    pub patch: PatchRef,
    pub event: EventRef,
    pub tag: Tag,
}

#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CreateEvent {
    pub event: EventRef,
    pub start: DateTime<Utc>,
    pub tags: Vec<Tag>,
}

impl Patch {
    pub fn new() -> Self {
        Self {
            id: Uuid::new_v4(),
            add_start: Set::new(),
            remove_start: Set::new(),
            add_tag: Set::new(),
            remove_tag: Set::new(),
            create_event: Set::new(),
        }
    }

    pub fn with_id(id: PatchRef) -> Self {
        Self {
            id,
            add_start: Set::new(),
            remove_start: Set::new(),
            add_tag: Set::new(),
            remove_tag: Set::new(),
            create_event: Set::new(),
        }
    }

    pub fn patch_ref(&self) -> &PatchRef {
        &self.id
    }

    pub fn parents(&self) -> Set<PatchRef> {
        let add_start_parents = self.add_start.iter().flat_map(|x| x.parents.iter());
        let remove_start_parents = self.remove_start.iter().map(|x| &x.patch).chain(
            self.remove_start
                .iter()
                .flat_map(|x| x.parents.iter().flat_map(|s| s.iter())),
        );
        let remove_tag_parents = self.remove_tag.iter().map(|x| &x.patch).chain(
            self.remove_tag
                .iter()
                .flat_map(|x| x.parents.iter().flat_map(|s| s.iter())),
        );
        let add_tag_parents = self.add_tag.iter().flat_map(|x| x.parents.iter());
        add_start_parents
            .chain(remove_start_parents)
            .chain(remove_tag_parents)
            .chain(add_tag_parents)
            .cloned()
            .collect()
    }

    pub fn add_start(mut self, parent: PatchRef, event: EventRef, time: DateTime<Utc>) -> Self {
        self.add_start.insert(AddStart {
            parents: {
                let mut s = BTreeSet::new();
                s.insert(parent);
                s
            },
            event,
            time,
        });
        self
    }

    pub fn remove_start(mut self, patch: PatchRef, event: EventRef, time: DateTime<Utc>) -> Self {
        self.remove_start.insert(RemoveStart {
            parents: None,
            patch,
            event,
            time,
        });
        self
    }

    pub fn add_tag(mut self, parent: PatchRef, event: EventRef, tag: String) -> Self {
        self.add_tag.insert(AddTag {
            parents: {
                let mut s = BTreeSet::new();
                s.insert(parent);
                s
            },
            event,
            tag,
        });
        self
    }

    pub fn remove_tag(mut self, patch: PatchRef, event: EventRef, tag: String) -> Self {
        self.remove_tag.insert(RemoveTag {
            parents: None,
            patch,
            event,
            tag,
        });
        self
    }

    pub fn create_event(
        mut self,
        event: EventRef,
        start: DateTime<Utc>,
        tags: Vec<String>,
    ) -> Self {
        self.create_event.insert(CreateEvent { event, start, tags });
        self
    }

    pub fn insert_add_start(&mut self, add_start: AddStart) {
        self.add_start.insert(add_start);
    }

    pub fn insert_remove_start(&mut self, remove_start: RemoveStart) {
        self.remove_start.insert(remove_start);
    }

    pub fn insert_add_tag(&mut self, add_tag: AddTag) {
        self.add_tag.insert(add_tag);
    }

    pub fn insert_remove_tag(&mut self, remove_tag: RemoveTag) {
        self.remove_tag.insert(remove_tag);
    }

    pub fn insert_create_event(&mut self, create_event: CreateEvent) {
        self.create_event.insert(create_event);
    }
}

impl Default for Patch {
    fn default() -> Self {
        Patch::new()
    }
}

impl AddStart {
    pub fn parents(&self) -> impl Iterator<Item = &PatchRef> {
        self.parents.iter()
    }
}
impl RemoveStart {
    pub fn parents(&self) -> impl Iterator<Item = &PatchRef> {
        self.parents.iter().flat_map(|s| s.iter())
    }
}
impl AddTag {
    pub fn parents(&self) -> impl Iterator<Item = &PatchRef> {
        self.parents.iter()
    }
}
impl RemoveTag {
    pub fn parents(&self) -> impl Iterator<Item = &PatchRef> {
        self.parents.iter().flat_map(|s| s.iter())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use chrono::offset::{TimeZone, Utc};

    macro_rules! s (
        { $stuff:expr } => {
            {
                $stuff.to_string()
            }
         };
    );

    #[test]
    fn read_patch_with_create_event_toml() {
        let id = Uuid::parse_str("e39076fe-6b5a-4a7f-b927-7fc1df5ba275").unwrap();
        let expected = Patch::with_id(id).create_event(
            s!("a"),
            Utc.ymd(2019, 7, 24).and_hms(14, 0, 0),
            vec![s!("work"), s!("coding")],
        );

        let toml_str = r#"
            id = "e39076fe-6b5a-4a7f-b927-7fc1df5ba275"

            [[create-event]]
            event = "a"
            start = "2019-07-24T14:00:00+00:00"
            tags = ["work", "coding"]
        "#;
        assert_eq!(toml::de::from_str(toml_str), Ok(expected));
    }

    #[test]
    fn serialize_patch_with_add_tag_toml() {
        let id = Uuid::parse_str("e39076fe-6b5a-4a7f-b927-7fc1df5ba275").unwrap();
        let patch0 = Uuid::parse_str("fa5de1d9-aa11-49fa-b064-8128281a7d91").unwrap();
        let event0 = Uuid::parse_str("0c435b19-4504-440c-abc7-f4e4d6a7d25f").unwrap();

        let patch = Patch::with_id(id).add_start(
            patch0.clone(),
            event0.to_string(),
            Utc.ymd(2019, 07, 24).and_hms(14, 0, 0),
        );

        let toml_str = "id = \"e39076fe-6b5a-4a7f-b927-7fc1df5ba275\"\n\n[[add-start]]\nparents = [\"fa5de1d9-aa11-49fa-b064-8128281a7d91\"]\nevent = \"0c435b19-4504-440c-abc7-f4e4d6a7d25f\"\ntime = \"2019-07-24T14:00:00Z\"\n".to_string();
        let serialized = toml::ser::to_string(&patch).unwrap();
        println!("{}", serialized);
        assert_eq!(toml_str, serialized);
    }

    #[test]
    fn read_patch_with_parents() {
        let id = Uuid::parse_str("e39076fe-6b5a-4a7f-b927-7fc1df5ba275").unwrap();
        let patch0 = Uuid::parse_str("fa5de1d9-aa11-49fa-b064-8128281a7d91").unwrap();
        let patch1 = Uuid::parse_str("0c435b19-4504-440c-abc7-f4e4d6a7d25f").unwrap();

        let mut expected = Patch::with_id(id);

        let remove_start = RemoveStart {
            parents: {
                let mut s = BTreeSet::new();
                s.insert(patch0.clone());
                s.insert(patch1.clone());
                Some(s)
            },
            patch: patch0.clone(),
            event: s!("a"),
            time: Utc.ymd(2019, 7, 24).and_hms(14, 0, 0),
        };
        expected.insert_remove_start(remove_start);

        let toml_str = r#"
            id = "e39076fe-6b5a-4a7f-b927-7fc1df5ba275"

            [[remove-start]]
            parents = ["fa5de1d9-aa11-49fa-b064-8128281a7d91", "0c435b19-4504-440c-abc7-f4e4d6a7d25f"]
            patch = "fa5de1d9-aa11-49fa-b064-8128281a7d91"
            event = "a"
            time = "2019-07-24T14:00:00+00:00"
        "#;
        assert_eq!(toml::de::from_str(toml_str), Ok(expected));
    }

    #[test]
    fn read_patch_with_all_fields_toml() {
        let patch0 = Uuid::parse_str("fa5de1d9-aa11-49fa-b064-8128281a7d91").unwrap();

        let expected =
            Patch::with_id(Uuid::parse_str("2a226f4d-60f2-493d-9e9a-d6c71d98b515").unwrap())
                .add_start(
                    patch0.clone(),
                    s!("a"),
                    Utc.ymd(2019, 7, 24).and_hms(14, 0, 0),
                )
                .remove_start(
                    patch0.clone(),
                    s!("a"),
                    Utc.ymd(2019, 7, 24).and_hms(14, 0, 0),
                )
                .add_tag(patch0.clone(), s!("a"), s!("work"))
                .remove_tag(patch0.clone(), s!("a"), s!("coding"))
                .create_event(
                    s!("a"),
                    Utc.ymd(2019, 7, 24).and_hms(14, 0, 0),
                    vec![s!("work"), s!("coding")],
                );

        let toml_str = r#"
            id = "2a226f4d-60f2-493d-9e9a-d6c71d98b515"

            [[add-start]]
            parents = ["fa5de1d9-aa11-49fa-b064-8128281a7d91"]
            event = "a"
            time = "2019-07-24T14:00:00+00:00"

            [[remove-start]]
            patch = "fa5de1d9-aa11-49fa-b064-8128281a7d91"
            event = "a"
            time = "2019-07-24T14:00:00+00:00"

            [[add-tag]]
            parents = ["fa5de1d9-aa11-49fa-b064-8128281a7d91"]
            event = "a"
            tag = "work"

            [[remove-tag]]
            patch = "fa5de1d9-aa11-49fa-b064-8128281a7d91"
            event = "a"
            tag = "coding"

            [[create-event]]
            event = "a"
            start = "2019-07-24T14:00:00+00:00"
            tags = ["work", "coding"]
        "#;
        assert_eq!(toml::de::from_str(toml_str), Ok(expected));
    }

}