humansize/options/
mod.rs

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