nyx_space/cosmic/
nyx_python.rs1use crate::Spacecraft;
20use crate::cosmic::GuidanceMode;
21use crate::dynamics::guidance::Thruster;
22use anise::prelude::Orbit;
23use anise::structure::spacecraft::{DragData, Mass, SRPData};
24use der::{Decode, Encode};
25use pyo3::exceptions::PyValueError;
26use pyo3::prelude::*;
27use pyo3::types::{PyBytes, PyType};
28
29#[pymethods]
30impl Spacecraft {
31 #[pyo3(signature=(orbit, mass=None, srp=None, drag=None, thruster=None, mode=None))]
32 #[new]
33 fn py_new(
34 orbit: Orbit,
35 mass: Option<Mass>,
36 srp: Option<SRPData>,
37 drag: Option<DragData>,
38 thruster: Option<Thruster>,
39 mode: Option<GuidanceMode>,
40 ) -> Self {
41 Self {
42 orbit,
43 thruster,
44 mass: mass.unwrap_or_default(),
45 srp: srp.unwrap_or_default(),
46 drag: drag.unwrap_or_default(),
47 mode: mode.unwrap_or_default(),
48 ..Default::default()
49 }
50 }
51
52 #[getter]
53 fn orbit(&self) -> Orbit {
54 self.orbit
55 }
56
57 #[getter]
58 fn mass(&self) -> Mass {
59 self.mass
60 }
61 #[getter]
62 fn srp(&self) -> SRPData {
63 self.srp
64 }
65 #[getter]
66 fn drag(&self) -> DragData {
67 self.drag
68 }
69 fn __str__(&self) -> String {
70 format!("{self}")
71 }
72
73 fn __repr__(&self) -> String {
74 format!("{self} @ {self:p}")
75 }
76
77 #[classmethod]
82 pub fn from_asn1(_cls: &Bound<'_, PyType>, data: &[u8]) -> PyResult<Self> {
83 match Self::from_der(data) {
84 Ok(obj) => Ok(obj),
85 Err(e) => Err(PyValueError::new_err(format!("ASN.1 decoding error: {e}"))),
86 }
87 }
88
89 pub fn to_asn1<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
93 let mut buf = Vec::new();
94 match self.encode_to_vec(&mut buf) {
95 Ok(_) => Ok(PyBytes::new(py, &buf)),
96 Err(e) => Err(PyValueError::new_err(format!("ASN.1 encoding error: {e}"))),
97 }
98 }
99}