use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Position {
pub(crate) value: f64,
}
impl Position {
pub const ZERO: Self = Self { value: 0.0 };
#[must_use]
pub const fn value(&self) -> f64 {
self.value
}
#[must_use]
pub fn distance_to(self, other: Self) -> f64 {
(self.value - other.value).abs()
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:.2}m", self.value)
}
}
impl From<f64> for Position {
fn from(value: f64) -> Self {
debug_assert!(
value.is_finite(),
"Position value must be finite, got {value}"
);
Self { value }
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Velocity {
pub(crate) value: f64,
}
impl Velocity {
pub const ZERO: Self = Self { value: 0.0 };
#[must_use]
pub const fn value(&self) -> f64 {
self.value
}
#[must_use]
pub const fn speed(self) -> f64 {
self.value.abs()
}
}
impl fmt::Display for Velocity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:.2}m/s", self.value)
}
}
impl From<f64> for Velocity {
fn from(value: f64) -> Self {
debug_assert!(
value.is_finite(),
"Velocity value must be finite, got {value}"
);
Self { value }
}
}