Struct uucore::parse_size::Parser
source · pub struct Parser<'parser> {
pub capital_b_bytes: bool,
pub b_byte_count: bool,
pub allow_list: Option<&'parser [&'parser str]>,
pub default_unit: Option<&'parser str>,
}Expand description
Parser for sizes in SI or IEC units (multiples of 1000 or 1024 bytes).
The Parser::parse function performs the parse.
Fields§
§capital_b_bytes: boolWhether to treat the suffix “B” as meaning “bytes”.
b_byte_count: boolWhether to treat “b” as a “byte count” instead of “block”
allow_list: Option<&'parser [&'parser str]>Whitelist for the suffix
default_unit: Option<&'parser str>Default unit when no suffix is provided
Implementations§
source§impl<'parser> Parser<'parser>
impl<'parser> Parser<'parser>
pub fn with_allow_list(&mut self, allow_list: &'parser [&str]) -> &mut Self
pub fn with_default_unit(&mut self, default_unit: &'parser str) -> &mut Self
pub fn with_b_byte_count(&mut self, value: bool) -> &mut Self
sourcepub fn parse(&self, size: &str) -> Result<u64, ParseSizeError>
pub fn parse(&self, size: &str) -> Result<u64, ParseSizeError>
Parse a size string into a number of bytes.
A size string comprises an integer and an optional unit. The unit may be K, M, G, T, P, E, Z or Y (powers of 1024), or KB, MB, etc. (powers of 1000), or b which is 512. Binary prefixes can be used, too: KiB=K, MiB=M, and so on.
Errors
Will return ParseSizeError if it’s not possible to parse this
string into a number, e.g. if the string does not begin with a
numeral, or if the unit is not one of the supported units described
in the preceding section.
Examples
use uucore::parse_size::parse_size;
assert_eq!(Ok(123), parse_size("123"));
assert_eq!(Ok(9 * 1000), parse_size("9kB")); // kB is 1000
assert_eq!(Ok(2 * 1024), parse_size("2K")); // K is 1024
assert_eq!(Ok(44251 * 1024), parse_size("0xACDBK"));