apple-quant-algorithmic 0.4.0

Apple Quant's algorithmic library.
Documentation
use super::{Timestamp, TimestampError};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TimestampRangeExclusive {
	pub start: Timestamp,
	pub end: Timestamp,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TimestampRangeInclusive {
	One(Timestamp),

	Multiple {
		start: Timestamp,
		last: Timestamp,
	},
}

impl TimestampRangeInclusive {
	pub fn new_from_iter(
		timestamps: impl IntoIterator<Item = &Timestamp>,
	) -> Result<Self, TimestampError> {
		let mut iter = 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_timestamp = start;
			start = last;
			last = older_timestamp;
		}

		for &timestamp in iter {
			if timestamp < start {
				start = timestamp;
			}

			if timestamp > last {
				last = timestamp;
			}
		}

		Ok(Self::Multiple { start, last })
	}

	pub fn start(
		&self,
	) -> &Timestamp {
		match self {
			Self::One(
				timestamp,
			) => timestamp,
			Self::Multiple {
				start,
				last: _,
			} => start,
		}
	}

	pub fn last(
		&self,
	) -> &Timestamp {
		match self {
			Self::One(
				timestamp,
			) => timestamp,
			Self::Multiple {
				start: _,
				last,
			} => last,
		}
	}
}