Skip to main content

gnss_time/
error.rs

1//! Error types for the `gnss-time` crate.
2
3use core::fmt;
4
5/// All errors that `gnss-time` operations can produce.
6///
7/// This type is marked `#[must_use]`: ignoring an error from a fallible
8/// conversion silently discards the failure reason.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[must_use = "errors must be handled; use `?` or `match` to inspect the failure"]
11#[non_exhaustive]
12pub enum GnssTimeError {
13    /// Integer arithmetic overflowed the `u64` nanosecond range.
14    Overflow,
15
16    /// A coordinate or parameter was outside its valid domain.
17    ///
18    /// Carries a static description of which parameter was invalid.
19    InvalidInput(&'static str),
20
21    /// A conversion required leap-second context that was not provided.
22    LeapSecondsRequired,
23}
24
25impl fmt::Display for GnssTimeError {
26    fn fmt(
27        &self,
28        f: &mut fmt::Formatter<'_>,
29    ) -> fmt::Result {
30        match self {
31            GnssTimeError::Overflow => {
32                f.write_str("nanosecond arithmetic overflowed the u64 range")
33            }
34            GnssTimeError::InvalidInput(msg) => {
35                write!(f, "invalid input: {msg}")
36            }
37            GnssTimeError::LeapSecondsRequired => f.write_str(
38                "this conversion requires a LeapSeconds context \
39                 (GLONASS <-> GPS or GPS <-> UTC)",
40            ),
41        }
42    }
43}
44
45// `std::error::Error` impl behind the `std` feature gate.
46#[cfg(feature = "std")]
47impl std::error::Error for GnssTimeError {}
48
49// defmt support: embedded logging via probe-rs / defmt-rtt.
50#[cfg(feature = "defmt")]
51impl defmt::Format for GnssTimeError {
52    fn format(
53        &self,
54        f: defmt::Formatter,
55    ) {
56        match self {
57            GnssTimeError::Overflow => {
58                defmt::write!(f, "nanosecond arithmetic overflowed the u64 range")
59            }
60            GnssTimeError::InvalidInput(msg) => {
61                defmt::write!(f, "invalid input: {}", msg)
62            }
63            GnssTimeError::LeapSecondsRequired => {
64                defmt::write!(f, "conversion requires LeapSeconds context")
65            }
66        }
67    }
68}