humanize_proper 0.1.2

Dependency-free, no_std formatting of numbers with SI/binary unit prefixes and of durations
Documentation
//! Input conversion for the number formatter.

/// A number handed to the formatter, kept in the widest lossless form for its kind.
///
/// This type is an implementation detail of [`ToValue`] and carries no stability guarantees.
#[doc(hidden)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Value {
    Uint(u128),
    Int(i128),
    Float(f64),
}

/// Types that can be formatted by [`crate::NumFmt`].
///
/// Implemented for all primitive integer types and for `f32` / `f64`.
pub trait ToValue {
    /// Converts to the widest lossless representation of this number's kind.
    fn to_value(self) -> Value;
}

macro_rules! impl_uint {
    ($($t:ty),*) => {$(
        impl ToValue for $t {
            fn to_value(self) -> Value {
                Value::Uint(self as u128)
            }
        }
    )*};
}

macro_rules! impl_int {
    ($($t:ty),*) => {$(
        impl ToValue for $t {
            fn to_value(self) -> Value {
                Value::Int(self as i128)
            }
        }
    )*};
}

impl_uint!(u8, u16, u32, u64, u128, usize);
impl_int!(i8, i16, i32, i64, i128, isize);

impl ToValue for f32 {
    fn to_value(self) -> Value {
        Value::Float(self as f64)
    }
}

impl ToValue for f64 {
    fn to_value(self) -> Value {
        Value::Float(self)
    }
}

impl ToValue for Value {
    fn to_value(self) -> Value {
        self
    }
}