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: bool

Whether to treat the suffix “B” as meaning “bytes”.

§b_byte_count: bool

Whether 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>

source

pub fn with_allow_list(&mut self, allow_list: &'parser [&str]) -> &mut Self

source

pub fn with_default_unit(&mut self, default_unit: &'parser str) -> &mut Self

source

pub fn with_b_byte_count(&mut self, value: bool) -> &mut Self

source

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"));

Trait Implementations§

source§

impl<'parser> Default for Parser<'parser>

source§

fn default() -> Parser<'parser>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'parser> RefUnwindSafe for Parser<'parser>

§

impl<'parser> Send for Parser<'parser>

§

impl<'parser> Sync for Parser<'parser>

§

impl<'parser> Unpin for Parser<'parser>

§

impl<'parser> UnwindSafe for Parser<'parser>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.