use std::fmt::{Debug, Display};
use log::error;
use num_traits::ToPrimitive;
use number_prefix::NumberPrefix;
pub struct BinaryBytesOne<T>(pub T)
where
T: ToPrimitive;
impl<T> Display for BinaryBytesOne<T>
where
T: ToPrimitive + Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let num = self.0.to_f64().unwrap_or_else(|| {
error!(
"could not convert number/type {:?} to f64 to display it in bytes, using f64::NAN instead",
self.0
);
f64::NAN
});
match NumberPrefix::binary(num) {
NumberPrefix::Standalone(number) => write!(f, "{number:.0} B"),
NumberPrefix::Prefixed(prefix, number) => write!(f, "{number:.1} {prefix}B"),
}
}
}
pub struct OnOffBool(pub bool);
impl Display for OnOffBool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(if self.0 { "ON" } else { "OFF" })
}
}