Skip to main content

stkcraft_time/
error.rs

1//! Error types shared by all modules.
2
3use core::fmt;
4
5/// Errors returned by `stkcraft-time`.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum TimeError {
8    /// A calendar date is not valid for the selected calendar.
9    InvalidDate { year: i32, month: u8, day: u8 },
10    /// The time of day is outside its legal range.
11    InvalidTime { hour: u8, minute: u8 },
12    /// The seconds component is outside `0.0 .. 61.0`.
13    InvalidSecond,
14    /// A formatting pattern contains an unsupported token.
15    InvalidFormat,
16    /// A kernel `@date` literal is not in a supported N0067 form.
17    ///
18    /// This is deliberately distinct from a missing kernel variable: the
19    /// literal is present, it simply cannot be resolved.
20    KernelDateUnsupported,
21    /// An EOP table was empty.
22    EopMissing,
23    /// An IERS EOP file could not be parsed.
24    EopParse,
25    /// A spacecraft clock kernel could not be parsed.
26    SclkParse,
27    /// A required spacecraft clock kernel variable is missing.
28    SclkMissing(&'static str),
29    /// The spacecraft clock type is not supported.
30    SclkUnsupported,
31    /// SCLK partition-aware field/string conversion is not implemented.
32    SclkPartitionsUnsupported,
33    /// The requested epoch lies outside the EOP table.
34    Ut1OutOfRange,
35    /// A UTC time that does not exist at a leap-second boundary:
36    /// `23:59:60.x` on a day without a positive leap second, or
37    /// `23:59:59.x` on a shortened (86399-second) day.
38    InvalidLeapSecond,
39    /// The requested UTC epoch lies before the first LSK entry.
40    UtcOutOfRange,
41    /// The LSK text could not be parsed.
42    LskParse,
43    /// A required LSK variable is missing.
44    LskMissing(&'static str),
45    /// An arithmetic operation overflowed the internal representation.
46    Overflow,
47}
48
49impl fmt::Display for TimeError {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match self {
52            TimeError::InvalidDate { year, month, day } => {
53                write!(f, "invalid calendar date {year:04}-{month:02}-{day:02}")
54            }
55            TimeError::InvalidTime { hour, minute } => {
56                write!(f, "invalid time of day {hour:02}:{minute:02}")
57            }
58            TimeError::InvalidSecond => write!(f, "invalid seconds component"),
59            TimeError::InvalidFormat => write!(f, "unsupported formatting token"),
60            TimeError::KernelDateUnsupported => {
61                write!(f, "unsupported kernel date literal")
62            }
63            TimeError::EopMissing => write!(f, "EOP table is empty"),
64            TimeError::EopParse => write!(f, "could not parse the IERS EOP file"),
65            TimeError::SclkParse => write!(f, "could not parse the spacecraft clock kernel"),
66            TimeError::SclkMissing(name) => {
67                write!(f, "spacecraft clock kernel is missing {name}")
68            }
69            TimeError::SclkUnsupported => {
70                write!(
71                    f,
72                    "unsupported spacecraft clock type (only type 1 is supported)"
73                )
74            }
75            TimeError::SclkPartitionsUnsupported => {
76                write!(
77                    f,
78                    "SCLK partition-aware field/string conversion is not implemented"
79                )
80            }
81            TimeError::Ut1OutOfRange => {
82                write!(f, "requested epoch lies outside the EOP table")
83            }
84            TimeError::InvalidLeapSecond => {
85                write!(f, "UTC time does not exist at this leap-second boundary")
86            }
87            TimeError::UtcOutOfRange => {
88                write!(f, "UTC epoch lies outside the supplied leap-second table")
89            }
90            TimeError::LskParse => write!(f, "could not parse the leap-second kernel"),
91            TimeError::LskMissing(name) => write!(f, "leap-second kernel is missing {name}"),
92            TimeError::Overflow => write!(f, "time arithmetic overflow"),
93        }
94    }
95}
96
97#[cfg(feature = "std")]
98impl std::error::Error for TimeError {}