use std::fmt::Debug;
mod binary;
mod decimal;
#[cfg(test)]
pub(crate) mod test;
pub use binary::Binary;
pub use decimal::Decimal;
pub trait Prefix: Debug + Clone + Copy + PartialEq {
fn base(&self) -> i32;
fn exponent(&self) -> i32;
fn multiplier(&self) -> f64 {
let base = self.base() as f64;
let exp = self.exponent();
base.powi(exp)
}
fn scale_value<T: Into<f64>>(&self, value: T) -> f64 {
let x: f64 = value.into();
x / self.multiplier()
}
fn label(&self) -> &'static str;
}
pub trait PrefixFamily {
type Prefix: Prefix + 'static;
fn unit_prefix() -> Self::Prefix;
fn all_prefixes() -> &'static [&'static Self::Prefix];
fn autoscale(val: f64) -> (f64, Self::Prefix) {
if !val.is_finite() || !val.is_normal() {
return (val, Self::unit_prefix());
}
let pfxs = Self::all_prefixes();
let mut iter = pfxs.iter();
let mut cur = iter.next().unwrap();
while let Some(next) = iter.next() {
if next.scale_value(val).abs() < 1.0 {
break;
} else {
cur = next;
}
}
(cur.scale_value(val), **cur)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Scale<F: PrefixFamily> {
Auto,
Native,
Fixed(F::Prefix),
}
impl From<Decimal> for Scale<Decimal> {
fn from(p: Decimal) -> Scale<Decimal> {
Scale::Fixed(p)
}
}
impl From<Binary> for Scale<Binary> {
fn from(p: Binary) -> Scale<Binary> {
Scale::Fixed(p)
}
}