use core::fmt;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BackendCapabilities {
pub geocentric: bool,
pub topocentric: bool,
pub apparent: bool,
pub mean: bool,
pub batch: bool,
pub native_sidereal: bool,
}
impl Default for BackendCapabilities {
fn default() -> Self {
Self {
geocentric: true,
topocentric: false,
apparent: true,
mean: true,
batch: true,
native_sidereal: false,
}
}
}
impl BackendCapabilities {
pub fn summary_line(&self) -> String {
format!(
"geocentric={}; topocentric={}; apparent={}; mean={}; batch={}; native_sidereal={}",
self.geocentric,
self.topocentric,
self.apparent,
self.mean,
self.batch,
self.native_sidereal
)
}
pub fn validated_summary_line(&self) -> Result<String, BackendCapabilitiesValidationError> {
self.validate()?;
Ok(self.summary_line())
}
pub fn validate(&self) -> Result<(), BackendCapabilitiesValidationError> {
if !self.geocentric && !self.topocentric {
return Err(BackendCapabilitiesValidationError::MissingPositionMode);
}
if !self.apparent && !self.mean {
return Err(BackendCapabilitiesValidationError::MissingValueMode);
}
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BackendCapabilitiesValidationError {
MissingPositionMode,
MissingValueMode,
}
impl BackendCapabilitiesValidationError {
pub fn summary_line(&self) -> &'static str {
match self {
Self::MissingPositionMode => {
"backend capabilities must support geocentric or topocentric positions"
}
Self::MissingValueMode => "backend capabilities must support mean or apparent output",
}
}
}
impl fmt::Display for BackendCapabilitiesValidationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.summary_line())
}
}
impl std::error::Error for BackendCapabilitiesValidationError {}
impl fmt::Display for BackendCapabilities {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.summary_line())
}
}
#[cfg(test)]
#[path = "capabilities_tests.rs"]
mod tests;