use crate::locale::{English, Locale};
use crate::RoundingMode;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum Precision {
Decimals(u8),
Significant(u8),
}
#[derive(Copy, Clone, Debug)]
pub struct NumberOptions<L: Locale = English> {
pub(crate) precision: Precision,
pub(crate) compact: bool,
pub(crate) force_sign: bool,
pub(crate) rounding: RoundingMode,
pub(crate) long_units: bool,
pub(crate) separators: bool,
pub(crate) fixed_precision: bool,
pub(crate) locale: L,
}
impl NumberOptions<English> {
#[inline]
pub fn new() -> Self {
Self {
precision: Precision::Decimals(1),
compact: true,
force_sign: false,
rounding: RoundingMode::HalfUp,
long_units: false,
separators: false,
fixed_precision: false,
locale: English,
}
}
}
impl<L: Locale> Default for NumberOptions<L> {
#[inline]
fn default() -> Self {
Self {
precision: Precision::Decimals(1),
compact: true,
force_sign: false,
rounding: RoundingMode::HalfUp,
long_units: false,
separators: false,
fixed_precision: false,
locale: L::default(),
}
}
}
impl<L: Locale> NumberOptions<L> {
#[inline]
pub fn precision(mut self, n: u8) -> Self {
self.precision = Precision::Decimals(n.min(6));
self
}
#[inline]
pub fn significant_digits(mut self, n: u8) -> Self {
self.precision = Precision::Significant(n.clamp(1, 39));
self
}
#[inline]
pub fn compact(mut self, enabled: bool) -> Self {
self.compact = enabled;
self
}
#[inline]
pub fn force_sign(mut self, yes: bool) -> Self {
self.force_sign = yes;
self
}
#[inline]
pub fn rounding(mut self, mode: RoundingMode) -> Self {
self.rounding = mode;
self
}
#[inline]
pub fn long_units(mut self) -> Self {
self.long_units = true;
self
}
#[inline]
pub fn separators(mut self, yes: bool) -> Self {
self.separators = yes;
self
}
#[inline]
pub fn fixed_precision(mut self, yes: bool) -> Self {
self.fixed_precision = yes;
self
}
#[inline]
pub fn locale<N: Locale>(self, locale: N) -> NumberOptions<N> {
NumberOptions {
precision: self.precision,
compact: self.compact,
force_sign: self.force_sign,
rounding: self.rounding,
long_units: self.long_units,
separators: self.separators,
fixed_precision: self.fixed_precision,
locale,
}
}
}