diff --git a/examples/custom_options.rs b/examples/custom_options.rs index 5f479f4..ce97b68 100644 --- a/examples/custom_options.rs +++ b/examples/custom_options.rs @@ -1,5 +1,5 @@ extern crate humansize; -use humansize::{format_size, format_size_i, FormatSizeOptions, Kilo, FixedAt, DECIMAL}; +use humansize::{format_size, format_size_i, FixedAt, FormatSizeOptions, Kilo, DECIMAL}; fn main() { // Declare a fully custom option struct @@ -24,4 +24,4 @@ fn main() { }; println!("{}", format_size_i(1000, semi_custom_options)); -} \ No newline at end of file +} diff --git a/examples/sizes.rs b/examples/sizes.rs index 05b2c9f..03eaafd 100644 --- a/examples/sizes.rs +++ b/examples/sizes.rs @@ -1,6 +1,6 @@ extern crate humansize; //Import the trait and the options module -use humansize::{format_size, BINARY, DECIMAL, CONVENTIONAL}; +use humansize::{format_size, BINARY, CONVENTIONAL, DECIMAL}; fn main() { // Call the file_size method on any non-negative integer with the option set you require diff --git a/src/allocating.rs b/src/allocating.rs index b53d25e..edecd9e 100644 --- a/src/allocating.rs +++ b/src/allocating.rs @@ -1,23 +1,29 @@ use alloc::string::String; -use crate::traits::*; use crate::options::FormatSizeOptions; +use crate::traits::*; use crate::IFormatter; pub fn format_size_i(input: impl ToF64, options: impl AsRef) -> String { - format!("{}", IFormatter{value: input, options}) + format!( + "{}", + IFormatter { + value: input, + options + } + ) } pub fn format_size(input: impl ToF64 + Unsigned, options: impl AsRef) -> String { - format_size_i(input, &options) + format_size_i(input, &options) } pub fn make_format_i(options: impl AsRef) -> impl Fn(T) -> String { - move |val | -> String { - format_size_i(val, &options) - } + move |val| -> String { format_size_i(val, &options) } } -pub fn make_format(options: impl AsRef) -> impl Fn(T) -> String { - make_format_i(options) -} \ No newline at end of file +pub fn make_format( + options: impl AsRef, +) -> impl Fn(T) -> String { + make_format_i(options) +} diff --git a/src/lib.rs b/src/lib.rs index e7a19aa..2664dda 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,11 +43,11 @@ use core::f64; use libm::{fabs, modf}; mod options; -pub use options::{FormatSizeOptions, BINARY, CONVENTIONAL, DECIMAL, FixedAt, Kilo}; +pub use options::{FixedAt, FormatSizeOptions, Kilo, BINARY, CONVENTIONAL, DECIMAL}; mod scales; mod traits; -use traits::{ToF64,Unsigned}; +use traits::{ToF64, Unsigned}; #[cfg(not(feature = "no_alloc"))] mod allocating; @@ -56,7 +56,7 @@ pub use allocating::*; fn f64_eq(left: f64, right: f64) -> bool { left == right || fabs(left - right) <= f64::EPSILON - } +} pub struct IFormatter> { pub value: T, @@ -65,7 +65,7 @@ pub struct IFormatter> { impl> IFormatter { pub fn new(value: V, options: O) -> Self { - IFormatter{value, options} + IFormatter { value, options } } } @@ -73,10 +73,10 @@ impl> core::fmt::Display for IFormatter core::fmt::Result { let opts = self.options.as_ref(); let divider = opts.kilo.value(); - + let mut size: f64 = self.value.to_f64(); let mut scale_idx = 0; - + match opts.fixed_at { FixedAt::No => { while fabs(size) >= divider { @@ -96,29 +96,35 @@ impl> core::fmt::Display for IFormatter scales::SCALE_DECIMAL[scale_idx], (Kilo::Decimal, true) => scales::SCALE_DECIMAL_LONG[scale_idx], (Kilo::Binary, false) => scales::SCALE_BINARY[scale_idx], - (Kilo::Binary, true) => scales::SCALE_BINARY_LONG[scale_idx] + (Kilo::Binary, true) => scales::SCALE_BINARY_LONG[scale_idx], }; - + // Remove "s" from the scale if the size is 1.x let (fpart, ipart) = modf(size); - if opts.long_units && f64_eq(ipart, 1.0) { scale = &scale[0 .. scale.len()-1]; } - + if opts.long_units && f64_eq(ipart, 1.0) { + scale = &scale[0..scale.len() - 1]; + } + let places = if f64_eq(fpart, 0.0) { opts.decimal_zeroes } else { opts.decimal_places }; - - let space = if opts.space {" "} else {""}; - + + let space = if opts.space { " " } else { "" }; + write!(f, "{:.*}{}{}{}", places, size, space, scale, opts.suffix) } } - -impl<'a, U: ToF64 + Unsigned + Copy, O: AsRef> From<&'a Formatter> for IFormatter { - fn from(source: & 'a Formatter) -> Self { - IFormatter{value: source.value, options: &source.options} +impl<'a, U: ToF64 + Unsigned + Copy, O: AsRef> From<&'a Formatter> + for IFormatter +{ + fn from(source: &'a Formatter) -> Self { + IFormatter { + value: source.value, + options: &source.options, + } } } @@ -129,12 +135,14 @@ pub struct Formatter> { impl> Formatter { pub fn new(value: V, options: O) -> Self { - Formatter{value, options} + Formatter { value, options } } } -impl + Copy> core::fmt::Display for Formatter { +impl + Copy> core::fmt::Display + for Formatter +{ fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { write!(f, "{}", IFormatter::from(self)) } -} \ No newline at end of file +} diff --git a/src/options/defaults.rs b/src/options/defaults.rs index 8469af6..0f13866 100644 --- a/src/options/defaults.rs +++ b/src/options/defaults.rs @@ -1,4 +1,4 @@ -use super::{FormatSizeOptions, FixedAt, Kilo}; +use super::{FixedAt, FormatSizeOptions, Kilo}; /// Options to display sizes in the SI format. pub const BINARY: FormatSizeOptions = FormatSizeOptions { diff --git a/src/options/mod.rs b/src/options/mod.rs index 81fc25d..2988141 100644 --- a/src/options/mod.rs +++ b/src/options/mod.rs @@ -16,8 +16,8 @@ pub enum Kilo { impl Kilo { pub(crate) fn value(&self) -> f64 { match self { - Kilo::Decimal => 1000.0, - Kilo::Binary => 1024.0, + Kilo::Decimal => 1000.0, + Kilo::Binary => 1024.0, } } } diff --git a/src/traits.rs b/src/traits.rs index 956e82a..f477c98 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1,5 +1,5 @@ pub trait ToF64 { - fn to_f64(&self) -> f64; + fn to_f64(&self) -> f64; } macro_rules! impl_to_f64 { @@ -14,7 +14,6 @@ macro_rules! impl_to_f64 { impl_to_f64!(for usize u8 u16 u32 u64 isize i8 i16 i32 i64); - pub trait Unsigned {} macro_rules! impl_unsigned { @@ -23,4 +22,4 @@ macro_rules! impl_unsigned { )*) } -impl_unsigned!(for usize u8 u16 u32 u64); \ No newline at end of file +impl_unsigned!(for usize u8 u16 u32 u64); diff --git a/tests/test.rs b/tests/test.rs index aacf73d..c804e18 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,5 +1,5 @@ use humansize::{ - format_size,format_size_i, BINARY, DECIMAL, CONVENTIONAL, FormatSizeOptions, FixedAt + format_size, format_size_i, FixedAt, FormatSizeOptions, BINARY, CONVENTIONAL, DECIMAL, }; #[test] @@ -62,13 +62,8 @@ fn test_sizes() { "0.0000139016 TiB" ); - let semi_custom_options7 = FormatSizeOptions { - ..DECIMAL - }; - assert_eq! - ((format_size_i(-5500, &semi_custom_options7)), - "-5.50 kB" - ); + let semi_custom_options7 = FormatSizeOptions { ..DECIMAL }; + assert_eq!((format_size_i(-5500, &semi_custom_options7)), "-5.50 kB"); assert_eq!((format_size(5500u32, &semi_custom_options7)), "5.50 kB"); } @@ -101,10 +96,7 @@ fn pluralization_works() { assert_eq!(format_size(1000000000u32, &options), "1.00 Gigabyte",); - assert_eq!( - format_size_i(1000000000000_i64, &options), - "1.00 Terabyte", - ); + assert_eq!(format_size_i(1000000000000_i64, &options), "1.00 Terabyte",); assert_eq!( format_size_i(1000000000000000_i64, &options), @@ -125,10 +117,7 @@ fn max_value_decimal() { ..DECIMAL }; - assert_eq!( - format_size(std::u64::MAX, &options), - "18.4467441 Exabytes", - ); + assert_eq!(format_size(std::u64::MAX, &options), "18.4467441 Exabytes",); } #[test]