use masonry_core::debug_panic;
#[derive(Clone, Copy, PartialEq)]
pub struct Length {
value: f64,
}
impl std::fmt::Debug for Length {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self}")
}
}
impl std::fmt::Display for Length {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}px", self.value)
}
}
impl Length {
pub const ZERO: Self = Self { value: 0. };
#[track_caller]
pub fn px(value: f64) -> Self {
if value < 0. || !value.is_finite() {
debug_panic!("Invalid length value '{value}'");
return Self::ZERO;
}
Self { value }
}
#[track_caller]
pub const fn const_px(value: f64) -> Self {
if value < 0. || !value.is_finite() {
panic!("Invalid length value");
}
Self { value }
}
pub const fn get(self) -> f64 {
self.value
}
}