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 177 178 179 180 181 182
use core::cmp::Ordering; use alloc::fmt::{self, Display, Formatter}; use alloc::string::String; use crate::{get_bytes, Byte, ByteUnit}; #[derive(Debug, Clone, Copy)] /// Generated from the `get_appropriate_unit` and `get_adjusted_unit` methods of a `Byte` object. pub struct AdjustedByte { pub(crate) value: f64, pub(crate) unit: ByteUnit, } impl AdjustedByte { /// Format the `AdjustedByte` object to string. /// /// # Examples /// /// ``` /// extern crate byte_unit; /// /// use byte_unit::{Byte, ByteUnit}; /// /// let byte = Byte::from_unit(1555f64, ByteUnit::KB).unwrap(); /// /// let result = byte.get_appropriate_unit(false).format(3); /// /// assert_eq!("1.555 MB", result); /// ``` /// /// ``` /// extern crate byte_unit; /// /// use byte_unit::{Byte, ByteUnit}; /// /// let byte = Byte::from_unit(1555.2f64, ByteUnit::B).unwrap(); /// /// let result = byte.get_adjusted_unit(ByteUnit::B).format(3); /// /// assert_eq!("1555 B", result); /// ``` #[inline] pub fn format(&self, fractional_digits: usize) -> String { if self.unit == ByteUnit::B { format!("{:.0} B", self.value) } else { format!("{:.*} {}", fractional_digits, self.value, self.unit) } } #[inline] pub fn get_value(&self) -> f64 { self.value } #[inline] pub fn get_unit(&self) -> ByteUnit { self.unit } /// Create a new `Byte` object from this `AdjustedByte` object. **Accuracy** should be taken care of. /// /// ## Examples /// /// ``` /// extern crate byte_unit; /// /// use byte_unit::{Byte, ByteUnit}; /// /// let byte = Byte::from_str("123456789123456").unwrap(); /// let adjusted_byte = byte.get_adjusted_unit(ByteUnit::GB); /// /// assert_eq!(123456.789123456, adjusted_byte.get_value()); /// /// let byte = adjusted_byte.get_byte(); /// let adjusted_byte = byte.get_adjusted_unit(ByteUnit::GB); /// /// assert_eq!(123456.789123, adjusted_byte.get_value()); /// ``` #[inline] pub fn get_byte(&self) -> Byte { let bytes = get_bytes(self.value, self.unit); Byte::from_bytes(bytes) } } impl Display for AdjustedByte { #[inline] fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { if self.unit == ByteUnit::B { f.write_fmt(format_args!("{:.0} B", self.value)) } else { f.write_fmt(format_args!("{:.2} ", self.value))?; Display::fmt(&self.unit, f) } } } impl PartialEq for AdjustedByte { /// Deal with the logical numeric equivalent. /// /// # Examples /// /// ``` /// extern crate byte_unit; /// /// use byte_unit::{Byte, ByteUnit}; /// /// let byte1 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); /// let byte2 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); /// /// assert_eq!(byte1.get_appropriate_unit(false), byte2.get_appropriate_unit(true)); /// ``` /// /// ``` /// extern crate byte_unit; /// /// use byte_unit::{Byte, ByteUnit}; /// /// let byte1 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); /// let byte2 = Byte::from_unit(1f64, ByteUnit::MiB).unwrap(); /// /// assert_eq!(byte1.get_appropriate_unit(true), byte2.get_appropriate_unit(false)); /// ``` #[inline] fn eq(&self, other: &AdjustedByte) -> bool { let s = self.get_byte(); let o = other.get_byte(); s.eq(&o) } } impl Eq for AdjustedByte {} impl PartialOrd for AdjustedByte { #[inline] fn partial_cmp(&self, other: &AdjustedByte) -> Option<Ordering> { let s = self.get_byte(); let o = other.get_byte(); s.partial_cmp(&o) } } impl Ord for AdjustedByte { /// Deal with the logical numeric comparation. /// /// # Examples /// /// ``` /// extern crate byte_unit; /// /// use byte_unit::{Byte, ByteUnit}; /// /// let byte1 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); /// let byte2 = Byte::from_unit(1025f64, ByteUnit::KiB).unwrap(); /// /// assert!(byte1.get_appropriate_unit(false) < byte2.get_appropriate_unit(true)); /// ``` /// /// ``` /// extern crate byte_unit; /// /// use byte_unit::{Byte, ByteUnit}; /// /// let byte1 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); /// let byte2 = Byte::from_unit(1.01f64, ByteUnit::MiB).unwrap(); /// /// assert!(byte1.get_appropriate_unit(true) < byte2.get_appropriate_unit(false)); /// ``` #[inline] fn cmp(&self, other: &AdjustedByte) -> Ordering { let s = self.get_byte(); let o = other.get_byte(); s.cmp(&o) } }