Skip to main content

nyx_space/dynamics/sequence/
python.rs

1#[cfg(feature = "python")]
2use {
3    super::SpacecraftSequence, crate::Spacecraft, anise::almanac::Almanac, pyo3::prelude::*,
4    std::sync::Arc,
5};
6
7#[cfg(feature = "python")]
8#[pymethods]
9impl SpacecraftSequence {
10    #[new]
11    fn py_new() -> Self {
12        SpacecraftSequence::default()
13    }
14
15    #[classmethod]
16    #[pyo3(name = "from_dhall")]
17    fn py_from_dhall(_cls: &Bound<'_, pyo3::types::PyType>, dhall_str: &str) -> PyResult<Self> {
18        serde_dhall::from_str(dhall_str)
19            .parse()
20            .map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))
21    }
22
23    #[classmethod]
24    #[pyo3(name = "from_yaml")]
25    fn py_from_yaml(_cls: &Bound<'_, pyo3::types::PyType>, yaml_str: &str) -> PyResult<Self> {
26        serde_yml::from_str(yaml_str)
27            .map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))
28    }
29
30    #[pyo3(name = "setup")]
31    fn py_setup(&mut self, py: Python<'_>, almanac: Py<Almanac>) -> PyResult<()> {
32        let almanac_ref = almanac.borrow(py);
33        self.setup(Arc::new(almanac_ref.clone()))
34            .map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))
35    }
36
37    #[pyo3(name = "propagate")]
38    fn py_propagate(
39        &self,
40        py: Python<'_>,
41        state: Spacecraft,
42        until_phase: Option<String>,
43        almanac: Py<Almanac>,
44    ) -> PyResult<Vec<(Option<String>, Vec<Spacecraft>)>> {
45        let almanac_ref = almanac.borrow(py);
46        let trajs = self
47            .propagate(state, until_phase, Arc::new(almanac_ref.clone()))
48            .map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
49
50        let result = trajs
51            .into_iter()
52            .map(|traj| (traj.name, traj.states))
53            .collect();
54        Ok(result)
55    }
56}