use std::cmp::Ordering;
use std::error::Error;
use std::fmt::Display;
use std::ops::RangeBounds;
#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
use jiff::SignedDuration;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::intervals::meta::{
Duration as IntervalDuration,
HasDuration,
HasIntervalTypeWithRel,
HasOpeningDirection,
HasOpenness,
HasRelativity,
Interval,
IntervalTypeWithRel,
IsEmpty,
OpeningDirection,
Openness,
Relativity,
};
use crate::intervals::relative::{
BoundedRelInterval,
EmptiableRelBoundPair,
EmptiableRelInterval,
HalfBoundedRelInterval,
HalfBoundedToFutureRelInterval,
HalfBoundedToPastRelInterval,
HasRelBoundPair,
RelBoundPair,
RelEndBound,
RelStartBound,
};
use crate::intervals::special::UnboundedInterval;
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RelInterval {
Bounded(BoundedRelInterval),
HalfBounded(HalfBoundedRelInterval),
Unbounded(UnboundedInterval),
}
impl RelInterval {
#[must_use]
pub fn from_range<R>(range: R) -> Self
where
R: RangeBounds<SignedDuration>,
{
Self::from(RelBoundPair::from_range(range))
}
#[must_use]
pub fn ord_by_start_and_inv_length(&self, other: &Self) -> Ordering {
self.rel_bound_pair()
.ord_by_start_and_inv_length(&other.rel_bound_pair())
}
#[must_use]
pub fn bounded(self) -> Option<BoundedRelInterval> {
match self {
Self::Bounded(interval) => Some(interval),
_ => None,
}
}
#[must_use]
pub fn half_bounded(self) -> Option<HalfBoundedRelInterval> {
match self {
Self::HalfBounded(interval) => Some(interval),
_ => None,
}
}
#[must_use]
pub fn unbounded(self) -> Option<UnboundedInterval> {
match self {
Self::Unbounded(interval) => Some(interval),
_ => None,
}
}
#[must_use]
pub fn to_emptiable(self) -> EmptiableRelInterval {
EmptiableRelInterval::from(self)
}
}
impl Interval for RelInterval {}
impl HasRelBoundPair for RelInterval {
fn rel_bound_pair(&self) -> RelBoundPair {
match self {
Self::Bounded(bounded) => bounded.rel_bound_pair(),
Self::HalfBounded(half_bounded) => half_bounded.rel_bound_pair(),
Self::Unbounded(unbounded) => unbounded.rel_bound_pair(),
}
}
fn rel_start(&self) -> RelStartBound {
match self {
Self::Bounded(bounded) => bounded.rel_start(),
Self::HalfBounded(half_bounded) => half_bounded.rel_start(),
Self::Unbounded(unbounded) => unbounded.rel_start(),
}
}
fn rel_end(&self) -> RelEndBound {
match self {
Self::Bounded(bounded) => bounded.rel_end(),
Self::HalfBounded(half_bounded) => half_bounded.rel_end(),
Self::Unbounded(unbounded) => unbounded.rel_end(),
}
}
}
impl HasDuration for RelInterval {
fn duration(&self) -> IntervalDuration {
match self {
Self::Bounded(interval) => interval.duration(),
Self::HalfBounded(interval) => interval.duration(),
Self::Unbounded(interval) => interval.duration(),
}
}
}
impl HasRelativity for RelInterval {
fn relativity(&self) -> Relativity {
match self {
Self::Bounded(interval) => interval.relativity(),
Self::HalfBounded(interval) => interval.relativity(),
Self::Unbounded(interval) => interval.relativity(),
}
}
}
impl HasOpenness for RelInterval {
fn openness(&self) -> Openness {
match self {
Self::Bounded(interval) => interval.openness(),
Self::HalfBounded(interval) => interval.openness(),
Self::Unbounded(interval) => interval.openness(),
}
}
}
impl PartialOrd for RelInterval {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for RelInterval {
fn cmp(&self, other: &Self) -> Ordering {
self.rel_bound_pair().cmp(&other.rel_bound_pair())
}
}
impl IsEmpty for RelInterval {
fn is_empty(&self) -> bool {
false
}
}
impl HasIntervalTypeWithRel for RelInterval {
fn interval_type_with_rel(&self) -> IntervalTypeWithRel {
match self {
Self::Bounded(_) => IntervalTypeWithRel::RelBounded,
Self::HalfBounded(half_bounded) => IntervalTypeWithRel::RelHalfBounded(half_bounded.opening_direction()),
Self::Unbounded(_) => IntervalTypeWithRel::Unbounded,
}
}
}
impl From<BoundedRelInterval> for RelInterval {
fn from(value: BoundedRelInterval) -> Self {
RelInterval::Bounded(value)
}
}
impl From<HalfBoundedToFutureRelInterval> for RelInterval {
fn from(value: HalfBoundedToFutureRelInterval) -> Self {
Self::from(HalfBoundedRelInterval::from(value))
}
}
impl From<HalfBoundedToPastRelInterval> for RelInterval {
fn from(value: HalfBoundedToPastRelInterval) -> Self {
Self::from(HalfBoundedRelInterval::from(value))
}
}
impl From<HalfBoundedRelInterval> for RelInterval {
fn from(value: HalfBoundedRelInterval) -> Self {
RelInterval::HalfBounded(value)
}
}
impl From<UnboundedInterval> for RelInterval {
fn from(value: UnboundedInterval) -> Self {
RelInterval::Unbounded(value)
}
}
impl From<RelBoundPair> for RelInterval {
fn from(value: RelBoundPair) -> Self {
type StartB = RelStartBound;
type EndB = RelEndBound;
match (value.start(), value.end()) {
(StartB::InfinitePast, EndB::InfiniteFuture) => RelInterval::Unbounded(UnboundedInterval),
(StartB::InfinitePast, EndB::Finite(finite_end)) => {
RelInterval::HalfBounded(HalfBoundedRelInterval::new(finite_end.pos(), OpeningDirection::ToPast))
},
(StartB::Finite(finite_start), EndB::InfiniteFuture) => RelInterval::HalfBounded(
HalfBoundedRelInterval::new(finite_start.pos(), OpeningDirection::ToFuture),
),
(StartB::Finite(finite_start), EndB::Finite(finite_end)) => {
RelInterval::Bounded(BoundedRelInterval::unchecked_new(finite_start, finite_end))
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RelIntervalTryFromEmptiableRelBoundPairError;
impl Display for RelIntervalTryFromEmptiableRelBoundPairError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Failed to convert the emptiable relative bound pair into an relative interval"
)
}
}
impl Error for RelIntervalTryFromEmptiableRelBoundPairError {}
impl TryFrom<EmptiableRelBoundPair> for RelInterval {
type Error = RelIntervalTryFromEmptiableRelBoundPairError;
fn try_from(value: EmptiableRelBoundPair) -> Result<Self, Self::Error> {
Ok(Self::from(
value.bound().ok_or(RelIntervalTryFromEmptiableRelBoundPairError)?,
))
}
}