Skip to main content

tempoch_core/period/
error.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Vallés Puig, Ramon
3
4//! Error types for period and interval construction.
5
6use core::fmt;
7
8/// Error constructing an [`super::Interval`] with invalid bounds.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum InvalidIntervalError {
11    /// `!(start <= end)` (unordered comparisons — includes NaN endpoints).
12    StartAfterEnd,
13}
14
15impl fmt::Display for InvalidIntervalError {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        f.write_str("interval start must not be after end")
18    }
19}
20
21impl std::error::Error for InvalidIntervalError {}
22
23/// Invariants on a period list.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum PeriodListError {
26    /// Interval at `index` has `start > end`.
27    InvalidInterval { index: usize },
28    /// Interval at `index` is not sorted by start time.
29    Unsorted { index: usize },
30    /// Interval at `index` overlaps its predecessor.
31    Overlapping { index: usize },
32}
33
34impl fmt::Display for PeriodListError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Self::InvalidInterval { index } => {
38                write!(f, "interval at index {index} has start > end")
39            }
40            Self::Unsorted { index } => {
41                write!(f, "interval at index {index} is not sorted by start time")
42            }
43            Self::Overlapping { index } => {
44                write!(f, "interval at index {index} overlaps its predecessor")
45            }
46        }
47    }
48}
49
50impl std::error::Error for PeriodListError {}