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 const 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 const fn precision(mut self, n: u8) -> Self {
let n = if n > 6 { 6 } else { n };
self.precision = Precision::Decimals(n);
self
}
#[inline]
pub const fn significant_digits(mut self, n: u8) -> Self {
let n = if n < 1 {
1
} else if n > 39 {
39
} else {
n
};
self.precision = Precision::Significant(n);
self
}
#[inline]
pub const fn rounding(mut self, mode: RoundingMode) -> Self {
self.rounding = mode;
self
}
#[inline]
pub const fn binary(mut self) -> Self {
self.binary = true;
self
}
#[inline]
pub const fn bits(mut self, enabled: bool) -> Self {
self.bits = enabled;
self
}
#[inline]
pub const fn long_units(mut self) -> Self {
self.long_units = true;
self
}
#[inline]
pub const fn space(mut self, enabled: bool) -> Self {
self.space = enabled;
self
}
#[inline]
pub const fn decimal_separator(mut self, separator: char) -> Self {
self.decimal_separator = separator;
self
}
#[inline]
pub const fn fixed_precision(mut self, yes: bool) -> Self {
self.fixed_precision = yes;
self
}
#[inline]
pub const fn min_unit(mut self, unit: ByteUnit) -> Self {
self.min_unit = unit;
self
}
#[inline]
pub const fn max_unit(mut self, unit: ByteUnit) -> Self {
self.max_unit = unit;
self
}
#[inline]
pub const fn unit(mut self, unit: ByteUnit) -> Self {
self.min_unit = unit;
self.max_unit = unit;
self
}
}
impl Default for BytesOptions {
#[inline]
fn default() -> Self {
Self::new()
}
}