rename optins struct

This commit is contained in:
LeopoldArkham 2022-08-21 19:42:07 +02:00
parent 28a531cae3
commit 5233e9e331
5 changed files with 23 additions and 29 deletions

View file

@ -40,7 +40,7 @@ fn main() {
}
```
If you wish to customize the way sizes are displayed, you may create your own custom `FileSizeOpts` struct
If you wish to customize the way sizes are displayed, you may create your own custom `FormatSizeOptions` struct
and pass that to the method. See the `custom_options.rs` file in the example folder.

View file

@ -3,7 +3,7 @@ use humansize::{file_size_opts as opts, FileSize};
fn main() {
// Declare a fully custom option struct
let custom_options = opts::FileSizeOpts {
let custom_options = opts::FormatSizeOptions {
divider: opts::Kilo::Binary,
units: opts::Kilo::Decimal,
decimal_places: 3,
@ -18,7 +18,7 @@ fn main() {
println!("{}", 3024.file_size(custom_options).unwrap());
// Or use only some custom parameters and adopt the rest from an existing config
let semi_custom_options = opts::FileSizeOpts {
let semi_custom_options = opts::FormatSizeOptions {
decimal_zeroes: 3,
..opts::DECIMAL
};

View file

@ -1,7 +1,7 @@
use crate::file_size_opts::{FileSizeOpts, FixedAt, Kilo};
use crate::file_size_opts::{FormatSizeOptions, FixedAt, Kilo};
/// Options to display sizes in the binary format.
pub const BINARY: FileSizeOpts = FileSizeOpts {
pub const BINARY: FormatSizeOptions = FormatSizeOptions {
divider: Kilo::Binary,
units: Kilo::Binary,
decimal_places: 2,
@ -10,11 +10,10 @@ pub const BINARY: FileSizeOpts = FileSizeOpts {
long_units: false,
space: true,
suffix: "",
allow_negative: false,
};
/// Options to display sizes in the decimal format.
pub const DECIMAL: FileSizeOpts = FileSizeOpts {
pub const DECIMAL: FormatSizeOptions = FormatSizeOptions {
divider: Kilo::Decimal,
units: Kilo::Decimal,
decimal_places: 2,
@ -23,12 +22,11 @@ pub const DECIMAL: FileSizeOpts = FileSizeOpts {
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 {
pub const CONVENTIONAL: FormatSizeOptions = FormatSizeOptions {
divider: Kilo::Binary,
units: Kilo::Decimal,
decimal_places: 2,
@ -37,5 +35,4 @@ pub const CONVENTIONAL: FileSizeOpts = FileSizeOpts {
long_units: false,
space: true,
suffix: "",
allow_negative: false,
};

View file

@ -6,7 +6,7 @@ pub mod defaults;
pub use self::defaults::*;
#[derive(Debug, PartialEq, Copy, Clone)]
/// Holds the standard to use when displying the size.
/// Holds the standard to use when displaying the size.
pub enum Kilo {
/// The decimal scale and units
Decimal,
@ -31,7 +31,7 @@ pub enum FixedAt {
/// Holds the options for the `file_size` method.
#[derive(Debug)]
pub struct FileSizeOpts {
pub struct FormatSizeOptions {
/// The scale (binary/decimal) to divide against.
pub divider: Kilo,
@ -47,7 +47,7 @@ pub struct FileSizeOpts {
/// Whether to force a certain representation and if so, which one.
pub fixed_at: FixedAt,
/// Whether to use the full suffix or its abbreveation.
/// Whether to use the full suffix or its abbreviation.
pub long_units: bool,
/// Whether to place a space between value and units.
@ -55,13 +55,10 @@ pub struct FileSizeOpts {
/// 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 {
impl AsRef<FormatSizeOptions> for FormatSizeOptions {
fn as_ref(&self) -> &FormatSizeOptions {
self
}
}

View file

@ -14,32 +14,32 @@ fn test_sizes() {
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 {
let semi_custom_options = file_size_opts::FormatSizeOptions {
space: false,
..file_size_opts::DECIMAL
};
assert_eq!(1000.file_size(semi_custom_options).unwrap(), "1KB");
let semi_custom_options2 = file_size_opts::FileSizeOpts {
let semi_custom_options2 = file_size_opts::FormatSizeOptions {
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 {
let semi_custom_options3 = file_size_opts::FormatSizeOptions {
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 {
let semi_custom_options4 = file_size_opts::FormatSizeOptions {
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 {
let semi_custom_options5 = file_size_opts::FormatSizeOptions {
fixed_at: file_size_opts::FixedAt::Kilo,
..file_size_opts::BINARY
};
@ -48,7 +48,7 @@ fn test_sizes() {
"16196.26 KiB"
);
let semi_custom_options6 = file_size_opts::FileSizeOpts {
let semi_custom_options6 = file_size_opts::FormatSizeOptions {
fixed_at: file_size_opts::FixedAt::Tera,
decimal_places: 10,
..file_size_opts::BINARY
@ -58,7 +58,7 @@ fn test_sizes() {
"0.0000139016 TiB"
);
let semi_custom_options7 = file_size_opts::FileSizeOpts {
let semi_custom_options7 = file_size_opts::FormatSizeOptions {
allow_negative: true,
..file_size_opts::DECIMAL
};
@ -71,7 +71,7 @@ fn test_sizes() {
#[test]
fn use_custom_option_struct_twice() {
let options = file_size_opts::FileSizeOpts {
let options = file_size_opts::FormatSizeOptions {
long_units: true,
..file_size_opts::DECIMAL
};
@ -83,7 +83,7 @@ fn use_custom_option_struct_twice() {
#[test]
fn pluralization_works() {
let options = file_size_opts::FileSizeOpts {
let options = file_size_opts::FormatSizeOptions {
long_units: true,
decimal_zeroes: 2,
..file_size_opts::DECIMAL
@ -115,7 +115,7 @@ fn pluralization_works() {
#[test]
fn max_value_decimal() {
let options = file_size_opts::FileSizeOpts {
let options = file_size_opts::FormatSizeOptions {
long_units: true,
decimal_places: 7,
..file_size_opts::DECIMAL
@ -129,7 +129,7 @@ fn max_value_decimal() {
#[test]
fn max_value_binary() {
let options = file_size_opts::FileSizeOpts {
let options = file_size_opts::FormatSizeOptions {
long_units: true,
decimal_places: 7,
..file_size_opts::BINARY