periodical 0.3.0

Management of all kinds of time intervals, use it to manage schedules, find overlaps, and more!
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Special intervals
//!
//! Includes intervals that are not absolute nor relative: [`UnboundedInterval`]
//! and [`EmptyInterval`].

use std::error::Error;
use std::fmt::Display;
use std::ops::RangeFull;
use std::time::Duration as StdDuration;

#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use super::absolute::{
    AbsBoundPair,
    AbsEndBound,
    AbsInterval,
    AbsStartBound,
    EmptiableAbsBoundPair,
    EmptiableAbsInterval,
    HasAbsBoundPair,
    HasEmptiableAbsBoundPair,
};
use super::meta::{
    Duration as IntervalDuration,
    HasDuration,
    HasOpenness,
    HasRelativity,
    IsEmpty,
    Openness,
    Relativity,
};
use super::relative::{
    EmptiableRelBoundPair,
    EmptiableRelInterval,
    HasEmptiableRelBoundPair,
    HasRelBoundPair,
    RelBoundPair,
    RelEndBound,
    RelInterval,
    RelStartBound,
};
use crate::intervals::meta::{Epsilon, Interval};

/// An unbounded interval
///
/// Interval without [`Relativity`] (not absolute nor relative) and without any
/// bounds. Is equivalent to _time itself_ (all time), infinite duration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct UnboundedInterval;

impl UnboundedInterval {
    /// Converts `self` into an [`AbsBoundPair`]
    #[must_use]
    pub fn to_abs_bound_pair(self) -> AbsBoundPair {
        AbsBoundPair::from(self)
    }

    /// Converts `self` into an [`EmptiableAbsBoundPair`]
    #[must_use]
    pub fn to_emptiable_abs_bound_pair(self) -> EmptiableAbsBoundPair {
        EmptiableAbsBoundPair::from(self)
    }

    /// Converts `self` into an [`AbsInterval`]
    #[must_use]
    pub fn to_abs_interval(self) -> AbsInterval {
        AbsInterval::from(self)
    }

    /// Converts `self` into an [`EmptiableAbsInterval`]
    #[must_use]
    pub fn to_emptiable_abs_interval(self) -> EmptiableAbsInterval {
        EmptiableAbsInterval::from(self)
    }

    /// Converts `self` into an [`RelBoundPair`]
    #[must_use]
    pub fn to_rel_bound_pair(self) -> RelBoundPair {
        RelBoundPair::from(self)
    }

    /// Converts `self` into an [`EmptiableRelBoundPair`]
    #[must_use]
    pub fn to_emptiable_rel_bound_pair(self) -> EmptiableRelBoundPair {
        EmptiableRelBoundPair::from(self)
    }

    /// Converts `self` into an [`RelInterval`]
    #[must_use]
    pub fn to_rel_interval(self) -> RelInterval {
        RelInterval::from(self)
    }

    /// Converts `self` into an [`EmptiableRelInterval`]
    #[must_use]
    pub fn to_emptiable_rel_interval(self) -> EmptiableRelInterval {
        EmptiableRelInterval::from(self)
    }
}

impl Interval for UnboundedInterval {}

impl HasOpenness for UnboundedInterval {
    fn openness(&self) -> Openness {
        Openness::Unbounded
    }
}

impl HasRelativity for UnboundedInterval {
    fn relativity(&self) -> Relativity {
        Relativity::Any
    }
}

impl HasDuration for UnboundedInterval {
    fn duration(&self) -> IntervalDuration {
        IntervalDuration::Infinite
    }
}

impl HasAbsBoundPair for UnboundedInterval {
    fn abs_bound_pair(&self) -> AbsBoundPair {
        AbsBoundPair::new(self.abs_start(), self.abs_end())
    }

    fn abs_start(&self) -> AbsStartBound {
        AbsStartBound::InfinitePast
    }

    fn abs_end(&self) -> AbsEndBound {
        AbsEndBound::InfiniteFuture
    }
}

impl HasRelBoundPair for UnboundedInterval {
    fn rel_bound_pair(&self) -> RelBoundPair {
        RelBoundPair::new(self.rel_start(), self.rel_end())
    }

    fn rel_start(&self) -> RelStartBound {
        RelStartBound::InfinitePast
    }

    fn rel_end(&self) -> RelEndBound {
        RelEndBound::InfiniteFuture
    }
}

