libevtx/
range.rs

1use std::fmt::Display;
2
3use crate::event_id::EventId;
4
5#[derive(PartialEq, Eq)]
6pub struct Range {
7    events: Vec<EventId>
8}
9
10impl Ord for Range {
11    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
12        self.begin().timestamp().cmp(other.begin().timestamp())
13    }
14}
15
16impl PartialOrd for Range {
17    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
18        Some(self.begin().timestamp().cmp(other.begin().timestamp()))
19    }
20}
21
22impl Range {
23    #[allow(dead_code)]
24    pub fn from(begin: EventId) -> Self {
25        Self {
26            events: vec![begin]
27        }
28    }
29
30    pub fn begin(&self) -> &EventId {
31        &self.events[0]
32    }
33
34    pub fn end(&self) -> &EventId {
35        &self.events[self.events.len()-1]
36    }
37
38    #[allow(dead_code)]
39    pub fn add_event(&mut self, end: EventId) {
40        assert!(self.can_contain(&end));
41        self.events.push(end);
42    }
43
44    #[allow(dead_code)]
45    pub fn can_contain(&self, id: &EventId) -> bool {
46        id.follows(self.end())
47    }
48
49    #[allow(dead_code)]
50    pub fn len(&self) -> usize {
51        self.events.len()
52    }
53
54    #[allow(dead_code)]
55    pub fn is_empty(&self) -> bool {
56        self.len() == 0
57    }
58
59    #[allow(dead_code)]
60    pub fn events(&self) -> std::slice::Iter<'_, EventId> {
61        self.events.iter()
62    }
63}
64
65impl Display for Range {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        write!(f, "{} - {} ({} - {})",
68            self.begin().timestamp().format("%FT%T"), 
69            self.end().timestamp().format("%FT%T"), 
70            self.begin().event_record_id(), 
71            self.end().event_record_id())
72    }
73}