use std::cmp::Ordering;
use jiff::Timestamp;
use jiff::tz::TimeZone;
use crate::intervals::absolute::{BoundedAbsInterval, HalfBoundedAbsInterval, HasAbsBoundPair};
use crate::iter::intervals::split::CalendarAnchorOffsetSplit;
use crate::time::CalendarAnchorOffset;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CalendarAnchorOffsetSplitResult {
Infinite(HalfBoundedAbsInterval),
Full(BoundedAbsInterval),
Partial(BoundedAbsInterval),
}
impl CalendarAnchorOffsetSplitResult {
#[must_use]
pub fn infinite(self) -> Option<HalfBoundedAbsInterval> {
match self {
Self::Infinite(x) => Some(x),
_ => None,
}
}
#[must_use]
pub fn full(self) -> Option<BoundedAbsInterval> {
match self {
Self::Full(x) => Some(x),
_ => None,
}
}
#[must_use]
pub fn partial(self) -> Option<BoundedAbsInterval> {
match self {
Self::Partial(x) => Some(x),
_ => None,
}
}
#[must_use]
pub fn is_infinite(&self) -> bool {
matches!(self, Self::Infinite(_))
}
#[must_use]
pub fn is_full(&self) -> bool {
matches!(self, Self::Full(_))
}
#[must_use]
pub fn is_partial(&self) -> bool {
matches!(self, Self::Partial(_))
}
}
impl PartialOrd for CalendarAnchorOffsetSplitResult {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for CalendarAnchorOffsetSplitResult {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(Self::Infinite(_), Self::Infinite(_))
| (Self::Full(_), Self::Full(_))
| (Self::Partial(_), Self::Partial(_)) => Ordering::Equal,
(Self::Infinite(_), _) | (Self::Full(_), Self::Partial(_)) => Ordering::Greater,
(Self::Full(_), Self::Infinite(_)) | (Self::Partial(_), _) => Ordering::Less,
}
}
}
pub trait CalendarAnchorOffsetSplittable
where
Self: Sized,
{
fn split_by_calendar_anchor_offset(
self,
calendar_anchor_offset: CalendarAnchorOffset,
tz: TimeZone,
) -> CalendarAnchorOffsetSplit;
}
impl<T> CalendarAnchorOffsetSplittable for T
where
T: HasAbsBoundPair,
{
fn split_by_calendar_anchor_offset(
self,
calendar_anchor_offset: CalendarAnchorOffset,
tz: TimeZone,
) -> CalendarAnchorOffsetSplit {
CalendarAnchorOffsetSplit::new(&self, calendar_anchor_offset, tz)
}
}