impl From<RangeFull> for UnboundedInterval {
    fn from(_value: RangeFull) -> Self {
        UnboundedInterval
    }
}

/// Error that can occur when trying to convert [`AbsBoundPair`] into [`UnboundedInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnboundedIntervalTryFromAbsBoundPairError;

impl Display for UnboundedIntervalTryFromAbsBoundPairError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `AbsBoundPair` into `UnboundedInterval`"
        )
    }
}

impl Error for UnboundedIntervalTryFromAbsBoundPairError {}

impl TryFrom<AbsBoundPair> for UnboundedInterval {
    type Error = UnboundedIntervalTryFromAbsBoundPairError;

    fn try_from(value: AbsBoundPair) -> Result<Self, Self::Error> {
        match (value.start(), value.end()) {
            (AbsStartBound::InfinitePast, AbsEndBound::InfiniteFuture) => Ok(UnboundedInterval),
            _ => Err(UnboundedIntervalTryFromAbsBoundPairError),
        }
    }
}

/// Error that can occur when trying to convert [`RelBoundPair`] into [`UnboundedInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnboundedIntervalTryFromRelBoundPairError;

impl Display for UnboundedIntervalTryFromRelBoundPairError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `RelBoundPair` into `UnboundedInterval`"
        )
    }
}

impl Error for UnboundedIntervalTryFromRelBoundPairError {}

impl TryFrom<RelBoundPair> for UnboundedInterval {
    type Error = UnboundedIntervalTryFromRelBoundPairError;

    fn try_from(value: RelBoundPair) -> Result<Self, Self::Error> {
        match (value.start(), value.end()) {
            (RelStartBound::InfinitePast, RelEndBound::InfiniteFuture) => Ok(UnboundedInterval),
            _ => Err(UnboundedIntervalTryFromRelBoundPairError),
        }
    }
}

/// Error that can occur when trying to convert [`EmptiableAbsBoundPair`] into [`UnboundedInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnboundedIntervalTryFromEmptiableAbsBoundPairError;

impl Display for UnboundedIntervalTryFromEmptiableAbsBoundPairError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `EmptiableAbsBoundPair` into `UnboundedInterval`"
        )
    }
}

impl Error for UnboundedIntervalTryFromEmptiableAbsBoundPairError {}

impl TryFrom<EmptiableAbsBoundPair> for UnboundedInterval {
    type Error = UnboundedIntervalTryFromEmptiableAbsBoundPairError;

    fn try_from(value: EmptiableAbsBoundPair) -> Result<Self, Self::Error> {
        Self::try_from(
            value
                .bound()
                .ok_or(UnboundedIntervalTryFromEmptiableAbsBoundPairError)?,
        )
        .or(Err(UnboundedIntervalTryFromEmptiableAbsBoundPairError))
    }
}

/// Error that can occur when trying to convert [`EmptiableRelBoundPair`] into [`UnboundedInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnboundedIntervalTryFromEmptiableRelBoundPairError;

impl Display for UnboundedIntervalTryFromEmptiableRelBoundPairError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `EmptiableRelBoundPair` into `UnboundedInterval`"
        )
    }
}

impl Error for UnboundedIntervalTryFromEmptiableRelBoundPairError {}

impl TryFrom<EmptiableRelBoundPair> for UnboundedInterval {
    type Error = UnboundedIntervalTryFromEmptiableRelBoundPairError;

    fn try_from(value: EmptiableRelBoundPair) -> Result<Self, Self::Error> {
        Self::try_from(
            value
                .bound()
                .ok_or(UnboundedIntervalTryFromEmptiableRelBoundPairError)?,
        )
        .or(Err(UnboundedIntervalTryFromEmptiableRelBoundPairError))
    }
}

/// Error that can occur when trying to convert [`AbsInterval`] into [`UnboundedInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnboundedIntervalTryFromAbsIntervalError;

impl Display for UnboundedIntervalTryFromAbsIntervalError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `AbsInterval` into `UnboundedInterval`"
        )
    }
}

impl Error for UnboundedIntervalTryFromAbsIntervalError {}

impl TryFrom<AbsInterval> for UnboundedInterval {
    type Error = UnboundedIntervalTryFromAbsIntervalError;

    fn try_from(value: AbsInterval) -> Result<Self, Self::Error> {
        value.unbounded().ok_or(UnboundedIntervalTryFromAbsIntervalError)
    }
}

