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