parse_size

Struct Config

Source
pub struct Config { /* private fields */ }
Expand description

Configuration of the parser.

Implementations§

Source§

impl Config

Source

pub const fn new() -> Self

Creates a new parser configuration.

Source

pub const fn with_unit_system(self, unit_system: UnitSystem) -> Self

Changes the configuration’s unit system.

The default system is decimal (powers of 1000).

Source

pub const fn with_binary(self) -> Self

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

pub const fn with_decimal(self) -> Self

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

pub const fn with_default_factor(self, factor: u64) -> Self

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

pub const fn with_byte_suffix(self, byte_suffix: ByteSuffix) -> Self

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

pub fn parse_size<T: AsRef<[u8]>>(&self, src: T) -> Result<u64, Error>

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));
§Errors

Returns an Error if the input has invalid digits or the result exceeded 264−1.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.