mirror of
https://github.com/LeopoldArkham/humansize
synced 2024-11-22 11:43:19 +00:00
WIP split files
This commit is contained in:
parent
89e9b0cf50
commit
d8d76060c2
4 changed files with 189 additions and 178 deletions
48
src/file_size_opts/defaults.rs
Normal file
48
src/file_size_opts/defaults.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use file_size_opts::{
|
||||
FileSizeOpts,
|
||||
Kilo,
|
||||
FixedAt,
|
||||
};
|
||||
|
||||
|
||||
/// Options to display sizes in the binary format.
|
||||
pub const BINARY: FileSizeOpts = FileSizeOpts {
|
||||
divider: Kilo::Binary,
|
||||
units: Kilo::Binary,
|
||||
decimal_places: 2,
|
||||
decimal_zeroes: 0,
|
||||
fixed_at: FixedAt::No,
|
||||
long_units: false,
|
||||
space: true,
|
||||
suffix: "",
|
||||
allow_negative: false,
|
||||
};
|
||||
|
||||
|
||||
/// Options to display sizes in the decimal format.
|
||||
pub const DECIMAL: FileSizeOpts = FileSizeOpts {
|
||||
divider: Kilo::Decimal,
|
||||
units: Kilo::Decimal,
|
||||
decimal_places: 2,
|
||||
decimal_zeroes: 0,
|
||||
fixed_at: FixedAt::No,
|
||||
long_units: false,
|
||||
space: true,
|
||||
suffix: "",
|
||||
allow_negative: false,
|
||||
};
|
||||
|
||||
|
||||
/// Options to display sizes in the "conventional" format.
|
||||
/// This 1024 as the value of the `Kilo`, but displays decimal-style units (`KB`, not `KiB`).
|
||||
pub const CONVENTIONAL: FileSizeOpts = FileSizeOpts {
|
||||
divider: Kilo::Binary,
|
||||
units: Kilo::Decimal,
|
||||
decimal_places: 2,
|
||||
decimal_zeroes: 0,
|
||||
fixed_at: FixedAt::No,
|
||||
long_units: false,
|
||||
space: true,
|
||||
suffix: "",
|
||||
allow_negative: false,
|
||||
};
|
73
src/file_size_opts/mod.rs
Normal file
73
src/file_size_opts/mod.rs
Normal file
|
@ -0,0 +1,73 @@
|
|||
|
||||
//! Describes the struct that holds the options needed by the `file_size` method.
|
||||
//! The three most common formats are provided as constants to be used easily
|
||||
|
||||
pub mod defaults;
|
||||
|
||||
pub use self::defaults::*;
|
||||
|
||||
|
||||
|
||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||
/// Holds the standard to use when displying the size.
|
||||
pub enum Kilo {
|
||||
/// The decimal scale and units
|
||||
Decimal,
|
||||
/// The binary scale and units
|
||||
Binary,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
/// Forces a certain representation of the resulting file size.
|
||||
pub enum FixedAt {
|
||||
Byte,
|
||||
Kilo,
|
||||
Mega,
|
||||
Giga,
|
||||
Tera,
|
||||
Peta,
|
||||
Exa,
|
||||
Zetta,
|
||||
Yotta,
|
||||
No,
|
||||
}
|
||||
|
||||
|
||||
/// Holds the options for the `file_size` method.
|
||||
#[derive(Debug)]
|
||||
pub struct FileSizeOpts {
|
||||
/// The scale (binary/decimal) to divide against.
|
||||
pub divider: Kilo,
|
||||
|
||||
/// The unit set to display.
|
||||
pub units: Kilo,
|
||||
|
||||
/// The amount of decimal places to display if the decimal part is non-zero.
|
||||
pub decimal_places: usize,
|
||||
|
||||
/// The amount of zeroes to display if the decimal part is zero.
|
||||
pub decimal_zeroes: usize,
|
||||
|
||||
/// Whether to force a certain representation and if so, which one.
|
||||
pub fixed_at: FixedAt,
|
||||
|
||||
/// Whether to use the full suffix or its abbreveation.
|
||||
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.
|
||||
pub suffix: &'static str,
|
||||
|
||||
/// Whether to allow negative numbers as input. If `False`, negative values will return an error.
|
||||
pub allow_negative: bool,
|
||||
}
|
||||
|
||||
|
||||
impl AsRef<FileSizeOpts> for FileSizeOpts {
|
||||
fn as_ref(&self) -> &FileSizeOpts {
|
||||
self
|
||||
}
|
||||
}
|
179
src/lib.rs
179
src/lib.rs
|
@ -32,118 +32,7 @@
|
|||
//! and pass that to the method. See the `custom_options.rs` file in the example folder.
|
||||
|
||||
mod scales;
|
||||
|
||||
pub mod file_size_opts {
|
||||
//! Describes the struct that holds the options needed by the `file_size` method.
|
||||
//! The three most common formats are provided as constants to be used easily
|
||||
|
||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||
/// Holds the standard to use when displying the size.
|
||||
pub enum Kilo {
|
||||
/// The decimal scale and units
|
||||
Decimal,
|
||||
/// The binary scale and units
|
||||
Binary,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
/// Forces a certain representation of the resulting file size.
|
||||
pub enum FixedAt {
|
||||
Byte,
|
||||
Kilo,
|
||||
Mega,
|
||||
Giga,
|
||||
Tera,
|
||||
Peta,
|
||||
Exa,
|
||||
Zetta,
|
||||
Yotta,
|
||||
No,
|
||||
}
|
||||
|
||||
|
||||
/// Holds the options for the `file_size` method.
|
||||
#[derive(Debug)]
|
||||
pub struct FileSizeOpts {
|
||||
/// The scale (binary/decimal) to divide against.
|
||||
pub divider: Kilo,
|
||||
|
||||
/// The unit set to display.
|
||||
pub units: Kilo,
|
||||
|
||||
/// The amount of decimal places to display if the decimal part is non-zero.
|
||||
pub decimal_places: usize,
|
||||
|
||||
/// The amount of zeroes to display if the decimal part is zero.
|
||||
pub decimal_zeroes: usize,
|
||||
|
||||
/// Whether to force a certain representation and if so, which one.
|
||||
pub fixed_at: FixedAt,
|
||||
|
||||
/// Whether to use the full suffix or its abbreveation.
|
||||
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.
|
||||
pub suffix: &'static str,
|
||||
|
||||
/// Whether to allow negative numbers as input. If `False`, negative values will return an error.
|
||||
pub allow_negative: bool,
|
||||
}
|
||||
|
||||
|
||||
impl AsRef<FileSizeOpts> for FileSizeOpts {
|
||||
fn as_ref(&self) -> &FileSizeOpts {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Options to display sizes in the binary format.
|
||||
pub const BINARY: FileSizeOpts = FileSizeOpts {
|
||||
divider: Kilo::Binary,
|
||||
units: Kilo::Binary,
|
||||
decimal_places: 2,
|
||||
decimal_zeroes: 0,
|
||||
fixed_at: FixedAt::No,
|
||||
long_units: false,
|
||||
space: true,
|
||||
suffix: "",
|
||||
allow_negative: false,
|
||||
};
|
||||
|
||||
|
||||
/// Options to display sizes in the decimal format.
|
||||
pub const DECIMAL: FileSizeOpts = FileSizeOpts {
|
||||
divider: Kilo::Decimal,
|
||||
units: Kilo::Decimal,
|
||||
decimal_places: 2,
|
||||
decimal_zeroes: 0,
|
||||
fixed_at: FixedAt::No,
|
||||
long_units: false,
|
||||
space: true,
|
||||
suffix: "",
|
||||
allow_negative: false,
|
||||
};
|
||||
|
||||
|
||||
/// Options to display sizes in the "conventional" format.
|
||||
/// This 1024 as the value of the `Kilo`, but displays decimal-style units (`KB`, not `KiB`).
|
||||
pub const CONVENTIONAL: FileSizeOpts = FileSizeOpts {
|
||||
divider: Kilo::Binary,
|
||||
units: Kilo::Decimal,
|
||||
decimal_places: 2,
|
||||
decimal_zeroes: 0,
|
||||
fixed_at: FixedAt::No,
|
||||
long_units: false,
|
||||
space: true,
|
||||
suffix: "",
|
||||
allow_negative: false,
|
||||
};
|
||||
}
|
||||
pub mod file_size_opts;
|
||||
|
||||
|
||||
/// The trait for the `file_size`method
|
||||
|
@ -252,69 +141,3 @@ macro_rules! impl_file_size_i {
|
|||
|
||||
impl_file_size_u!(for usize u8 u16 u32 u64);
|
||||
impl_file_size_i!(for isize i8 i16 i32 i64);
|
||||
|
||||
#[test]
|
||||
fn test_sizes() {
|
||||
assert_eq!(0.file_size(BINARY).unwrap(), "0 B");
|
||||
assert_eq!(999.file_size(BINARY).unwrap(), "999 B");
|
||||
assert_eq!(1000.file_size(BINARY).unwrap(), "1000 B");
|
||||
assert_eq!(1000.file_size(DECIMAL).unwrap(), "1 KB");
|
||||
assert_eq!(1023.file_size(BINARY).unwrap(), "1023 B");
|
||||
assert_eq!(1023.file_size(DECIMAL).unwrap(), "1.02 KB");
|
||||
assert_eq!(1024.file_size(BINARY).unwrap(), "1 KiB");
|
||||
assert_eq!(1024.file_size(CONVENTIONAL).unwrap(), "1 KB");
|
||||
|
||||
let semi_custom_options = file_size_opts::FileSizeOpts {
|
||||
space: false,
|
||||
..file_size_opts::DECIMAL
|
||||
};
|
||||
assert_eq!(1000.file_size(semi_custom_options).unwrap(), "1KB");
|
||||
|
||||
let semi_custom_options2 = file_size_opts::FileSizeOpts {
|
||||
suffix: "/s",
|
||||
..file_size_opts::BINARY
|
||||
};
|
||||
assert_eq!(999.file_size(semi_custom_options2).unwrap(), "999 B/s");
|
||||
|
||||
let semi_custom_options3 = file_size_opts::FileSizeOpts {
|
||||
suffix: "/day",
|
||||
space: false,
|
||||
..file_size_opts::DECIMAL
|
||||
};
|
||||
assert_eq!(1000.file_size(semi_custom_options3).unwrap(), "1KB/day");
|
||||
|
||||
let semi_custom_options4 = file_size_opts::FileSizeOpts {
|
||||
fixed_at: file_size_opts::FixedAt::Byte,
|
||||
..file_size_opts::BINARY
|
||||
};
|
||||
assert_eq!(2048.file_size(semi_custom_options4).unwrap(), "2048 B");
|
||||
|
||||
let semi_custom_options5 = file_size_opts::FileSizeOpts {
|
||||
fixed_at: file_size_opts::FixedAt::Kilo,
|
||||
..file_size_opts::BINARY
|
||||
};
|
||||
assert_eq!(
|
||||
16584975.file_size(semi_custom_options5).unwrap(),
|
||||
"16196.26 KiB"
|
||||
);
|
||||
|
||||
let semi_custom_options6 = file_size_opts::FileSizeOpts {
|
||||
fixed_at: file_size_opts::FixedAt::Tera,
|
||||
decimal_places: 10,
|
||||
..file_size_opts::BINARY
|
||||
};
|
||||
assert_eq!(
|
||||
15284975.file_size(semi_custom_options6).unwrap(),
|
||||
"0.0000139016 TiB"
|
||||
);
|
||||
|
||||
let semi_custom_options7 = file_size_opts::FileSizeOpts {
|
||||
allow_negative: true,
|
||||
..file_size_opts::DECIMAL
|
||||
};
|
||||
assert_eq!(
|
||||
(-5500).file_size(&semi_custom_options7).unwrap(),
|
||||
"-5.50 KB"
|
||||
);
|
||||
assert_eq!((5500).file_size(&semi_custom_options7).unwrap(), "5.50 KB");
|
||||
}
|
||||
|
|
67
tests/test.rs
Normal file
67
tests/test.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
use humansize::{file_size_opts, FileSize};
|
||||
|
||||
fn test_sizes() {
|
||||
|
||||
assert_eq!(0.file_size(BINARY).unwrap(), "0 B");
|
||||
assert_eq!(999.file_size(BINARY).unwrap(), "999 B");
|
||||
assert_eq!(1000.file_size(BINARY).unwrap(), "1000 B");
|
||||
assert_eq!(1000.file_size(DECIMAL).unwrap(), "1 KB");
|
||||
assert_eq!(1023.file_size(BINARY).unwrap(), "1023 B");
|
||||
assert_eq!(1023.file_size(DECIMAL).unwrap(), "1.02 KB");
|
||||
assert_eq!(1024.file_size(BINARY).unwrap(), "1 KiB");
|
||||
assert_eq!(1024.file_size(CONVENTIONAL).unwrap(), "1 KB");
|
||||
|
||||
let semi_custom_options = file_size_opts::FileSizeOpts {
|
||||
space: false,
|
||||
..file_size_opts::DECIMAL
|
||||
};
|
||||
assert_eq!(1000.file_size(semi_custom_options).unwrap(), "1KB");
|
||||
|
||||
let semi_custom_options2 = file_size_opts::FileSizeOpts {
|
||||
suffix: "/s",
|
||||
..file_size_opts::BINARY
|
||||
};
|
||||
assert_eq!(999.file_size(semi_custom_options2).unwrap(), "999 B/s");
|
||||
|
||||
let semi_custom_options3 = file_size_opts::FileSizeOpts {
|
||||
suffix: "/day",
|
||||
space: false,
|
||||
..file_size_opts::DECIMAL
|
||||
};
|
||||
assert_eq!(1000.file_size(semi_custom_options3).unwrap(), "1KB/day");
|
||||
|
||||
let semi_custom_options4 = file_size_opts::FileSizeOpts {
|
||||
fixed_at: file_size_opts::FixedAt::Byte,
|
||||
..file_size_opts::BINARY
|
||||
};
|
||||
assert_eq!(2048.file_size(semi_custom_options4).unwrap(), "2048 B");
|
||||
|
||||
let semi_custom_options5 = file_size_opts::FileSizeOpts {
|
||||
fixed_at: file_size_opts::FixedAt::Kilo,
|
||||
..file_size_opts::BINARY
|
||||
};
|
||||
assert_eq!(
|
||||
16584975.file_size(semi_custom_options5).unwrap(),
|
||||
"16196.26 KiB"
|
||||
);
|
||||
|
||||
let semi_custom_options6 = file_size_opts::FileSizeOpts {
|
||||
fixed_at: file_size_opts::FixedAt::Tera,
|
||||
decimal_places: 10,
|
||||
..file_size_opts::BINARY
|
||||
};
|
||||
assert_eq!(
|
||||
15284975.file_size(semi_custom_options6).unwrap(),
|
||||
"0.0000139016 TiB"
|
||||
);
|
||||
|
||||
let semi_custom_options7 = file_size_opts::FileSizeOpts {
|
||||
allow_negative: true,
|
||||
..file_size_opts::DECIMAL
|
||||
};
|
||||
assert_eq!(
|
||||
(-5500).file_size(&semi_custom_options7).unwrap(),
|
||||
"-5.50 KB"
|
||||
);
|
||||
assert_eq!((5500).file_size(&semi_custom_options7).unwrap(), "5.50 KB");
|
||||
}
|
Loading…
Reference in a new issue