Skip to main content

apple_quant_algorithmic/timestamp/
range_trade.rs

1use crate::timestamp::{TimestampError, TradeTimestamp};
2
3#[derive(Debug, Clone, Copy)]
4pub enum TradeTimestampRange {
5	One(TradeTimestamp),
6	Many {
7		min: TradeTimestamp,
8		max: TradeTimestamp,
9	},
10}
11
12impl Default for TradeTimestampRange {
13	fn default() -> Self {
14		Self::One(TradeTimestamp::default())
15	}
16}
17
18impl TradeTimestampRange {
19	pub fn new_from_iter(
20		trade_timestamps: impl IntoIterator<Item = &TradeTimestamp>
21	) -> Result<Self, TimestampError> {
22		let mut iter = trade_timestamps.into_iter();
23
24		let Some(mut min) = iter.next().cloned() else {
25			return Err(TimestampError::RangeNotSatisfied { min: None, max: None });
26		};
27
28		let Some(mut max) = iter.next().cloned() else {
29			return Ok(Self::One(min));
30		};
31
32		for timestamp in iter {
33			if timestamp < &min {
34				min = *timestamp;
35			}
36
37			if timestamp > &max {
38				max = *timestamp;
39			}
40		}
41
42		Ok(Self::Many { min, max })
43	}
44
45	pub fn new_with_many_from_parts(
46		min: TradeTimestamp,
47		max: TradeTimestamp,
48	) -> Self {
49		Self::Many { min, max }
50	}
51
52	pub fn min(&self) -> &TradeTimestamp {
53		match self {
54			Self::One(trade_timestamp) => trade_timestamp,
55			Self::Many { min, max: _ } => min,
56		}
57	}
58
59	pub fn max(&self) -> &TradeTimestamp {
60		match self {
61			Self::One(trade_timestamp) => trade_timestamp,
62			Self::Many { min: _, max } => max,
63		}
64	}
65}