Skip to main content

parse_size

Function parse_size 

Source
pub fn parse_size(s: &str) -> Result<u64, String>
Expand description

Parse a human-readable byte size like 1024, 10MB, or 5MiB into a count of bytes.

Sibling of parse_duration (same trim -> split -> numeric-prefix -> unit-match shape) but a distinct unit class, kept separate per the module docs rather than folded into one over-general parser.

Recognised units:

  • bare integer ("1024") -> bytes
  • B -> bytes
  • decimal SI (powers of 1000): KB, MB, GB
  • binary IEC (powers of 1024): KiB, MiB, GiB

The unit match is case-sensitive on the IEC i (so KiB is binary and KB is decimal). A leading - is non-digit, so negative inputs fall out the same way parse_duration("-5m") does (no number before the unit). The multiplier is applied with u64::checked_mul so an oversized input ("99999999999GB") returns Err rather than overflowing – this is a library crate and must not panic.

The error is a plain String so both the String-erroring config layer and the CliError-erroring CLI layer can wrap it without a shared error type.