use serde_derive::{Deserialize, Serialize};
use serde_dhall::StaticType;
use snafu::prelude::*;
use std::str::FromStr;
use url::Url;
#[cfg(feature = "python")]
use pyo3::prelude::*;
use crate::errors::{AlmanacResult, MetaSnafu};
use super::{Almanac, MetaAlmanacError, MetaFile};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, StaticType)]
#[cfg_attr(feature = "python", pyclass)]
#[cfg_attr(feature = "python", pyo3(module = "anise"))]
pub struct MetaAlmanac {
pub files: Vec<MetaFile>,
}
impl MetaAlmanac {
pub fn new(path: &str) -> Result<Self, MetaAlmanacError> {
match serde_dhall::from_file(path).parse::<Self>() {
Err(e) => Err(MetaAlmanacError::ParseDhall {
path: path.to_string(),
err: format!("{e}"),
}),
Ok(me) => Ok(me),
}
}
pub fn process(&mut self, autodelete: bool) -> AlmanacResult<Almanac> {
for (fno, file) in self.files.iter_mut().enumerate() {
file.process(autodelete).context(MetaSnafu {
fno,
file: file.clone(),
})?;
}
let mut ctx = Almanac::default();
for uri in &self.files {
ctx = ctx.load(&uri.uri)?;
}
Ok(ctx)
}
pub fn latest() -> AlmanacResult<Almanac> {
Self::default().process(true)
}
pub fn from_dhall(repr: &str) -> Result<Self, MetaAlmanacError> {
serde_dhall::from_str(repr)
.static_type_annotation()
.parse::<Self>()
.map_err(|e| MetaAlmanacError::ParseDhall {
err: format!("{e}"),
path: "from string representation".to_string(),
})
}
pub fn to_dhall(&self) -> Result<String, MetaAlmanacError> {
serde_dhall::serialize(&self)
.static_type_annotation()
.to_string()
.map_err(|e| MetaAlmanacError::ExportDhall {
err: format!("{e}"),
})
}
}
impl FromStr for MetaAlmanac {
type Err = MetaAlmanacError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_dhall(s)
}
}
#[cfg_attr(feature = "python", pymethods)]
impl MetaAlmanac {
pub fn dumps(&self) -> Result<String, MetaAlmanacError> {
self.to_dhall()
}
}
impl Default for MetaAlmanac {
fn default() -> Self {
let nyx_cloud_stor = Url::parse("http://public-data.nyxspace.com/anise/").unwrap();
let jpl_cloud_stor =
Url::parse("https://naif.jpl.nasa.gov/pub/naif/generic_kernels/").unwrap();
Self {
files: vec![
MetaFile {
uri: nyx_cloud_stor.join("de440s.bsp").unwrap().to_string(),
crc32: Some(0x7286750a),
},
MetaFile {
uri: nyx_cloud_stor.join("v0.7/pck11.pca").unwrap().to_string(),
crc32: Some(0x51f69e46),
},
MetaFile {
uri: nyx_cloud_stor
.join("v0.7/moon_fk_de440.epa")
.unwrap()
.to_string(),
crc32: Some(0x32c8f9d7),
},
MetaFile {
uri: nyx_cloud_stor
.join("moon_pa_de440_200625.bpc")
.unwrap()
.to_string(),
crc32: Some(0xcde5ca7d),
},
MetaFile {
uri: jpl_cloud_stor
.join("pck/earth_latest_high_prec.bpc")
.unwrap()
.to_string(),
crc32: None,
},
],
}
}
}