/// Error that can occur when trying to convert [`RelInterval`] into [`UnboundedInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnboundedIntervalTryFromRelIntervalError;

impl Display for UnboundedIntervalTryFromRelIntervalError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `RelInterval` into `UnboundedInterval`"
        )
    }
}

impl Error for UnboundedIntervalTryFromRelIntervalError {}

impl TryFrom<RelInterval> for UnboundedInterval {
    type Error = UnboundedIntervalTryFromRelIntervalError;

    fn try_from(value: RelInterval) -> Result<Self, Self::Error> {
        value.unbounded().ok_or(UnboundedIntervalTryFromRelIntervalError)
    }
}

/// Error that can occur when trying to convert [`EmptiableAbsInterval`] into [`UnboundedInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnboundedIntervalTryFromEmptiableAbsIntervalError;

impl Display for UnboundedIntervalTryFromEmptiableAbsIntervalError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `EmptiableAbsInterval` into `UnboundedInterval`"
        )
    }
}

impl Error for UnboundedIntervalTryFromEmptiableAbsIntervalError {}

impl TryFrom<EmptiableAbsInterval> for UnboundedInterval {
    type Error = UnboundedIntervalTryFromEmptiableAbsIntervalError;

    fn try_from(value: EmptiableAbsInterval) -> Result<Self, Self::Error> {
        Self::try_from(value.bound().ok_or(UnboundedIntervalTryFromEmptiableAbsIntervalError)?)
            .or(Err(UnboundedIntervalTryFromEmptiableAbsIntervalError))
    }
}

/// Error that can occur when trying to convert [`EmptiableRelInterval`] into [`UnboundedInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnboundedIntervalTryFromEmptiableRelIntervalError;

impl Display for UnboundedIntervalTryFromEmptiableRelIntervalError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `EmptiableRelInterval` into `UnboundedInterval`"
        )
    }
}

impl Error for UnboundedIntervalTryFromEmptiableRelIntervalError {}

impl TryFrom<EmptiableRelInterval> for UnboundedInterval {
    type Error = UnboundedIntervalTryFromEmptiableRelIntervalError;

    fn try_from(value: EmptiableRelInterval) -> Result<Self, Self::Error> {
        Self::try_from(value.bound().ok_or(UnboundedIntervalTryFromEmptiableRelIntervalError)?)
            .or(Err(UnboundedIntervalTryFromEmptiableRelIntervalError))
    }
}

/// Empty interval
///
/// Similar to the [empty set](https://en.wikipedia.org/w/index.php?title=Empty_set&oldid=1336567852),
/// this allows for still performing operations such as the complement of the interval without issues,
/// but the difference between an empty set and and empty interval is that intervals are
/// linked to time, therefore empty intervals are out of this time dimension.
///
/// This means that, contrary to an empty set, an empty interval is **not** a
/// subset of any interval. It simply represents the _lack_ of a time interval,
/// like the complement of an unbounded interval.
///
/// In regards to operations such as the overlap position, or union, since an
/// empty interval has no defined place in time, it is always _outside_,
/// _separate_ from the compared.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct EmptyInterval;

impl EmptyInterval {
    /// Converts `self` into [`EmptiableAbsBoundPair`]
    #[must_use]
    pub fn to_emptiable_abs_bound_pair(self) -> EmptiableAbsBoundPair {
        EmptiableAbsBoundPair::Empty
    }

    /// Converts `self` into [`EmptiableAbsInterval`]
    #[must_use]
    pub fn to_emptiable_abs_interval(self) -> EmptiableAbsInterval {
        EmptiableAbsInterval::Empty(self)
    }

    /// Converts `self` into [`EmptiableRelBoundPair`]
    #[must_use]
    pub fn to_emptiable_rel_bound_pair(self) -> EmptiableRelBoundPair {
        EmptiableRelBoundPair::Empty
    }

    /// Converts `self` into [`EmptiableRelInterval`]
    #[must_use]
    pub fn to_emptiable_rel_interval(self) -> EmptiableRelInterval {
        EmptiableRelInterval::Empty(self)
    }
}

impl Interval for EmptyInterval {}

impl HasOpenness for EmptyInterval {
    fn openness(&self) -> Openness {
        Openness::Empty
    }
}

impl HasRelativity for EmptyInterval {
    fn relativity(&self) -> Relativity {
        Relativity::Any
    }
}

