use crate::locale::Locale;
use crate::RoundingMode;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum ByteUnit {
B = 0,
KB = 1,
MB = 2,
GB = 3,
TB = 4,
PB = 5,
EB = 6,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum Precision {
Decimals(u8),
Significant(u8),
}
#[derive(Copy, Clone, Debug)]
pub struct BytesOptions {
pub(crate) precision: Precision,
pub(crate) rounding: RoundingMode,
pub(crate) binary: bool,
pub(crate) bits: bool,
pub(crate) long_units: bool,
pub(crate) decimal_separator: char,
pub(crate) space: bool,
pub(crate) fixed_precision: bool,
pub(crate) min_unit: ByteUnit,
pub(crate) max_unit: ByteUnit,
}
impl BytesOptions {
#[inline]
pub fn new() -> Self {
Self {
precision: Precision::Decimals(1),
rounding: RoundingMode::HalfUp,
binary: false,
bits: false,
long_units: false,
decimal_separator: '.',
space: false,
fixed_precision: false,
min_unit: ByteUnit::B,
max_unit: ByteUnit::EB,
}
}
#[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 rounding(mut self, mode: RoundingMode) -> Self {
self.rounding = mode;
self
}
#[inline]
pub fn binary(mut self) -> Self {
self.binary = true;
self
}
#[inline]
pub fn bits(mut self, enabled: bool) -> Self {
self.bits = enabled;
self
}
#[inline]
pub fn long_units(mut self) -> Self {
self.long_units = true;
self
}
#[inline]
pub fn space(mut self, enabled: bool) -> Self {
self.space = enabled;
self
}
#[inline]
pub fn decimal_separator(mut self, separator: char) -> Self {
self.decimal_separator = separator;
self
}
#[inline]
pub fn fixed_precision(mut self, yes: bool) -> Self {
self.fixed_precision = yes;
self
}
#[inline]
pub fn min_unit(mut self, unit: ByteUnit) -> Self {
self.min_unit = unit;
self
}
#[inline]
pub fn max_unit(mut self, unit: ByteUnit) -> Self {
self.max_unit = unit;
self
}
#[inline]
pub fn unit(mut self, unit: ByteUnit) -> Self {
self.min_unit = unit;
self.max_unit = unit;
self
}
#[inline]
pub fn locale<L: Locale>(mut self, locale: L) -> Self {
self.decimal_separator = locale.decimal_separator();
self
}
}
impl Default for BytesOptions {
#[inline]
fn default() -> Self {
Self::new()
}
}