use crate::timestamp::{TimestampError, TradeTimestamp};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TradeTimestampRangeExclusive {
pub start: TradeTimestamp,
pub end: TradeTimestamp,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TradeTimestampRangeInclusive {
One(TradeTimestamp),
Multiple {
start: TradeTimestamp,
last: TradeTimestamp,
},
}
impl TradeTimestampRangeInclusive {
pub fn new_from_iter(
trade_timestamps: impl IntoIterator<Item = &TradeTimestamp>,
) -> Result<Self, TimestampError> {
let mut iter = trade_timestamps.into_iter();
let Some(
mut start,
) = iter.next().cloned() else {
return Err(TimestampError::RangeNotSatisfied { min: None, max: None });
};
let Some(
mut last,
) = iter.next().cloned() else {
return Ok(Self::One(start));
};
if start > last {
let older_trade_timestamp = start;
start = last;
last = older_trade_timestamp;
}
for &trade_timestamp in iter {
if trade_timestamp < start {
start = trade_timestamp;
}
if trade_timestamp > last {
last = trade_timestamp;
}
}
Ok(Self::Multiple { start, last })
}
pub fn start(
&self,
) -> &TradeTimestamp {
match self {
Self::One(
trade_timestamp,
) => trade_timestamp,
Self::Multiple {
start,
last: _,
} => start,
}
}
pub fn last(
&self,
) -> &TradeTimestamp {
match self {
Self::One(
trade_timestamp,
) => trade_timestamp,
Self::Multiple {
start: _,
last,
} => last,
}
}
}