pub type Scalar = f64;
pub type Second = Scalar;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Unit {
Second,
MilliSecond,
}
impl Unit {
pub fn short_name(self) -> String {
match self {
Unit::Second => String::from("s"),
Unit::MilliSecond => String::from("ms"),
}
}
pub fn format(self, value: Second) -> String {
match self {
Unit::Second => format!("{:.3}", value),
Unit::MilliSecond => format!("{:.1}", value * 1e3),
}
}
}
#[test]
fn test_unit_short_name() {
assert_eq!("s", Unit::Second.short_name());
assert_eq!("ms", Unit::MilliSecond.short_name());
}
#[test]
fn test_unit_format() {
let value: Second = 123.456789;
assert_eq!("123.457", Unit::Second.format(value));
assert_eq!("123456.8", Unit::MilliSecond.format(value));
}