apple_quant_algorithmic/timestamp/
range.rs1use super::{Timestamp, TimestampError};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub struct TimestampRangeExclusive {
5 pub start: Timestamp,
6 pub end: Timestamp,
7}
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum TimestampRangeInclusive {
11 One(Timestamp),
12
13 Multiple {
14 start: Timestamp,
15 last: Timestamp,
16 },
17}
18
19impl TimestampRangeInclusive {
20 pub fn new_from_iter(
21 timestamps: impl IntoIterator<Item = &Timestamp>,
22 ) -> Result<Self, TimestampError> {
23 let mut iter = timestamps.into_iter();
24
25 let Some(
26 mut start,
27 ) = iter.next().cloned() else {
28 return Err(TimestampError::RangeNotSatisfied { min: None, max: None });
29 };
30
31 let Some(
32 mut last,
33 ) = iter.next().cloned() else {
34 return Ok(Self::One(start));
35 };
36
37 if start > last {
38 let older_timestamp = start;
39 start = last;
40 last = older_timestamp;
41 }
42
43 for ×tamp in iter {
44 if timestamp < start {
45 start = timestamp;
46 }
47
48 if timestamp > last {
49 last = timestamp;
50 }
51 }
52
53 Ok(Self::Multiple { start, last })
54 }
55
56 pub fn start(
57 &self,
58 ) -> &Timestamp {
59 match self {
60 Self::One(
61 timestamp,
62 ) => timestamp,
63 Self::Multiple {
64 start,
65 last: _,
66 } => start,
67 }
68 }
69
70 pub fn last(
71 &self,
72 ) -> &Timestamp {
73 match self {
74 Self::One(
75 timestamp,
76 ) => timestamp,
77 Self::Multiple {
78 start: _,
79 last,
80 } => last,
81 }
82 }
83}