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