Skip to main content

elevator_core/components/
position.rs

1//! Position and velocity components along the shaft axis.
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6/// Position along the shaft axis.
7///
8/// # Display
9///
10/// Formats as a compact distance string:
11///
12/// ```
13/// # use elevator_core::components::Position;
14/// let pos = Position::from(4.5);
15/// assert_eq!(format!("{pos}"), "4.50m");
16/// ```
17#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
18pub struct Position {
19    /// Absolute position value.
20    pub(crate) value: f64,
21}
22
23impl Position {
24    /// Absolute position value.
25    #[must_use]
26    pub const fn value(&self) -> f64 {
27        self.value
28    }
29}
30
31impl fmt::Display for Position {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        write!(f, "{:.2}m", self.value)
34    }
35}
36
37impl From<f64> for Position {
38    fn from(value: f64) -> Self {
39        debug_assert!(
40            value.is_finite(),
41            "Position value must be finite, got {value}"
42        );
43        Self { value }
44    }
45}
46
47/// Velocity along the shaft axis (signed: +up, -down).
48///
49/// # Display
50///
51/// Formats as a compact speed string:
52///
53/// ```
54/// # use elevator_core::components::Velocity;
55/// let vel = Velocity::from(1.2);
56/// assert_eq!(format!("{vel}"), "1.20m/s");
57/// let stopped = Velocity::from(0.0);
58/// assert_eq!(format!("{stopped}"), "0.00m/s");
59/// ```
60#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
61pub struct Velocity {
62    /// Signed velocity value.
63    pub(crate) value: f64,
64}
65
66impl Velocity {
67    /// Signed velocity value.
68    #[must_use]
69    pub const fn value(&self) -> f64 {
70        self.value
71    }
72}
73
74impl fmt::Display for Velocity {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        write!(f, "{:.2}m/s", self.value)
77    }
78}
79
80impl From<f64> for Velocity {
81    fn from(value: f64) -> Self {
82        debug_assert!(
83            value.is_finite(),
84            "Velocity value must be finite, got {value}"
85        );
86        Self { value }
87    }
88}