use std::cmp::Ordering;
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,
HasOpenness,
HasRelativity,
Interval,
IntervalTypeWithRel,
IsEmpty,
Openness,
Relativity,
};
use crate::intervals::relative::{
BoundedRelInterval,
EmptiableRelBoundPair,
HalfBoundedRelInterval,
HalfBoundedToFutureRelInterval,
HalfBoundedToPastRelInterval,
HasEmptiableRelBoundPair,
HasRelBoundPair,
RelBoundPair,
RelEndBound,
RelInterval,
RelStartBound,
};
use crate::intervals::special::{EmptyInterval, UnboundedInterval};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum EmptiableRelInterval {
Bound(RelInterval),
Empty(EmptyInterval),
}
impl EmptiableRelInterval {
#[must_use]
pub fn from_range<R>(range: R) -> Self
where
R: RangeBounds<SignedDuration>,
{
RelInterval::from_range(range).to_emptiable()
}
#[must_use]
pub fn bound(self) -> Option<RelInterval> {
match self {
Self::Bound(interval) => Some(interval),
Self::Empty(_) => None,
}
}
#[must_use]
pub fn ord_by_start_and_inv_length(&self, other: &Self) -> Ordering {
self.emptiable_rel_bound_pair()
.ord_by_start_and_inv_length(&other.emptiable_rel_bound_pair())
}
}
impl Interval for EmptiableRelInterval {}
impl HasDuration for EmptiableRelInterval {
fn duration(&self) -> IntervalDuration {
match self {
Self::Bound(interval) => interval.duration(),
Self::Empty(empty) => empty.duration(),
}
}
}
impl HasRelativity for EmptiableRelInterval {
fn relativity(&self) -> Relativity {
match self {
Self::Bound(interval) => interval.relativity(),
Self::Empty(empty) => empty.relativity(),
}
}
}
impl HasOpenness for EmptiableRelInterval {
fn openness(&self) -> Openness {
match self {
Self::Bound(interval) => interval.openness(),
Self::Empty(empty) => empty.openness(),
}
}
}
impl HasEmptiableRelBoundPair for EmptiableRelInterval {
fn emptiable_rel_bound_pair(&self) -> EmptiableRelBoundPair {
match self {
Self::Bound(interval) => EmptiableRelBoundPair::from(interval.rel_bound_pair()),
Self::Empty(interval) => interval.emptiable_rel_bound_pair(),
}
}
fn partial_rel_start(&self) -> Option<RelStartBound> {
match self {
Self::Bound(interval) => interval.partial_rel_start(),
Self::Empty(empty) => empty.partial_rel_start(),
}
}
fn partial_rel_end(&self) -> Option<RelEndBound> {
match self {
Self::Bound(interval) => interval.partial_rel_end(),
Self::Empty(empty) => empty.partial_rel_end(),
}
}
}
impl IsEmpty for EmptiableRelInterval {
fn is_empty(&self) -> bool {
matches!(self, Self::Empty(_))
}
}
impl HasIntervalTypeWithRel for EmptiableRelInterval {
fn interval_type_with_rel(&self) -> IntervalTypeWithRel {
match self {
Self::Empty(_) => IntervalTypeWithRel::Empty,
Self::Bound(interval) => interval.interval_type_with_rel(),
}
}
}
impl PartialOrd for EmptiableRelInterval {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for EmptiableRelInterval {
fn cmp(&self, other: &Self) -> Ordering {
self.emptiable_rel_bound_pair().cmp(&other.emptiable_rel_bound_pair())
}
}
impl From<BoundedRelInterval> for EmptiableRelInterval {
fn from(value: BoundedRelInterval) -> Self {
EmptiableRelInterval::Bound(value.to_interval())
}
}
impl From<HalfBoundedToFutureRelInterval> for EmptiableRelInterval {
fn from(value: HalfBoundedToFutureRelInterval) -> Self {
EmptiableRelInterval::Bound(value.to_interval())
}
}
impl From<HalfBoundedToPastRelInterval> for EmptiableRelInterval {
fn from(value: HalfBoundedToPastRelInterval) -> Self {
EmptiableRelInterval::Bound(value.to_interval())
}
}
impl From<HalfBoundedRelInterval> for EmptiableRelInterval {
fn from(value: HalfBoundedRelInterval) -> Self {
EmptiableRelInterval::Bound(value.to_interval())
}
}
impl From<UnboundedInterval> for EmptiableRelInterval {
fn from(value: UnboundedInterval) -> Self {
EmptiableRelInterval::Bound(RelInterval::Unbounded(value))
}
}
impl From<EmptyInterval> for EmptiableRelInterval {
fn from(value: EmptyInterval) -> Self {
EmptiableRelInterval::Empty(value)
}
}
impl From<RelBoundPair> for EmptiableRelInterval {
fn from(value: RelBoundPair) -> Self {
EmptiableRelInterval::Bound(RelInterval::from(value))
}
}
impl From<EmptiableRelBoundPair> for EmptiableRelInterval {
fn from(value: EmptiableRelBoundPair) -> Self {
match value.bound() {
Some(rel_bound_pair) => Self::from(rel_bound_pair),
None => Self::Empty(EmptyInterval),
}
}
}
impl From<RelInterval> for EmptiableRelInterval {
fn from(value: RelInterval) -> Self {
Self::Bound(value)
}
}
impl From<()> for EmptiableRelInterval {
fn from(_value: ()) -> Self {
EmptiableRelInterval::Empty(EmptyInterval)
}
}