byte_unit/byte/adjusted/mod.rs
1mod built_in_traits;
2#[cfg(feature = "rocket")]
3mod rocket_traits;
4#[cfg(feature = "serde")]
5mod serde_traits;
6
7use core::{
8 cmp::Ordering,
9 fmt::{self, Alignment, Display, Formatter, Write},
10};
11
12use super::{Byte, Unit};
13use crate::{common::round_fractional_part_f64, UnitType};
14
15/// Generated from the [`Byte::get_adjusted_unit`](./struct.Byte.html#method.get_adjusted_unit) method or the the [`Byte::get_appropriate_unit`](./struct.Byte.html#method.get_appropriate_unit) method.
16///
17/// For accuracy representation, utilize the `Byte` struct.
18#[derive(Debug, Clone, Copy)]
19pub struct AdjustedByte {
20 pub(crate) value: f64,
21 pub(crate) unit: Unit,
22}
23
24impl PartialEq for AdjustedByte {
25 #[inline]
26 fn eq(&self, other: &AdjustedByte) -> bool {
27 let s = self.get_byte();
28 let o = other.get_byte();
29
30 s.eq(&o)
31 }
32}
33
34impl Eq for AdjustedByte {}
35
36impl PartialOrd for AdjustedByte {
37 #[inline]
38 fn partial_cmp(&self, other: &AdjustedByte) -> Option<Ordering> {
39 Some(self.cmp(other))
40 }
41}
42
43impl Ord for AdjustedByte {
44 #[inline]
45 fn cmp(&self, other: &AdjustedByte) -> Ordering {
46 let s = self.get_byte();
47 let o = other.get_byte();
48
49 s.cmp(&o)
50 }
51}
52
53impl Display for AdjustedByte {
54 /// Formats the value using the given formatter.
55 ///
56 /// # Examples
57 ///
58 /// ```
59 /// use byte_unit::{Byte, Unit};
60 ///
61 /// let byte = Byte::from_u64_with_unit(1555, Unit::KB).unwrap();
62 ///
63 /// let adjusted_byte = byte.get_adjusted_unit(Unit::MB);
64 ///
65 /// assert_eq!("1.555 MB", adjusted_byte.to_string());
66 /// ```
67 ///
68 /// ```
69 /// use byte_unit::{Byte, UnitType};
70 ///
71 /// let byte = Byte::from_u64(10000);
72 ///
73 /// let adjusted_byte_based_2 = byte.get_appropriate_unit(UnitType::Binary);
74 /// let adjusted_byte_based_10 = byte.get_appropriate_unit(UnitType::Decimal);
75 ///
76 /// assert_eq!("9.765625 KiB", format!("{adjusted_byte_based_2}"));
77 /// assert_eq!("10 KB", format!("{adjusted_byte_based_10}"));
78 ///
79 /// // with precision
80 /// assert_eq!("9.77 KiB", format!("{adjusted_byte_based_2:.2}"));
81 /// assert_eq!("10.00 KB", format!("{adjusted_byte_based_10:.2}"));
82 ///
83 /// // without any unnecessary fractional part
84 /// assert_eq!("9.77 KiB", format!("{adjusted_byte_based_2:#.2}"));
85 /// assert_eq!("10 KB", format!("{adjusted_byte_based_10:#.2}"));
86 ///
87 /// // with a width, left alignment
88 /// assert_eq!("9.77 KiB", format!("{adjusted_byte_based_2:10.2}"));
89 /// assert_eq!("10.00 KB", format!("{adjusted_byte_based_10:10.2}"));
90 ///
91 /// // with a width, right alignment
92 /// assert_eq!(" 9.77 KiB", format!("{adjusted_byte_based_2:>10.2}"));
93 /// assert_eq!(" 10.00 KB", format!("{adjusted_byte_based_10:>10.2}"));
94 ///
95 /// // with a width, right alignment, more spaces between the value and the unit
96 /// assert_eq!(" 9.77 KiB", format!("{adjusted_byte_based_2:>+10.2}"));
97 /// assert_eq!(" 10.00 KB", format!("{adjusted_byte_based_10:>+10.2}"));
98 ///
99 /// // no spaces between the value and the unit
100 /// assert_eq!("9.765625KiB", format!("{adjusted_byte_based_2:-}"));
101 /// assert_eq!("10KB", format!("{adjusted_byte_based_10:-}"));
102 /// ```
103 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
104 let Self {
105 value,
106 unit,
107 } = self;
108 let handle_basic_precision = |precision: usize, f: &mut Formatter<'_>| -> fmt::Result {
109 if f.alternate() {
110 let value = round_fractional_part_f64(*value, precision);
111
112 f.write_fmt(format_args!("{value}"))
113 } else if matches!(unit, Unit::Bit | Unit::B) {
114 f.write_fmt(format_args!("{value}"))
115 } else {
116 f.write_fmt(format_args!("{value:.precision$}"))
117 }
118 };
119
120 let space_length = if f.sign_plus() {
121 4 - unit.as_str().len()
122 } else if f.sign_minus() {
123 0
124 } else {
125 1
126 };
127
128 if let Some(mut width) = f.width() {
129 let l = unit.as_str().len() + space_length;
130
131 if let Some(precision) = f.precision() {
132 if width > l + 1 {
133 width -= l;
134
135 let alignment = f.align().unwrap_or(Alignment::Left);
136
137 if f.alternate() {
138 let value = round_fractional_part_f64(*value, precision);
139
140 match alignment {
141 Alignment::Left | Alignment::Center => {
142 f.write_fmt(format_args!("{value:<width$}"))?
143 },
144 Alignment::Right => f.write_fmt(format_args!("{value:>width$}"))?,
145 }
146 } else {
147 match alignment {
148 Alignment::Left | Alignment::Center => {
149 f.write_fmt(format_args!("{value:<width$.precision$}"))?
150 },
151 Alignment::Right => {
152 f.write_fmt(format_args!("{value:>width$.precision$}"))?
153 },
154 }
155 }
156 } else {
157 handle_basic_precision(precision, f)?;
158 }
159 } else if width > l + 1 {
160 width -= l;
161
162 let alignment = f.align().unwrap_or(Alignment::Left);
163
164 match alignment {
165 Alignment::Left | Alignment::Center => {
166 f.write_fmt(format_args!("{value:<width$}"))?
167 },
168 Alignment::Right => f.write_fmt(format_args!("{value:>width$}"))?,
169 }
170 } else {
171 f.write_fmt(format_args!("{value}"))?;
172 }
173 } else if let Some(precision) = f.precision() {
174 handle_basic_precision(precision, f)?;
175 } else {
176 f.write_fmt(format_args!("{value}"))?;
177 }
178
179 for _ in 0..space_length {
180 f.write_char(' ')?;
181 }
182
183 f.write_fmt(format_args!("{unit}"))
184 }
185}
186
187/// Methods for getting values.
188impl AdjustedByte {
189 /// Get the value.
190 #[inline]
191 pub const fn get_value(&self) -> f64 {
192 self.value
193 }
194
195 /// Get the unit.
196 #[inline]
197 pub const fn get_unit(&self) -> Unit {
198 self.unit
199 }
200
201 /// Create a new `Byte` instance from this `AdjustedByte` instance.
202 ///
203 /// # Examples
204 ///
205 /// ```
206 /// use byte_unit::{Byte, Unit};
207 ///
208 /// let byte = Byte::from_u64_with_unit(1555, Unit::KB).unwrap();
209 ///
210 /// let adjusted_byte = byte.get_adjusted_unit(Unit::MB);
211 ///
212 /// let byte_back = adjusted_byte.get_byte();
213 ///
214 /// assert_eq!(byte, byte_back);
215 /// ```
216 ///
217 /// # Points to Note
218 ///
219 /// * The result may not be logically equal to the original `Byte` instance due to the accuracy of floating-point numbers.
220 #[inline]
221 pub fn get_byte(&self) -> Byte {
222 Byte::from_f64_with_unit(self.value, self.unit).unwrap()
223 }
224}
225
226/// Associated functions for generating `AdjustedByte`.
227impl Byte {
228 /// Adjust the unit and value for this `Byte` instance.
229 ///
230 /// # Examples
231 ///
232 /// ```
233 /// use byte_unit::{AdjustedByte, Byte, Unit};
234 ///
235 /// let byte = Byte::parse_str("123KiB", true).unwrap();
236 ///
237 /// let adjusted_byte = byte.get_adjusted_unit(Unit::KB);
238 ///
239 /// assert_eq!("125.952 KB", adjusted_byte.to_string());
240 /// ```
241 ///
242 /// ```
243 /// use byte_unit::{AdjustedByte, Byte, Unit};
244 ///
245 /// let byte = Byte::parse_str("50.84 MB", true).unwrap();
246 ///
247 /// let adjusted_byte = byte.get_adjusted_unit(Unit::MiB);
248 ///
249 /// assert_eq!("48.48480224609375 MiB", adjusted_byte.to_string());
250 /// ```
251 #[inline]
252 pub fn get_adjusted_unit(self, unit: Unit) -> AdjustedByte {
253 let byte_v = self.as_u128();
254
255 let value = match unit {
256 Unit::Bit => (byte_v << 3) as f64,
257 Unit::B => byte_v as f64,
258 _ => byte_v as f64 / unit.as_bytes_u128() as f64,
259 };
260
261 AdjustedByte {
262 value,
263 unit,
264 }
265 }
266
267 /// Find the appropriate unit and value for this `Byte` instance.
268 ///
269 /// # Examples
270 ///
271 /// ```
272 /// use byte_unit::{Byte, UnitType};
273 ///
274 /// let byte = Byte::parse_str("123KiB", true).unwrap();
275 ///
276 /// let adjusted_byte = byte.get_appropriate_unit(UnitType::Decimal);
277 ///
278 /// assert_eq!("125.952 KB", adjusted_byte.to_string());
279 /// ```
280 ///
281 /// ```
282 /// use byte_unit::{Byte, UnitType};
283 ///
284 /// let byte = Byte::parse_str("50.84 MB", true).unwrap();
285 ///
286 /// let adjusted_byte = byte.get_appropriate_unit(UnitType::Binary);
287 ///
288 /// assert_eq!("48.48480224609375 MiB", adjusted_byte.to_string());
289 /// ```
290 pub fn get_appropriate_unit(&self, unit_type: UnitType) -> AdjustedByte {
291 let a = Unit::get_multiples_bytes();
292
293 let (skip, step) = match unit_type {
294 UnitType::Binary => (0, 2),
295 UnitType::Decimal => (1, 2),
296 UnitType::Both => (0, 1),
297 };
298
299 let bytes_v = self.as_u128();
300
301 for unit in a.iter().rev().skip(skip).step_by(step) {
302 if bytes_v >= unit.as_bytes_u128() {
303 return self.get_adjusted_unit(*unit);
304 }
305 }
306
307 self.get_adjusted_unit(Unit::B)
308 }
309}