use std::cmp::Ordering;
use std::error::Error;
use std::fmt::Display;
use std::ops::RangeBounds;
use jiff::SignedDuration;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::intervals::meta::{
BoundInclusivity,
Duration as IntervalDuration,
Epsilon,
HasBoundInclusivity,
HasDuration,
HasIntervalTypeWithRel,
HasOpenness,
HasRelativity,
Interval,
IntervalTypeWithRel,
IsEmpty,
OpeningDirection,
Openness,
Relativity,
};
use crate::intervals::ops::{BoundOrd, BoundOverlapDisambiguationRuleSet};
use crate::intervals::relative::{
BoundedRelInterval,
EmptiableRelBoundPair,
EmptiableRelInterval,
HalfBoundedRelInterval,
HasEmptiableRelBoundPair,
RelEndBound,
RelInterval,
RelStartBound,
check_rel_start_end_bounds_for_interval_creation,
prepare_rel_start_end_bounds_for_interval_creation,
};
use crate::intervals::special::UnboundedInterval;
pub trait HasRelBoundPair: HasEmptiableRelBoundPair {
#[must_use]
fn rel_bound_pair(&self) -> RelBoundPair;
#[must_use]
fn rel_start(&self) -> RelStartBound;
#[must_use]
fn rel_end(&self) -> RelEndBound;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct RelBoundPair {
start: RelStartBound,
end: RelEndBound,
}
impl RelBoundPair {
#[must_use]
pub fn unchecked_new(start: RelStartBound, end: RelEndBound) -> Self {
RelBoundPair {
start,
end,
}
}
#[must_use]
pub fn new(mut start: RelStartBound, mut end: RelEndBound) -> Self {
prepare_rel_start_end_bounds_for_interval_creation(&mut start, &mut end);
Self::unchecked_new(start, end)
}
#[must_use]
pub fn from_range<R>(range: R) -> Self
where
R: RangeBounds<SignedDuration>,
{
RelBoundPair::new(
RelStartBound::from(range.start_bound().cloned()),
RelEndBound::from(range.end_bound().cloned()),
)
}
#[must_use]
pub fn start(&self) -> RelStartBound {
self.start
}
#[must_use]
pub fn end(&self) -> RelEndBound {
self.end
}
#[must_use]
pub fn ord_by_start_and_inv_length(&self, other: &Self) -> Ordering {
match self
.start()
.bound_cmp(&other.start())
.disambiguate(BoundOverlapDisambiguationRuleSet::Strict)
{
Ordering::Less => Ordering::Less,
Ordering::Equal => self
.end()
.bound_cmp(&other.end())
.disambiguate(BoundOverlapDisambiguationRuleSet::Strict)
.reverse(),
Ordering::Greater => Ordering::Greater,
}
}
pub fn unchecked_set_start(&mut self, new_start: RelStartBound) {
self.start = new_start;
}
pub fn unchecked_set_end(&mut self, new_end: RelEndBound) {
self.end = new_end;
}
pub fn set_start(&mut self, new_start: RelStartBound) -> bool {
match check_rel_start_end_bounds_for_interval_creation(&new_start, &self.end()) {
Ok(()) => {
self.unchecked_set_start(new_start);
true
},
Err(_) => false,
}
}
pub fn set_end(&mut self, new_end: RelEndBound) -> bool {
match check_rel_start_end_bounds_for_interval_creation(&self.start(), &new_end) {
Ok(()) => {
self.unchecked_set_end(new_end);
true
},
Err(_) => false,
}
}
#[must_use]
pub fn to_interval(self) -> RelInterval {
RelInterval::from(self)
}
#[must_use]
pub fn to_emptiable_interval(self) -> EmptiableRelInterval {
self.to_interval().to_emptiable()
}
#[must_use]
pub fn to_emptiable(self) -> EmptiableRelBoundPair {
EmptiableRelBoundPair::from(self)
}
}
impl Interval for RelBoundPair {}
impl HasRelBoundPair for RelBoundPair {
fn rel_bound_pair(&self) -> RelBoundPair {
self.clone()
}
fn rel_start(&self) -> RelStartBound {
self.start()
}
fn rel_end(&self) -> RelEndBound {
self.end()
}
}
impl HasDuration for RelBoundPair {
fn duration(&self) -> IntervalDuration {
match (self.start(), self.end()) {
(RelStartBound::InfinitePast, _) | (_, RelEndBound::InfiniteFuture) => IntervalDuration::Infinite,
(RelStartBound::Finite(finite_start), RelEndBound::Finite(finite_end)) => IntervalDuration::Finite(
finite_end
.pos()
.offset()
.saturating_sub(finite_start.pos().offset())
.unsigned_abs(),
Epsilon::from((finite_start.pos().inclusivity(), finite_end.pos().inclusivity())),
),
}
}
}
impl HasOpenness for RelBoundPair {
fn openness(&self) -> Openness {
match (self.start(), self.end()) {
(RelStartBound::InfinitePast, RelEndBound::InfiniteFuture) => Openness::Unbounded,
(RelStartBound::InfinitePast, RelEndBound::Finite(_))
| (RelStartBound::Finite(_), RelEndBound::InfiniteFuture) => Openness::HalfBounded,
(RelStartBound::Finite(_), RelEndBound::Finite(_)) => Openness::Bounded,
}
}
}
impl HasRelativity for RelBoundPair {
fn relativity(&self) -> Relativity {
match (self.start(), self.end()) {
(RelStartBound::InfinitePast, RelEndBound::InfiniteFuture) => Relativity::Any,
_ => Relativity::Relative,
}
}
}
impl PartialOrd for RelBoundPair {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for RelBoundPair {
fn cmp(&self, other: &Self) -> Ordering {
self.start()
.bound_cmp(&other.start())
.disambiguate(BoundOverlapDisambiguationRuleSet::Strict)
}
}
impl IsEmpty for RelBoundPair {
fn is_empty(&self) -> bool {
false
}
}
impl HasIntervalTypeWithRel for RelBoundPair {
fn interval_type_with_rel(&self) -> IntervalTypeWithRel {
match (self.start(), self.end()) {
(RelStartBound::InfinitePast, RelEndBound::InfiniteFuture) => IntervalTypeWithRel::Unbounded,
(RelStartBound::InfinitePast, RelEndBound::Finite(_)) => {
IntervalTypeWithRel::RelHalfBounded(OpeningDirection::ToPast)
},
(RelStartBound::Finite(_), RelEndBound::InfiniteFuture) => {
IntervalTypeWithRel::RelHalfBounded(OpeningDirection::ToFuture)
},
(RelStartBound::Finite(_), RelEndBound::Finite(_)) => IntervalTypeWithRel::RelBounded,
}
}
}
impl From<(Option<SignedDuration>, Option<SignedDuration>)> for RelBoundPair {
fn from((start_opt, end_opt): (Option<SignedDuration>, Option<SignedDuration>)) -> Self {
let start = RelStartBound::from(start_opt);
let end = RelEndBound::from(end_opt);
RelBoundPair::new(start, end)
}
}
impl
From<(
Option<(SignedDuration, BoundInclusivity)>,
Option<(SignedDuration, BoundInclusivity)>,
)> for RelBoundPair
{
fn from(
(start_opt, end_opt): (
Option<(SignedDuration, BoundInclusivity)>,
Option<(SignedDuration, BoundInclusivity)>,
),
) -> Self {
let start = RelStartBound::from(start_opt);
let end = RelEndBound::from(end_opt);
Self::new(start, end)
}
}
impl From<BoundedRelInterval> for RelBoundPair {
fn from(value: BoundedRelInterval) -> Self {
value.rel_bound_pair()
}
}
impl From<HalfBoundedRelInterval> for RelBoundPair {
fn from(value: HalfBoundedRelInterval) -> Self {
value.rel_bound_pair()
}
}
impl From<RelInterval> for RelBoundPair {
fn from(value: RelInterval) -> Self {
value.rel_bound_pair()
}
}
impl From<UnboundedInterval> for RelBoundPair {
fn from(value: UnboundedInterval) -> Self {
value.rel_bound_pair()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RelBoundPairTryFromEmptiableRelBoundPairError;
impl Display for RelBoundPairTryFromEmptiableRelBoundPairError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"An error occurred when trying to convert `EmptiableRelBoundPair` into `RelBoundPair`"
)
}
}
impl Error for RelBoundPairTryFromEmptiableRelBoundPairError {}
impl TryFrom<EmptiableRelBoundPair> for RelBoundPair {
type Error = RelBoundPairTryFromEmptiableRelBoundPairError;
fn try_from(value: EmptiableRelBoundPair) -> Result<Self, Self::Error> {
value.bound().ok_or(RelBoundPairTryFromEmptiableRelBoundPairError)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RelBoundPairTryFromEmptiableRelIntervalError;
impl Display for RelBoundPairTryFromEmptiableRelIntervalError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"An error occurred when trying to convert `EmptiableRelInterval` into `RelBoundPair`"
)
}
}
impl Error for RelBoundPairTryFromEmptiableRelIntervalError {}
impl TryFrom<EmptiableRelInterval> for RelBoundPair {
type Error = RelBoundPairTryFromEmptiableRelIntervalError;
fn try_from(value: EmptiableRelInterval) -> Result<Self, Self::Error> {
Ok(value
.bound()
.ok_or(RelBoundPairTryFromEmptiableRelIntervalError)?
.rel_bound_pair())
}
}