mirror of
https://github.com/LeopoldArkham/humansize
synced 2024-11-22 19:53:08 +00:00
Fixing all tets and some docs
This commit is contained in:
parent
48c33b6134
commit
b8464e0177
6 changed files with 55 additions and 44 deletions
|
@ -10,9 +10,9 @@ Humansize lets you easily represent file sizes in a human-friendly format.
|
|||
You can specify your own formatting style or pick among the three defaults provided
|
||||
by the library:
|
||||
|
||||
* Decimal (Multiples of 1000, `KB` units)
|
||||
* Binary (Multiples of 1024, `KiB` units)
|
||||
* Windows/Conventional (Multiples of 1024, `KB` units)
|
||||
* Decimal (kilo = 1000, unit format is `kB`)
|
||||
* Binary (kilo = 1024, unit format is `KiB`)
|
||||
* Windows/Conventional (kilo = 1024, unit format is `kB`)
|
||||
|
||||
## How to use it
|
||||
|
||||
|
|
|
@ -1,28 +1,27 @@
|
|||
extern crate humansize;
|
||||
use humansize::{options as opts, FileSize};
|
||||
use humansize::{format_size, format_size_i, FormatSizeOptions, Kilo, FixedAt, DECIMAL};
|
||||
|
||||
fn main() {
|
||||
// Declare a fully custom option struct
|
||||
let custom_options = opts::FormatSizeOptions {
|
||||
divider: opts::Kilo::Binary,
|
||||
units: opts::Kilo::Decimal,
|
||||
let custom_options = FormatSizeOptions {
|
||||
kilo: Kilo::Binary,
|
||||
units: Kilo::Decimal,
|
||||
decimal_places: 3,
|
||||
decimal_zeroes: 1,
|
||||
fixed_at: opts::FixedAt::No,
|
||||
fixed_at: FixedAt::No,
|
||||
long_units: true,
|
||||
space: false,
|
||||
suffix: "",
|
||||
allow_negative: true,
|
||||
};
|
||||
|
||||
// Then use it
|
||||
println!("{}", 3024.file_size(custom_options).unwrap());
|
||||
println!("{}", format_size(3024usize, custom_options));
|
||||
|
||||
// Or use only some custom parameters and adopt the rest from an existing config
|
||||
let semi_custom_options = opts::FormatSizeOptions {
|
||||
let semi_custom_options = FormatSizeOptions {
|
||||
decimal_zeroes: 3,
|
||||
..opts::DECIMAL
|
||||
..DECIMAL
|
||||
};
|
||||
|
||||
println!("{}", 1000.file_size(semi_custom_options).unwrap());
|
||||
println!("{}", format_size_i(1000, semi_custom_options));
|
||||
}
|
23
src/lib.rs
23
src/lib.rs
|
@ -6,9 +6,9 @@ Humansize lets you easily represent file sizes in a human-friendly format.
|
|||
You can specify your own formatting style, pick among the three defaults provided
|
||||
by the library:
|
||||
|
||||
* Decimal (Multiples of 1000, `KB` units)
|
||||
* Binary (Multiples of 1024, `KiB` units)
|
||||
* Conventional (Multiples of 1024, `KB` units)
|
||||
* Decimal (kilo = 1000, unit format is `kB`)
|
||||
* Binary (kilo = 1024, unit format is `KiB`)
|
||||
* Windows/Conventional (kilo = 1024, unit format is `kB`)
|
||||
|
||||
## How to use it
|
||||
|
||||
|
@ -18,15 +18,15 @@ provided by the options module.
|
|||
|
||||
```rust
|
||||
extern crate humansize;
|
||||
use humansize::FileSize;
|
||||
use humansize::format_size;
|
||||
|
||||
fn main() {
|
||||
let size = 1000;
|
||||
println!("Size is {}", size.file_size(humansize::DECIMAL).unwrap());
|
||||
let size = 1000usize;
|
||||
println!("Size is {}", format_size(size, humansize::DECIMAL));
|
||||
|
||||
println!("Size is {}", size.file_size(humansize::BINARY).unwrap());
|
||||
println!("Size is {}", format_size(size, humansize::BINARY));
|
||||
|
||||
println!("Size is {}", size.file_size(humansize::CONVENTIONAL).unwrap());
|
||||
println!("Size is {}", format_size(size, humansize::CONVENTIONAL));
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -72,17 +72,14 @@ impl<V: ToF64, O: AsRef<FormatSizeOptions>> IFormatter<V, O> {
|
|||
impl<T: ToF64, O: AsRef<FormatSizeOptions>> core::fmt::Display for IFormatter<T, O> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
||||
let opts = self.options.as_ref();
|
||||
let divider = match opts.divider {
|
||||
Kilo::Decimal => 1000.0,
|
||||
Kilo::Binary => 1024.0,
|
||||
};
|
||||
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 size >= divider {
|
||||
while fabs(size) >= divider {
|
||||
size /= divider;
|
||||
scale_idx += 1;
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use super::{FormatSizeOptions, FixedAt, Kilo};
|
||||
|
||||
/// Options to display sizes in the binary format.
|
||||
/// Options to display sizes in the SI format.
|
||||
pub const BINARY: FormatSizeOptions = FormatSizeOptions {
|
||||
divider: Kilo::Binary,
|
||||
kilo: Kilo::Binary,
|
||||
units: Kilo::Binary,
|
||||
decimal_places: 2,
|
||||
decimal_zeroes: 0,
|
||||
|
@ -12,9 +12,9 @@ pub const BINARY: FormatSizeOptions = FormatSizeOptions {
|
|||
suffix: "",
|
||||
};
|
||||
|
||||
/// Options to display sizes in the decimal format.
|
||||
/// Options to display sizes in the SI (decimal) format.
|
||||
pub const DECIMAL: FormatSizeOptions = FormatSizeOptions {
|
||||
divider: Kilo::Decimal,
|
||||
kilo: Kilo::Decimal,
|
||||
units: Kilo::Decimal,
|
||||
decimal_places: 2,
|
||||
decimal_zeroes: 0,
|
||||
|
@ -25,9 +25,9 @@ pub const DECIMAL: FormatSizeOptions = FormatSizeOptions {
|
|||
};
|
||||
|
||||
/// Options to display sizes in the "conventional" format.
|
||||
/// This 1024 as the value of the `Kilo`, but displays decimal-style units (`KB`, not `KiB`).
|
||||
/// This 1024 as the value of the `Kilo`, but displays decimal-style units (`kB`, not `KiB`).
|
||||
pub const CONVENTIONAL: FormatSizeOptions = FormatSizeOptions {
|
||||
divider: Kilo::Binary,
|
||||
kilo: Kilo::Binary,
|
||||
units: Kilo::Decimal,
|
||||
decimal_places: 2,
|
||||
decimal_zeroes: 0,
|
||||
|
|
|
@ -13,6 +13,15 @@ pub enum Kilo {
|
|||
Binary,
|
||||
}
|
||||
|
||||
impl Kilo {
|
||||
pub(crate) fn value(&self) -> f64 {
|
||||
match self {
|
||||
Kilo::Decimal => 1000.0,
|
||||
Kilo::Binary => 1024.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
/// Forces a certain representation of the resulting file size.
|
||||
pub enum FixedAt {
|
||||
|
@ -32,7 +41,7 @@ pub enum FixedAt {
|
|||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct FormatSizeOptions {
|
||||
/// The scale (binary/decimal) to divide against.
|
||||
pub divider: Kilo,
|
||||
pub kilo: Kilo,
|
||||
|
||||
/// The unit set to display.
|
||||
pub units: Kilo,
|
||||
|
@ -46,13 +55,13 @@ pub struct FormatSizeOptions {
|
|||
/// Whether to force a certain representation and if so, which one.
|
||||
pub fixed_at: FixedAt,
|
||||
|
||||
/// Whether to use the full suffix or its abbreviation.
|
||||
/// Whether to use the full unit (e.g. `Kilobyte`) or its abbreviation (`kB`).
|
||||
pub long_units: bool,
|
||||
|
||||
/// Whether to place a space between value and units.
|
||||
pub space: bool,
|
||||
|
||||
/// An optional suffix which will be appended after the unit.
|
||||
/// An optional suffix which will be appended after the unit. Useful to represent speeds (e.g. `1 kB/s)
|
||||
pub suffix: &'static str,
|
||||
}
|
||||
|
||||
|
|
|
@ -7,17 +7,17 @@ fn test_sizes() {
|
|||
assert_eq!(format_size(0u32, BINARY), "0 B");
|
||||
assert_eq!(format_size(999u32, BINARY), "999 B");
|
||||
assert_eq!(format_size(1000u32, BINARY), "1000 B");
|
||||
assert_eq!(format_size(1000u32, DECIMAL), "1 KB");
|
||||
assert_eq!(format_size(1000u32, DECIMAL), "1 kB");
|
||||
assert_eq!(format_size(1023u32, BINARY), "1023 B");
|
||||
assert_eq!(format_size(1023u32, DECIMAL), "1.02 KB");
|
||||
assert_eq!(format_size(1023u32, DECIMAL), "1.02 kB");
|
||||
assert_eq!(format_size(1024u32, BINARY), "1 KiB");
|
||||
assert_eq!(format_size(1024u32, CONVENTIONAL), "1 KB");
|
||||
assert_eq!(format_size(1024u32, CONVENTIONAL), "1 kB");
|
||||
|
||||
let semi_custom_options = FormatSizeOptions {
|
||||
space: false,
|
||||
..DECIMAL
|
||||
};
|
||||
assert_eq!(format_size(1000u32, semi_custom_options), "1KB");
|
||||
assert_eq!(format_size(1000u32, semi_custom_options), "1kB");
|
||||
|
||||
let semi_custom_options2 = FormatSizeOptions {
|
||||
suffix: "/s",
|
||||
|
@ -30,7 +30,7 @@ fn test_sizes() {
|
|||
space: false,
|
||||
..DECIMAL
|
||||
};
|
||||
assert_eq!(format_size(1000u32, semi_custom_options3), "1KB/day");
|
||||
assert_eq!(format_size(1000u32, semi_custom_options3), "1kB/day");
|
||||
|
||||
let semi_custom_options4 = FormatSizeOptions {
|
||||
fixed_at: FixedAt::Byte,
|
||||
|
@ -47,6 +47,11 @@ fn test_sizes() {
|
|||
"16196.26 KiB"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
format_size_i(-16584975, semi_custom_options5),
|
||||
"-16196.26 KiB"
|
||||
);
|
||||
|
||||
let semi_custom_options6 = FormatSizeOptions {
|
||||
fixed_at: FixedAt::Tera,
|
||||
decimal_places: 10,
|
||||
|
@ -62,9 +67,9 @@ fn test_sizes() {
|
|||
};
|
||||
assert_eq!
|
||||
((format_size_i(-5500, &semi_custom_options7)),
|
||||
"-5.50 KB"
|
||||
"-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");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -77,6 +82,7 @@ fn use_custom_option_struct_twice() {
|
|||
assert_eq!(format_size(1500u32, &options), "1.50 Kilobyte",);
|
||||
|
||||
assert_eq!(format_size(2500u32, &options), "2.50 Kilobytes",);
|
||||
assert_eq!(format_size_i(-2500000, &options), "-2.50 Megabytes",);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue