use std::cmp::Ordering;
#[derive(Clone, Copy)]
#[cfg_attr(feature = "serde", derive(::serde::Deserialize, ::serde::Serialize))]
pub struct Position(pub(in super::super) f64);
impl std::fmt::Debug for Position {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl PartialEq for Position {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for Position {}
impl PartialOrd for Position {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Position {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.total_cmp(&other.0)
}
}
impl Position {
pub(crate) const LOWER: f64 = 0.0;
pub(crate) const UPPER: f64 = 32767.0;
pub fn between(left: Option<Position>, right: Option<Position>) -> Self {
Self(
(left.map(|p| p.0).unwrap_or(Position::LOWER)
+ right.map(|p| p.0).unwrap_or(Position::UPPER))
/ 2.0,
)
}
pub fn from_raw(value: f64) -> Option<Position> {
(Position::LOWER..=Position::UPPER)
.contains(&value)
.then_some(Self(value))
}
pub fn as_raw(&self) -> f64 {
self.0
}
}