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
70 fn __eq__(&self, other: &Self) -> bool {
71 self == other
72 }
73
74 fn __str__(&self) -> String {
75 format!("{self}")
76 }
77
78 fn __repr__(&self) -> String {
79 format!("{self} @ {self:p}")
80 }
81
82 #[classmethod]
87 pub fn from_asn1(_cls: &Bound<'_, PyType>, data: &[u8]) -> PyResult<Self> {
88 match Self::from_der(data) {
89 Ok(obj) => Ok(obj),
90 Err(e) => Err(PyValueError::new_err(format!("ASN.1 decoding error: {e}"))),
91 }
92 }
93
94 pub fn to_asn1<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
98 let mut buf = Vec::new();
99 match self.encode_to_vec(&mut buf) {
100 Ok(_) => Ok(PyBytes::new(py, &buf)),
101 Err(e) => Err(PyValueError::new_err(format!("ASN.1 encoding error: {e}"))),
102 }
103 }
104}