byte_unit/byte/
decimal.rs

1use rust_decimal::prelude::*;
2
3use super::Byte;
4use crate::{common::is_zero_remainder_decimal, Unit};
5
6const DECIMAL_EIGHT: Decimal = Decimal::from_parts(8, 0, 0, false, 0);
7
8/// Associated functions for building `Byte` instances using `Decimal`.
9impl Byte {
10    /// Create a new `Byte` instance from a size in bytes.
11    ///
12    /// # Examples
13    ///
14    /// ```
15    /// use byte_unit::Byte;
16    /// use rust_decimal::Decimal;
17    ///
18    /// let byte = Byte::from_decimal(Decimal::from(15000000u64)).unwrap(); // 15 MB
19    /// ```
20    ///
21    /// # Points to Note
22    ///
23    /// * If the input **size** is too large (the maximum is **10<sup>27</sup> - 1** if the `u128` feature is enabled, or **2<sup>64</sup> - 1** otherwise) or not greater than or equal to **0**, this function will return `None`.
24    /// * The fractional part will be rounded up.
25    #[inline]
26    pub fn from_decimal(size: Decimal) -> Option<Self> {
27        if size >= Decimal::ZERO {
28            #[cfg(feature = "u128")]
29            {
30                let size = size.ceil();
31
32                match size.to_u128() {
33                    Some(n) => Self::from_u128(n),
34                    None => None,
35                }
36            }
37
38            #[cfg(not(feature = "u128"))]
39            {
40                let size = size.ceil();
41
42                size.to_u64().map(Self::from_u64)
43            }
44        } else {
45            None
46        }
47    }
48}
49
50/// Associated functions for building `Byte` instances using `Decimal` (with `Unit`).
51impl Byte {
52    /// Create a new `Byte` instance from a size of bytes with a unit.
53    ///
54    /// # Examples
55    ///
56    /// ```
57    /// use byte_unit::{Byte, Unit};
58    /// use rust_decimal::Decimal;
59    ///
60    /// let byte = Byte::from_decimal_with_unit(Decimal::from(15u64), Unit::MB).unwrap(); // 15 MB
61    /// ```
62    ///
63    /// # Points to Note
64    ///
65    /// * If the calculated byte is too large or not greater than or equal to **0**, this function will return `None`.
66    /// * The calculated byte will be rounded up.
67    #[inline]
68    pub fn from_decimal_with_unit(size: Decimal, unit: Unit) -> Option<Self> {
69        let v = {
70            match unit {
71                Unit::Bit => (size / DECIMAL_EIGHT).ceil(),
72                Unit::B => size,
73                _ => size.checked_mul(Decimal::from(unit.as_bytes_u128()))?,
74            }
75        };
76
77        Self::from_decimal(v)
78    }
79}
80
81/// Methods for finding an unit using `Decimal`.
82impl Byte {
83    /// Find the appropriate unit and value that can be used to recover back to this `Byte` precisely.
84    ///
85    /// # Examples
86    ///
87    /// ```
88    /// use byte_unit::{Byte, Unit};
89    ///
90    /// let byte = Byte::from_u64(3670016);
91    ///
92    /// assert_eq!(
93    ///     (3.5f64.try_into().unwrap(), Unit::MiB),
94    ///     byte.get_recoverable_unit(false, 3)
95    /// );
96    /// ```
97    ///
98    /// ```
99    /// use byte_unit::{Byte, Unit};
100    ///
101    /// let byte = Byte::from_u64(437500);
102    ///
103    /// assert_eq!(
104    ///     (3.5f64.try_into().unwrap(), Unit::Mbit),
105    ///     byte.get_recoverable_unit(true, 3)
106    /// );
107    /// ```
108    ///
109    /// ```
110    /// use byte_unit::{Byte, Unit};
111    ///
112    /// let byte = Byte::from_u64(437500);
113    ///
114    /// assert_eq!(
115    ///     (437.5f64.try_into().unwrap(), Unit::KB),
116    ///     byte.get_recoverable_unit(false, 3)
117    /// );
118    /// ```
119    ///
120    /// # Points to Note
121    ///
122    /// * `precision` should be smaller or equal to `26` if the `u128` feature is enabled, otherwise `19`. The typical `precision` is `3`.
123    #[inline]
124    pub fn get_recoverable_unit(
125        self,
126        allow_in_bits: bool,
127        mut precision: usize,
128    ) -> (Decimal, Unit) {
129        let bytes_v = self.as_u128();
130        let bytes_vd = Decimal::from(bytes_v);
131
132        let a = if allow_in_bits { Unit::get_multiples() } else { Unit::get_multiples_bytes() };
133        let mut i = a.len() - 1;
134
135        if precision >= 28 {
136            precision = 28;
137        }
138
139        loop {
140            let unit = a[i];
141
142            let unit_v = unit.as_bytes_u128();
143
144            if bytes_v >= unit_v {
145                let unit_vd = Decimal::from(unit_v);
146
147                if let Some(quotient) = is_zero_remainder_decimal(bytes_vd, unit_vd, precision) {
148                    return (quotient, unit);
149                }
150            }
151
152            if i == 0 {
153                break;
154            }
155
156            i -= 1;
157        }
158
159        (bytes_vd, Unit::B)
160    }
161}