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
use std::fmt::Display;

use crate::event_id::EventId;

#[derive(PartialEq, Eq)]
pub struct Range {
    events: Vec<EventId>
}

impl Ord for Range {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.begin().timestamp().cmp(other.begin().timestamp())
    }
}

impl PartialOrd for Range {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.begin().timestamp().cmp(other.begin().timestamp()))
    }
}

impl Range {
    #[allow(dead_code)]
    pub fn from(begin: EventId) -> Self {
        Self {
            events: vec![begin]
        }
    }

    pub fn begin(&self) -> &EventId {
        &self.events[0]
    }

    pub fn end(&self) -> &EventId {
        &self.events[self.events.len()-1]
    }

    #[allow(dead_code)]
    pub fn add_event(&mut self, end: EventId) {
        assert!(self.can_contain(&end));
        self.events.push(end);
    }

    #[allow(dead_code)]
    pub fn can_contain(&self, id: &EventId) -> bool {
        id.follows(self.end())
    }

    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.events.len()
    }

    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[allow(dead_code)]
    pub fn events(&self) -> std::slice::Iter<'_, EventId> {
        self.events.iter()
    }
}

impl Display for Range {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} - {} ({} - {})",
            self.begin().timestamp().format("%FT%T"), 
            self.end().timestamp().format("%FT%T"), 
            self.begin().event_record_id(), 
            self.end().event_record_id())
    }
}