astroceleste_engine/
error.rs1use std::fmt;
2
3use crate::ephemeris::SpkError;
4
5#[derive(Debug, Clone, PartialEq)]
7#[non_exhaustive]
8pub enum EngineError {
9 OutOfRange {
11 jd: f64,
13 coverage: Option<(f64, f64)>,
15 },
16 Ephemeris(SpkError),
18 InvalidInput(String),
20}
21
22impl EngineError {
23 pub fn code(&self) -> &'static str {
25 match self {
26 EngineError::OutOfRange { .. } => "ephemeris_out_of_range",
27 EngineError::Ephemeris(_) => "ephemeris_error",
28 EngineError::InvalidInput(_) => "invalid_input",
29 }
30 }
31}
32
33impl fmt::Display for EngineError {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 match self {
36 EngineError::OutOfRange {
37 jd,
38 coverage: Some((start, end)),
39 } => write!(
40 f,
41 "JD {jd:.1} is outside the loaded ephemeris coverage (JD {start:.1} to {end:.1})"
42 ),
43 EngineError::OutOfRange { jd, coverage: None } => {
44 write!(f, "JD {jd:.1} cannot be computed: no ephemeris loaded")
45 }
46 EngineError::Ephemeris(err) => write!(f, "{err}"),
47 EngineError::InvalidInput(msg) => write!(f, "invalid input: {msg}"),
48 }
49 }
50}
51
52impl std::error::Error for EngineError {}
53
54impl From<SpkError> for EngineError {
55 fn from(err: SpkError) -> Self {
56 EngineError::Ephemeris(err)
57 }
58}