humfmt 0.5.0

Ergonomic human-readable formatting toolkit for Rust
Documentation
use core::fmt;

use crate::common::numeric::NumericValue;
use crate::locale::{English, Locale};

use super::{format::format_number, NumberOptions};

/// `Display` wrapper for compact number formatting (e.g. `"15.3K"`).
///
/// Instances of this type are created via [`crate::number`] and [`crate::number_with`].
///
/// This type does not allocate by itself; allocation only happens if the caller
/// requests an owned `String`.
#[derive(Copy, Clone, Debug)]
pub struct NumberDisplay<L: Locale = English> {
    value: NumericValue,
    options: NumberOptions<L>,
}

impl<L: Locale> NumberDisplay<L> {
    pub(crate) fn new(value: NumericValue, options: NumberOptions<L>) -> Self {
        Self { value, options }
    }
}

impl<L: Locale> fmt::Display for NumberDisplay<L> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        format_number(f, self.value, &self.options)
    }
}