elevator_core/components/
position.rs1use serde::{Deserialize, Serialize};
4use std::fmt;
5
6#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
18pub struct Position {
19 pub(crate) value: f64,
21}
22
23impl Position {
24 pub const ZERO: Self = Self { value: 0.0 };
26
27 #[must_use]
29 pub const fn value(&self) -> f64 {
30 self.value
31 }
32
33 #[must_use]
35 pub fn distance_to(self, other: Self) -> f64 {
36 (self.value - other.value).abs()
37 }
38}
39
40impl fmt::Display for Position {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 write!(f, "{:.2}m", self.value)
43 }
44}
45
46impl From<f64> for Position {
47 fn from(value: f64) -> Self {
48 debug_assert!(
49 value.is_finite(),
50 "Position value must be finite, got {value}"
51 );
52 Self { value }
53 }
54}
55
56#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
70pub struct Velocity {
71 pub(crate) value: f64,
73}
74
75impl Velocity {
76 pub const ZERO: Self = Self { value: 0.0 };
78
79 #[must_use]
81 pub const fn value(&self) -> f64 {
82 self.value
83 }
84
85 #[must_use]
87 pub const fn speed(self) -> f64 {
88 self.value.abs()
89 }
90}
91
92impl fmt::Display for Velocity {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 write!(f, "{:.2}m/s", self.value)
95 }
96}
97
98impl From<f64> for Velocity {
99 fn from(value: f64) -> Self {
100 debug_assert!(
101 value.is_finite(),
102 "Velocity value must be finite, got {value}"
103 );
104 Self { value }
105 }
106}