Skip to main content

nyx_space/cosmic/
nyx_python.rs

1/*
2    Nyx, blazing fast astrodynamics
3    Copyright (C) 2018-onwards Christopher Rabotin <christopher.rabotin@gmail.com>
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU Affero General Public License for more details.
14
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19use 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    /// Decodes an ASN.1 DER encoded byte array into a Mass object.
83    ///
84    /// :type data: bytes
85    /// :rtype: Mass
86    #[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    /// Encodes this Mass object into an ASN.1 DER encoded byte array.
95    ///
96    /// :rtype: bytes
97    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}