This commit is contained in:
LeopoldArkham 2022-08-29 14:40:28 +02:00
parent 93a8a5572d
commit bcd848b6f8
8 changed files with 56 additions and 54 deletions

View file

@ -1,5 +1,5 @@
extern crate humansize; 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() { fn main() {
// Declare a fully custom option struct // Declare a fully custom option struct
@ -24,4 +24,4 @@ fn main() {
}; };
println!("{}", format_size_i(1000, semi_custom_options)); println!("{}", format_size_i(1000, semi_custom_options));
} }

View file

@ -1,6 +1,6 @@
extern crate humansize; extern crate humansize;
//Import the trait and the options module //Import the trait and the options module
use humansize::{format_size, BINARY, DECIMAL, CONVENTIONAL}; use humansize::{format_size, BINARY, CONVENTIONAL, DECIMAL};
fn main() { fn main() {
// Call the file_size method on any non-negative integer with the option set you require // Call the file_size method on any non-negative integer with the option set you require

View file

@ -1,23 +1,29 @@
use alloc::string::String; use alloc::string::String;
use crate::traits::*;
use crate::options::FormatSizeOptions; use crate::options::FormatSizeOptions;
use crate::traits::*;
use crate::IFormatter; use crate::IFormatter;
pub fn format_size_i(input: impl ToF64, options: impl AsRef<FormatSizeOptions>) -> String { pub fn format_size_i(input: impl ToF64, options: impl AsRef<FormatSizeOptions>) -> String {
format!("{}", IFormatter{value: input, options}) format!(
"{}",
IFormatter {
value: input,
options
}
)
} }
pub fn format_size(input: impl ToF64 + Unsigned, options: impl AsRef<FormatSizeOptions>) -> String { pub fn format_size(input: impl ToF64 + Unsigned, options: impl AsRef<FormatSizeOptions>) -> String {
format_size_i(input, &options) format_size_i(input, &options)
} }
pub fn make_format_i<T: ToF64>(options: impl AsRef<FormatSizeOptions>) -> impl Fn(T) -> String { pub fn make_format_i<T: ToF64>(options: impl AsRef<FormatSizeOptions>) -> impl Fn(T) -> String {
move |val | -> String { move |val| -> String { format_size_i(val, &options) }
format_size_i(val, &options)
}
} }
pub fn make_format<T: ToF64 + Unsigned>(options: impl AsRef<FormatSizeOptions>) -> impl Fn(T) -> String { pub fn make_format<T: ToF64 + Unsigned>(
make_format_i(options) options: impl AsRef<FormatSizeOptions>,
} ) -> impl Fn(T) -> String {
make_format_i(options)
}

View file

@ -43,11 +43,11 @@ use core::f64;
use libm::{fabs, modf}; use libm::{fabs, modf};
mod options; mod options;
pub use options::{FormatSizeOptions, BINARY, CONVENTIONAL, DECIMAL, FixedAt, Kilo}; pub use options::{FixedAt, FormatSizeOptions, Kilo, BINARY, CONVENTIONAL, DECIMAL};
mod scales; mod scales;
mod traits; mod traits;
use traits::{ToF64,Unsigned}; use traits::{ToF64, Unsigned};
#[cfg(not(feature = "no_alloc"))] #[cfg(not(feature = "no_alloc"))]
mod allocating; mod allocating;
@ -56,7 +56,7 @@ pub use allocating::*;
fn f64_eq(left: f64, right: f64) -> bool { fn f64_eq(left: f64, right: f64) -> bool {
left == right || fabs(left - right) <= f64::EPSILON left == right || fabs(left - right) <= f64::EPSILON
} }
pub struct IFormatter<T: ToF64, O: AsRef<FormatSizeOptions>> { pub struct IFormatter<T: ToF64, O: AsRef<FormatSizeOptions>> {
pub value: T, pub value: T,
@ -65,7 +65,7 @@ pub struct IFormatter<T: ToF64, O: AsRef<FormatSizeOptions>> {
impl<V: ToF64, O: AsRef<FormatSizeOptions>> IFormatter<V, O> { impl<V: ToF64, O: AsRef<FormatSizeOptions>> IFormatter<V, O> {
pub fn new(value: V, options: O) -> Self { pub fn new(value: V, options: O) -> Self {
IFormatter{value, options} IFormatter { value, options }
} }
} }
@ -73,10 +73,10 @@ impl<T: ToF64, O: AsRef<FormatSizeOptions>> core::fmt::Display for IFormatter<T,
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let opts = self.options.as_ref(); let opts = self.options.as_ref();
let divider = opts.kilo.value(); let divider = opts.kilo.value();
let mut size: f64 = self.value.to_f64(); let mut size: f64 = self.value.to_f64();
let mut scale_idx = 0; let mut scale_idx = 0;
match opts.fixed_at { match opts.fixed_at {
FixedAt::No => { FixedAt::No => {
while fabs(size) >= divider { while fabs(size) >= divider {
@ -96,29 +96,35 @@ impl<T: ToF64, O: AsRef<FormatSizeOptions>> core::fmt::Display for IFormatter<T,
(Kilo::Decimal, false) => scales::SCALE_DECIMAL[scale_idx], (Kilo::Decimal, false) => scales::SCALE_DECIMAL[scale_idx],
(Kilo::Decimal, true) => scales::SCALE_DECIMAL_LONG[scale_idx], (Kilo::Decimal, true) => scales::SCALE_DECIMAL_LONG[scale_idx],
(Kilo::Binary, false) => scales::SCALE_BINARY[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 // Remove "s" from the scale if the size is 1.x
let (fpart, ipart) = modf(size); 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) { let places = if f64_eq(fpart, 0.0) {
opts.decimal_zeroes opts.decimal_zeroes
} else { } else {
opts.decimal_places opts.decimal_places
}; };
let space = if opts.space {" "} else {""}; let space = if opts.space { " " } else { "" };
write!(f, "{:.*}{}{}{}", places, size, space, scale, opts.suffix) write!(f, "{:.*}{}{}{}", places, size, space, scale, opts.suffix)
} }
} }
impl<'a, U: ToF64 + Unsigned + Copy, O: AsRef<FormatSizeOptions>> From<&'a Formatter<U, O>>
impl<'a, U: ToF64 + Unsigned + Copy, O: AsRef<FormatSizeOptions>> From<&'a Formatter<U, O>> for IFormatter<U, &'a O> { for IFormatter<U, &'a O>
fn from(source: & 'a Formatter<U, O>) -> Self { {
IFormatter{value: source.value, options: &source.options} fn from(source: &'a Formatter<U, O>) -> Self {
IFormatter {
value: source.value,
options: &source.options,
}
} }
} }
@ -129,12 +135,14 @@ pub struct Formatter<T: ToF64 + Unsigned, O: AsRef<FormatSizeOptions>> {
impl<V: ToF64 + Unsigned, O: AsRef<FormatSizeOptions>> Formatter<V, O> { impl<V: ToF64 + Unsigned, O: AsRef<FormatSizeOptions>> Formatter<V, O> {
pub fn new(value: V, options: O) -> Self { pub fn new(value: V, options: O) -> Self {
Formatter{value, options} Formatter { value, options }
} }
} }
impl<T: ToF64 + Unsigned + Copy, O: AsRef<FormatSizeOptions> + Copy> core::fmt::Display for Formatter<T, O> { impl<T: ToF64 + Unsigned + Copy, O: AsRef<FormatSizeOptions> + Copy> core::fmt::Display
for Formatter<T, O>
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", IFormatter::from(self)) write!(f, "{}", IFormatter::from(self))
} }
} }

View file

@ -1,4 +1,4 @@
use super::{FormatSizeOptions, FixedAt, Kilo}; use super::{FixedAt, FormatSizeOptions, Kilo};
/// Options to display sizes in the SI format. /// Options to display sizes in the SI format.
pub const BINARY: FormatSizeOptions = FormatSizeOptions { pub const BINARY: FormatSizeOptions = FormatSizeOptions {

View file

@ -16,8 +16,8 @@ pub enum Kilo {
impl Kilo { impl Kilo {
pub(crate) fn value(&self) -> f64 { pub(crate) fn value(&self) -> f64 {
match self { match self {
Kilo::Decimal => 1000.0, Kilo::Decimal => 1000.0,
Kilo::Binary => 1024.0, Kilo::Binary => 1024.0,
} }
} }
} }

View file

@ -1,5 +1,5 @@
pub trait ToF64 { pub trait ToF64 {
fn to_f64(&self) -> f64; fn to_f64(&self) -> f64;
} }
macro_rules! impl_to_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); impl_to_f64!(for usize u8 u16 u32 u64 isize i8 i16 i32 i64);
pub trait Unsigned {} pub trait Unsigned {}
macro_rules! impl_unsigned { macro_rules! impl_unsigned {
@ -23,4 +22,4 @@ macro_rules! impl_unsigned {
)*) )*)
} }
impl_unsigned!(for usize u8 u16 u32 u64); impl_unsigned!(for usize u8 u16 u32 u64);

View file

@ -1,5 +1,5 @@
use humansize::{ use humansize::{
format_size,format_size_i, BINARY, DECIMAL, CONVENTIONAL, FormatSizeOptions, FixedAt format_size, format_size_i, FixedAt, FormatSizeOptions, BINARY, CONVENTIONAL, DECIMAL,
}; };
#[test] #[test]
@ -62,13 +62,8 @@ fn test_sizes() {
"0.0000139016 TiB" "0.0000139016 TiB"
); );
let semi_custom_options7 = FormatSizeOptions { let semi_custom_options7 = FormatSizeOptions { ..DECIMAL };
..DECIMAL assert_eq!((format_size_i(-5500, &semi_custom_options7)), "-5.50 kB");
};
assert_eq!
((format_size_i(-5500, &semi_custom_options7)),
"-5.50 kB"
);
assert_eq!((format_size(5500u32, &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(1000000000u32, &options), "1.00 Gigabyte",);
assert_eq!( assert_eq!(format_size_i(1000000000000_i64, &options), "1.00 Terabyte",);
format_size_i(1000000000000_i64, &options),
"1.00 Terabyte",
);
assert_eq!( assert_eq!(
format_size_i(1000000000000000_i64, &options), format_size_i(1000000000000000_i64, &options),
@ -125,10 +117,7 @@ fn max_value_decimal() {
..DECIMAL ..DECIMAL
}; };
assert_eq!( assert_eq!(format_size(std::u64::MAX, &options), "18.4467441 Exabytes",);
format_size(std::u64::MAX, &options),
"18.4467441 Exabytes",
);
} }
#[test] #[test]