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;
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));
}
}

View file

@ -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

View file

@ -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<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 {
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 {
move |val | -> String {
format_size_i(val, &options)
}
move |val| -> String { format_size_i(val, &options) }
}
pub fn make_format<T: ToF64 + Unsigned>(options: impl AsRef<FormatSizeOptions>) -> impl Fn(T) -> String {
make_format_i(options)
}
pub fn make_format<T: ToF64 + Unsigned>(
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};
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<T: ToF64, O: AsRef<FormatSizeOptions>> {
pub value: T,
@ -65,7 +65,7 @@ pub struct IFormatter<T: ToF64, O: AsRef<FormatSizeOptions>> {
impl<V: ToF64, O: AsRef<FormatSizeOptions>> IFormatter<V, O> {
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 {
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<T: ToF64, O: AsRef<FormatSizeOptions>> core::fmt::Display for IFormatter<T,
(Kilo::Decimal, false) => 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<FormatSizeOptions>> From<&'a Formatter<U, O>> for IFormatter<U, &'a O> {
fn from(source: & 'a Formatter<U, O>) -> Self {
IFormatter{value: source.value, options: &source.options}
impl<'a, U: ToF64 + Unsigned + Copy, O: AsRef<FormatSizeOptions>> From<&'a Formatter<U, O>>
for IFormatter<U, &'a O>
{
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> {
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 {
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.
pub const BINARY: FormatSizeOptions = FormatSizeOptions {

View file

@ -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,
}
}
}

View file

@ -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);
impl_unsigned!(for usize u8 u16 u32 u64);

View file

@ -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]