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    fn __str__(&self) -> String {
70        format!("{self}")
71    }
72
73    fn __repr__(&self) -> String {
74        format!("{self} @ {self:p}")
75    }
76
77    /// Decodes an ASN.1 DER encoded byte array into a Mass object.
78    ///
79    /// :type data: bytes
80    /// :rtype: Mass
81    #[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    /// Encodes this Mass object into an ASN.1 DER encoded byte array.
90    ///
91    /// :rtype: bytes
92    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}