Skip to main content

pleiades_eclipse/
error.rs

1//! Structured, fail-closed eclipse errors.
2
3use core::fmt;
4
5/// First instant of the supported window (1900-01-01 TT), Julian Day.
6pub const WINDOW_START_JD: f64 = 2_415_020.5;
7/// Last instant of the supported window, Julian Day. This is the end of the
8/// packaged backend's actual Sun/Moon coverage (2100-01-01 TT); the backend has
9/// no segments beyond it, so eclipses in the remainder of 2100 are out of range.
10pub const WINDOW_END_JD: f64 = 2_488_069.5;
11
12/// Errors returned by the eclipse engine; all variants fail closed.
13#[derive(Clone, Debug, PartialEq)]
14pub enum EclipseError {
15    /// A requested instant falls outside the 1900–2100 CE window.
16    OutOfWindow {
17        /// The out-of-window instant, as a Julian Day.
18        julian_day: f64,
19    },
20    /// The backend returned a structured error (message forwarded verbatim).
21    Backend(String),
22    /// The backend produced no ecliptic coordinates for a required body.
23    MissingCoordinates {
24        /// Human-readable label of the body that was missing (e.g. `"Sun"`).
25        body_label: &'static str,
26        /// The Julian Day at which coordinates were requested.
27        julian_day: f64,
28    },
29}
30
31impl fmt::Display for EclipseError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            EclipseError::OutOfWindow { julian_day } => write!(
35                f,
36                "instant JD {julian_day} is outside the supported 1900–2100 CE window \
37                 (JD {WINDOW_START_JD}..={WINDOW_END_JD})"
38            ),
39            EclipseError::Backend(message) => write!(f, "backend error: {message}"),
40            EclipseError::MissingCoordinates {
41                body_label,
42                julian_day,
43            } => write!(
44                f,
45                "backend returned no ecliptic coordinates for {body_label} at JD {julian_day}"
46            ),
47        }
48    }
49}
50
51impl std::error::Error for EclipseError {}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn out_of_window_message_names_the_julian_day() {
59        let err = EclipseError::OutOfWindow {
60            julian_day: 2_400_000.5,
61        };
62        assert!(err.to_string().contains("2400000.5"));
63        assert!(err.to_string().contains("1900"));
64    }
65
66    #[test]
67    fn window_constants_match_1900_2100() {
68        assert_eq!(WINDOW_START_JD, 2_415_020.5);
69        assert_eq!(WINDOW_END_JD, 2_488_069.5);
70    }
71}