#![allow(clippy::wrong_self_convention)]
use pyo3::{Bound, Python, pyclass, pymethods, types::PyComplex};
use std::format;
use std::string::{String, ToString};
use lox_units::{Angle, AngularRate, Distance, Frequency, Power, Pressure, Temperature, Velocity};
fn repr_f64(v: f64) -> String {
let s = v.to_string();
if v.is_finite() && !s.contains('.') {
format!("{s}.0")
} else {
s
}
}
macro_rules! py_unit {
($(($unit:ident, $name:literal, $pyunit:ident $(, { $($extra:tt)* })?)),*) => {
$(
#[pyclass(name = $name, module = "lox_space", frozen, from_py_object)]
#[derive(Clone, Copy)]
pub struct $pyunit(pub $unit);
#[pymethods]
impl $pyunit {
#[new]
pub fn new(value: f64) -> Self {
Self($unit::new(value))
}
pub fn __add__(&self, other: &$pyunit) -> Self {
Self(self.0 + other.0)
}
pub fn __sub__(&self, other: &$pyunit) -> Self {
Self(self.0 - other.0)
}
pub fn __neg__(&self) -> Self {
Self(-self.0)
}
pub fn __mul__(&self, other: f64) -> Self {
Self(other * self.0)
}
pub fn __rmul__(&self, other: f64) -> Self {
Self(other * self.0)
}
pub fn __eq__(&self, other: &$pyunit) -> bool {
f64::from(self.0) == f64::from(other.0)
}
pub fn __getnewargs__(&self) -> (f64,) {
(f64::from(self.0),)
}
pub fn __repr__(&self) -> String {
format!("{}({})", $name, repr_f64(f64::from(self.0)))
}
pub fn __str__(&self) -> String {
self.0.to_string()
}
pub fn __complex__<'py>(&self, py: Python<'py>) -> Bound<'py, PyComplex> {
PyComplex::from_doubles(py, self.0.into(), 0.0)
}
pub fn __float__(&self) -> f64 {
self.0.into()
}
pub fn __int__(&self) -> i64 {
let val: f64 = self.0.into();
val.round_ties_even() as i64
}
$($($extra)*)?
}
)*
};
}
py_unit!(
(Angle, "Angle", PyAngle, {
fn to_radians(&self) -> f64 {
self.0.to_radians()
}
fn to_degrees(&self) -> f64 {
self.0.to_degrees()
}
fn to_arcseconds(&self) -> f64 {
self.0.to_arcseconds()
}
}),
(AngularRate, "AngularRate", PyAngularRate, {
fn to_radians_per_second(&self) -> f64 {
self.0.to_radians_per_second()
}
fn to_degrees_per_second(&self) -> f64 {
self.0.to_degrees_per_second()
}
}),
(Distance, "Distance", PyDistance, {
fn to_meters(&self) -> f64 {
self.0.to_meters()
}
fn to_kilometers(&self) -> f64 {
self.0.to_kilometers()
}
fn to_astronomical_units(&self) -> f64 {
self.0.to_astronomical_units()
}
}),
(Frequency, "Frequency", PyFrequency, {
fn to_hertz(&self) -> f64 {
self.0.to_hertz()
}
fn to_kilohertz(&self) -> f64 {
self.0.to_kilohertz()
}
fn to_megahertz(&self) -> f64 {
self.0.to_megahertz()
}
fn to_gigahertz(&self) -> f64 {
self.0.to_gigahertz()
}
fn to_terahertz(&self) -> f64 {
self.0.to_terahertz()
}
}),
(Power, "Power", PyPower, {
fn to_watts(&self) -> f64 {
self.0.to_watts()
}
fn to_kilowatts(&self) -> f64 {
self.0.to_kilowatts()
}
fn to_dbw(&self) -> f64 {
self.0.to_dbw()
}
}),
(Pressure, "Pressure", PyPressure, {
fn to_hpa(&self) -> f64 {
self.0.to_hpa()
}
fn to_pa(&self) -> f64 {
self.0.to_pa()
}
}),
(Temperature, "Temperature", PyTemperature, {
fn to_kelvin(&self) -> f64 {
self.0.to_kelvin()
}
}),
(Velocity, "Velocity", PyVelocity, {
fn to_meters_per_second(&self) -> f64 {
self.0.to_meters_per_second()
}
fn to_kilometers_per_second(&self) -> f64 {
self.0.to_kilometers_per_second()
}
})
);
use lox_core::elements::GravitationalParameter;
#[pyclass(
name = "GravitationalParameter",
module = "lox_space",
frozen,
from_py_object
)]
#[derive(Clone, Copy)]
pub struct PyGravitationalParameter(pub GravitationalParameter);
#[pymethods]
impl PyGravitationalParameter {
#[new]
pub fn new(value: f64) -> Self {
Self(GravitationalParameter::m3_per_s2(value))
}
#[staticmethod]
fn from_km3_per_s2(value: f64) -> Self {
Self(GravitationalParameter::km3_per_s2(value))
}
fn to_m3_per_s2(&self) -> f64 {
self.0.as_f64()
}
fn to_km3_per_s2(&self) -> f64 {
self.0.as_f64() * 1e-9
}
fn __float__(&self) -> f64 {
self.0.as_f64()
}
fn __eq__(&self, other: &PyGravitationalParameter) -> bool {
self.0.as_f64() == other.0.as_f64()
}
fn __getnewargs__(&self) -> (f64,) {
(self.0.as_f64(),)
}
fn __repr__(&self) -> String {
format!("GravitationalParameter({})", repr_f64(self.0.as_f64()))
}
fn __str__(&self) -> String {
self.0.to_string()
}
}