@@ -75,6 +75,13 @@ const LN_KIB: f64 = 6.931471806;
7575/// `ln(1000) ~= 6.908`
7676const LN_KB : f64 = 6.907755279 ;
7777
78+ #[ derive( Debug , Clone , Default ) ]
79+ pub enum Format {
80+ #[ default]
81+ IEC ,
82+ SI ,
83+ }
84+
7885pub fn kb < V : Into < u64 > > ( size : V ) -> u64 {
7986 size. into ( ) * KB
8087}
@@ -187,17 +194,27 @@ impl ByteSize {
187194 }
188195}
189196
190- pub fn to_string ( bytes : u64 , si_prefix : bool ) -> String {
191- let unit = if si_prefix { KB } else { KIB } ;
192- let unit_base = if si_prefix { LN_KB } else { LN_KIB } ;
197+ pub fn to_string ( bytes : u64 , si_unit : bool ) -> String {
198+ to_string_format ( bytes, if si_unit { Format :: SI } else { Format :: IEC } )
199+ }
200+
201+ pub fn to_string_format ( bytes : u64 , format : Format ) -> String {
202+ let unit = match format {
203+ Format :: IEC => KIB ,
204+ Format :: SI => KB ,
205+ } ;
206+ let unit_base = match format {
207+ Format :: IEC => LN_KIB ,
208+ Format :: SI => LN_KB ,
209+ } ;
193210
194- let unit_prefix = match si_prefix {
195- true => UNITS_SI . as_bytes ( ) ,
196- false => UNITS_IEC . as_bytes ( ) ,
211+ let unit_prefix = match format {
212+ Format :: IEC => UNITS_IEC . as_bytes ( ) ,
213+ Format :: SI => UNITS_SI . as_bytes ( ) ,
197214 } ;
198- let unit_suffix = match si_prefix {
199- true => "B " ,
200- false => "iB " ,
215+ let unit_suffix = match format {
216+ Format :: IEC => "iB " ,
217+ Format :: SI => "B " ,
201218 } ;
202219
203220 if bytes < unit {
@@ -220,7 +237,7 @@ pub fn to_string(bytes: u64, si_prefix: bool) -> String {
220237
221238impl Display for ByteSize {
222239 fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
223- f. pad ( & to_string ( self . 0 , false ) )
240+ f. pad ( & to_string_format ( self . 0 , Format :: IEC ) )
224241 }
225242}
226243
0 commit comments