Struct parse_size::Config [−][src]
pub struct Config { /* fields omitted */ }
Configuration of the parser.
Implementations
impl Config
[src]
impl Config
[src]pub const fn new() -> Self
[src]
Creates a new parser configuration.
pub const fn with_unit_system(self, unit_system: UnitSystem) -> Self
[src]
Changes the configuration’s unit system.
The default system is decimal (powers of 1000).
pub const fn with_binary(self) -> Self
[src]
Changes the configuration to use the binary unit system, which are defined to be powers of 1024.
Examples
use parse_size::Config; let cfg = Config::new().with_binary(); assert_eq!(cfg.parse_size("1 KB"), Ok(1024)); assert_eq!(cfg.parse_size("1 KiB"), Ok(1024)); assert_eq!(cfg.parse_size("1 MB"), Ok(1048576)); assert_eq!(cfg.parse_size("1 MiB"), Ok(1048576));
pub const fn with_decimal(self) -> Self
[src]
Changes the configuration to use the decimal unit system, which are defined to be powers of 1000. This is the default setting.
Examples
use parse_size::Config; let cfg = Config::new().with_decimal(); assert_eq!(cfg.parse_size("1 KB"), Ok(1000)); assert_eq!(cfg.parse_size("1 KiB"), Ok(1024)); assert_eq!(cfg.parse_size("1 MB"), Ok(1000000)); assert_eq!(cfg.parse_size("1 MiB"), Ok(1048576));
pub const fn with_default_factor(self, factor: u64) -> Self
[src]
Changes the default factor when a byte unit is not provided.
This is useful for keeping backward compatibility when migrating from an old user interface expecting non-byte input.
The default value is 1.
Examples
If the input is a pure number, we treat that as mebibytes.
use parse_size::Config; let cfg = Config::new().with_default_factor(1048576); assert_eq!(cfg.parse_size("10"), Ok(10485760)); assert_eq!(cfg.parse_size("0.5"), Ok(524288)); assert_eq!(cfg.parse_size("128 B"), Ok(128)); // explicit units overrides the default assert_eq!(cfg.parse_size("16 KiB"), Ok(16384));
pub const fn with_byte_suffix(self, byte_suffix: ByteSuffix) -> Self
[src]
Changes the handling of the “B” suffix.
Normally, the character “B” at the end of the input is optional. This can be changed to deny or require such suffix.
Power prefixes (K, Ki, M, Mi, …) are not affected.
Examples
Deny the suffix.
use parse_size::{ByteSuffix, Config, Error}; let cfg = Config::new().with_byte_suffix(ByteSuffix::Deny); assert_eq!(cfg.parse_size("123"), Ok(123)); assert_eq!(cfg.parse_size("123k"), Ok(123000)); assert_eq!(cfg.parse_size("123B"), Err(Error::InvalidDigit)); assert_eq!(cfg.parse_size("123KB"), Err(Error::InvalidDigit));
Require the suffix.
use parse_size::{ByteSuffix, Config, Error}; let cfg = Config::new().with_byte_suffix(ByteSuffix::Require); assert_eq!(cfg.parse_size("123"), Err(Error::InvalidDigit)); assert_eq!(cfg.parse_size("123k"), Err(Error::InvalidDigit)); assert_eq!(cfg.parse_size("123B"), Ok(123)); assert_eq!(cfg.parse_size("123KB"), Ok(123000));
pub fn parse_size<T: AsRef<[u8]>>(&self, src: T) -> Result<u64, Error>
[src]
Parses the string input into the number of bytes it represents.
Examples
use parse_size::{Config, Error}; let cfg = Config::new().with_binary(); assert_eq!(cfg.parse_size("10 KB"), Ok(10240)); assert_eq!(cfg.parse_size("20000"), Ok(20000)); assert_eq!(cfg.parse_size("^_^"), Err(Error::InvalidDigit));