use crate::Spacecraft;
use crate::cosmic::GuidanceMode;
use crate::dynamics::guidance::Thruster;
use anise::prelude::Orbit;
use anise::structure::spacecraft::{DragData, Mass, SRPData};
use der::{Decode, Encode};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyType};
#[pymethods]
impl Spacecraft {
#[pyo3(signature=(orbit, mass=None, srp=None, drag=None, thruster=None, mode=None))]
#[new]
fn py_new(
orbit: Orbit,
mass: Option<Mass>,
srp: Option<SRPData>,
drag: Option<DragData>,
thruster: Option<Thruster>,
mode: Option<GuidanceMode>,
) -> Self {
Self {
orbit,
thruster,
mass: mass.unwrap_or_default(),
srp: srp.unwrap_or_default(),
drag: drag.unwrap_or_default(),
mode: mode.unwrap_or_default(),
..Default::default()
}
}
#[getter]
fn orbit(&self) -> Orbit {
self.orbit
}
#[getter]
fn mass(&self) -> Mass {
self.mass
}
#[getter]
fn srp(&self) -> SRPData {
self.srp
}
#[getter]
fn drag(&self) -> DragData {
self.drag
}
fn __eq__(&self, other: &Self) -> bool {
self == other
}
fn __str__(&self) -> String {
format!("{self}")
}
fn __repr__(&self) -> String {
format!("{self} @ {self:p}")
}
#[classmethod]
pub fn from_asn1(_cls: &Bound<'_, PyType>, data: &[u8]) -> PyResult<Self> {
match Self::from_der(data) {
Ok(obj) => Ok(obj),
Err(e) => Err(PyValueError::new_err(format!("ASN.1 decoding error: {e}"))),
}
}
pub fn to_asn1<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
let mut buf = Vec::new();
match self.encode_to_vec(&mut buf) {
Ok(_) => Ok(PyBytes::new(py, &buf)),
Err(e) => Err(PyValueError::new_err(format!("ASN.1 encoding error: {e}"))),
}
}
}