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
21#[cfg(feature = "std")]
22impl std::error::Error for InvalidIntervalError {}
23
24/// Invariants on a period list.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum PeriodListError {
27    /// Interval at `index` has `start > end`.
28    InvalidInterval { index: usize },
29    /// Interval at `index` is not sorted by start time.
30    Unsorted { index: usize },
31    /// Interval at `index` overlaps its predecessor.
32    Overlapping { index: usize },
33}
34
35impl fmt::Display for PeriodListError {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Self::InvalidInterval { index } => {
39                write!(f, "interval at index {index} has start > end")
40            }
41            Self::Unsorted { index } => {
42                write!(f, "interval at index {index} is not sorted by start time")
43            }
44            Self::Overlapping { index } => {
45                write!(f, "interval at index {index} overlaps its predecessor")
46            }
47        }
48    }
49}
50
51#[cfg(feature = "std")]
52impl std::error::Error for PeriodListError {}