Function apollo_framework::fmt::parse_size[][src]

pub fn parse_size(str: impl AsRef<str>) -> Result<usize>
Expand description

Parses a file size from a given string.

This string can have the following suffixes:

  • k or K: multiplies the given value by 1024 thus treats the value as KiB
  • m or M: multiplies the given value by 1.048.576 thus treats the value as MiB
  • g or G: multiplies the given value by 1.073.741.824 thus treats the value as GiB
  • t or T: multiplies the given value by 1.099.511.627.776 thus treats the value as TiB

Returns an Err if either a non-integer value is given or if an unknow suffix was provided.

Examples

assert_eq!(apollo_framework::fmt::parse_size("100").unwrap(), 100);
assert_eq!(apollo_framework::fmt::parse_size("100b").unwrap(), 100);
assert_eq!(apollo_framework::fmt::parse_size("8k").unwrap(), 8192);
assert_eq!(apollo_framework::fmt::parse_size("8m").unwrap(), 8 * 1024 * 1024);
assert_eq!(apollo_framework::fmt::parse_size("4 G").unwrap(), 4 * 1024 * 1024 * 1024);
assert_eq!(apollo_framework::fmt::parse_size("3 T").unwrap(), 3 * 1024 * 1024 * 1024 * 1024);

// An invalid suffix results in an error...
assert_eq!(apollo_framework::fmt::parse_size("3 Y").is_err(), true);

// Decimal numbers result in an error...
assert_eq!(apollo_framework::fmt::parse_size("1.2g").is_err(), true);

// Negative numbers result in an error...
assert_eq!(apollo_framework::fmt::parse_size("-1").is_err(), true);