humansize/options/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
//! Describes the struct that holds the options needed by the formatting functions.
//! The three most common formats are provided as constants to be used easily
mod defaults;
pub use self::defaults::*;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
/// Holds the standard to use when displaying the size.
pub enum Kilo {
/// The decimal scale and units. SI standard.
Decimal,
/// The binary scale and units.
Binary,
}
impl Default for Kilo {
fn default() -> Self {
Self::Decimal
}
}
impl Kilo {
pub(crate) fn value(&self) -> f64 {
match self {
Kilo::Decimal => 1000.0,
Kilo::Binary => 1024.0,
}
}
}
#[derive(Debug, Copy, Clone)]
/// Forces a certain representation of the resulting file size.
pub enum FixedAt {
Base,
Kilo,
Mega,
Giga,
Tera,
Peta,
Exa,
Zetta,
Yotta,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum BaseUnit {
Bit,
Byte,
}
impl Default for BaseUnit {
fn default() -> Self {
Self::Byte
}
}
/// Holds the options for the `file_size` method.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct FormatSizeOptionsBuilder {
/// Whether the value being formatted represents an amount of bits or bytes.
pub base_unit: BaseUnit,
/// The scale (binary/decimal) to divide against.
pub kilo: Kilo,
/// The unit set to display.
pub units: Kilo,
/// The amount of decimal places to display if the decimal part is non-zero.
pub decimal_places: usize,
/// The amount of zeroes to display if the decimal part is zero.
pub decimal_zeroes: usize,
/// Whether to force a certain representation and if so, which one.
pub fixed_at: Option<FixedAt>,
/// Whether to use the full unit (e.g. `Kilobyte`) or its abbreviation (`kB`).
pub long_units: bool,
/// Whether to place a space between value and units.
pub space_after_value: bool,
/// An optional suffix which will be appended after the unit. Useful to represent speeds (e.g. `1 kB/s)
pub suffix: &'static str,
}
/// Holds the options for the `file_size` method.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct FormatSizeOptions {
/// Whether the value being formatted represents an amount of bits or bytes.
pub base_unit: BaseUnit,
/// The scale (binary/decimal) to divide against.
pub kilo: Kilo,
/// The unit set to display.
pub units: Kilo,
/// The amount of decimal places to display if the decimal part is non-zero.
pub decimal_places: usize,
/// The amount of zeroes to display if the decimal part is zero.
pub decimal_zeroes: usize,
/// Whether to force a certain representation and if so, which one.
pub fixed_at: Option<FixedAt>,
/// Whether to use the full unit (e.g. `Kilobyte`) or its abbreviation (`kB`).
pub long_units: bool,
/// Whether to place a space between value and units.
pub space_after_value: bool,
/// An optional suffix which will be appended after the unit. Useful to represent speeds (e.g. `1 kB/s)
pub suffix: &'static str,
}
impl FormatSizeOptions {
pub fn from(from: FormatSizeOptions) -> FormatSizeOptions {
FormatSizeOptions { ..from }
}
pub fn base_unit(mut self, base_unit: BaseUnit) -> FormatSizeOptions {
self.base_unit = base_unit;
self
}
pub fn kilo(mut self, kilo: Kilo) -> FormatSizeOptions {
self.kilo = kilo;
self
}
pub fn units(mut self, units: Kilo) -> FormatSizeOptions {
self.units = units;
self
}
pub fn decimal_places(mut self, decimal_places: usize) -> FormatSizeOptions {
self.decimal_places = decimal_places;
self
}
pub fn decimal_zeroes(mut self, decimal_zeroes: usize) -> FormatSizeOptions {
self.decimal_zeroes = decimal_zeroes;
self
}
pub fn fixed_at(mut self, fixed_at: Option<FixedAt>) -> FormatSizeOptions {
self.fixed_at = fixed_at;
self
}
pub fn long_units(mut self, long_units: bool) -> FormatSizeOptions {
self.long_units = long_units;
self
}
pub fn space_after_value(mut self, insert_space: bool) -> FormatSizeOptions {
self.space_after_value = insert_space;
self
}
pub fn suffix(mut self, suffix: &'static str) -> FormatSizeOptions {
self.suffix = suffix;
self
}
}
impl AsRef<FormatSizeOptions> for FormatSizeOptions {
fn as_ref(&self) -> &FormatSizeOptions {
self
}
}