impl HasDuration for EmptyInterval {
    fn duration(&self) -> IntervalDuration {
        IntervalDuration::Finite(StdDuration::ZERO, Epsilon::None)
    }
}

impl HasEmptiableAbsBoundPair for EmptyInterval {
    fn emptiable_abs_bound_pair(&self) -> EmptiableAbsBoundPair {
        EmptiableAbsBoundPair::Empty
    }

    fn partial_abs_start(&self) -> Option<AbsStartBound> {
        None
    }

    fn partial_abs_end(&self) -> Option<AbsEndBound> {
        None
    }
}

impl HasEmptiableRelBoundPair for EmptyInterval {
    fn emptiable_rel_bound_pair(&self) -> EmptiableRelBoundPair {
        EmptiableRelBoundPair::Empty
    }

    fn partial_rel_start(&self) -> Option<RelStartBound> {
        None
    }

    fn partial_rel_end(&self) -> Option<RelEndBound> {
        None
    }
}

impl IsEmpty for EmptyInterval {
    fn is_empty(&self) -> bool {
        true
    }
}

/// Error that can occur when trying to convert [`EmptiableAbsBoundPair`] into [`EmptyInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EmptyIntervalTryFromEmptiableAbsBoundPair;

impl Display for EmptyIntervalTryFromEmptiableAbsBoundPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `EmptiableAbsBoundPair` into `EmptyInterval`"
        )
    }
}

impl Error for EmptyIntervalTryFromEmptiableAbsBoundPair {}

impl TryFrom<EmptiableAbsBoundPair> for EmptyInterval {
    type Error = EmptyIntervalTryFromEmptiableAbsBoundPair;

    fn try_from(value: EmptiableAbsBoundPair) -> Result<Self, Self::Error> {
        value
            .is_empty()
            .then_some(EmptyInterval)
            .ok_or(EmptyIntervalTryFromEmptiableAbsBoundPair)
    }
}

/// Error that can occur when trying to convert [`EmptiableAbsInterval`] into [`EmptyInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EmptyIntervalTryFromEmptiableAbsInterval;

impl Display for EmptyIntervalTryFromEmptiableAbsInterval {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `EmptiableAbsInterval` into `EmptyInterval`"
        )
    }
}

impl Error for EmptyIntervalTryFromEmptiableAbsInterval {}

impl TryFrom<EmptiableAbsInterval> for EmptyInterval {
    type Error = EmptyIntervalTryFromEmptiableAbsInterval;

    fn try_from(value: EmptiableAbsInterval) -> Result<Self, Self::Error> {
        value
            .is_empty()
            .then_some(EmptyInterval)
            .ok_or(EmptyIntervalTryFromEmptiableAbsInterval)
    }
}

/// Error that can occur when trying to convert [`EmptiableRelBoundPair`] into [`EmptyInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EmptyIntervalTryFromEmptiableRelBoundPair;

impl Display for EmptyIntervalTryFromEmptiableRelBoundPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `EmptiableRelBoundPair` into `EmptyInterval`"
        )
    }
}

impl Error for EmptyIntervalTryFromEmptiableRelBoundPair {}

impl TryFrom<EmptiableRelBoundPair> for EmptyInterval {
    type Error = EmptyIntervalTryFromEmptiableRelBoundPair;

    fn try_from(value: EmptiableRelBoundPair) -> Result<Self, Self::Error> {
        value
            .is_empty()
            .then_some(EmptyInterval)
            .ok_or(EmptyIntervalTryFromEmptiableRelBoundPair)
    }
}

/// Error that can occur when trying to convert [`EmptiableRelInterval`] into [`EmptyInterval`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EmptyIntervalTryFromEmptiableRelInterval;

impl Display for EmptyIntervalTryFromEmptiableRelInterval {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "An error occurred when trying to convert `EmptiableRelInterval` into `EmptyInterval`"
        )
    }
}

impl Error for EmptyIntervalTryFromEmptiableRelInterval {}

impl TryFrom<EmptiableRelInterval> for EmptyInterval {
    type Error = EmptyIntervalTryFromEmptiableRelInterval;

    fn try_from(value: EmptiableRelInterval) -> Result<Self, Self::Error> {
        value
            .is_empty()
            .then_some(EmptyInterval)
            .ok_or(EmptyIntervalTryFromEmptiableRelInterval)
    }
}