Makes multiplier parsing system dependant

- multipler is now created as u128, then returned as usize after
  conversion. Errors due to overflow now depend on the system on which
the code is run.
This commit is contained in:
Tyler 2021-07-05 11:27:17 -07:00
parent 860cbc6311
commit 418ecbe61a

View file

@ -293,33 +293,33 @@ impl std::str::FromStr for StatusLevel {
} }
fn parse_multiplier<'a>(s: &'a str) -> Result<usize, ParseError> { fn parse_multiplier<'a>(s: &'a str) -> Result<usize, ParseError> {
match s { let mult: u128 = match s {
"c" => Ok(1), "c" => 1,
"w" => Ok(2), "w" => 2,
"b" => Ok(512), "b" => 512,
"kB" => Ok(1000), "kB" => 1000,
"K" | "KiB" => Ok(1024), "K" | "KiB" => 1024,
"MB" => Ok(1000 * 1000), "MB" => 1000 * 1000,
"M" | "MiB" => Ok(1024 * 1024), "M" | "MiB" => 1024 * 1024,
"GB" => Ok(1000 * 1000 * 1000), "GB" => 1000 * 1000 * 1000,
"G" | "GiB" => Ok(1024 * 1024 * 1024), "G" | "GiB" => 1024 * 1024 * 1024,
"TB" => Ok(1000 * 1000 * 1000 * 1000), "TB" => 1000 * 1000 * 1000 * 1000,
"T" | "TiB" => Ok(1024 * 1024 * 1024 * 1024), "T" | "TiB" => 1024 * 1024 * 1024 * 1024,
"PB" => Ok(1000 * 1000 * 1000 * 1000 * 1000), "PB" => 1000 * 1000 * 1000 * 1000 * 1000,
"P" | "PiB" => Ok(1024 * 1024 * 1024 * 1024 * 1024), "P" | "PiB" => 1024 * 1024 * 1024 * 1024 * 1024,
"EB" => Ok(1000 * 1000 * 1000 * 1000 * 1000 * 1000), "EB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
"E" | "EiB" => Ok(1024 * 1024 * 1024 * 1024 * 1024 * 1024), "E" | "EiB" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
// The following would overflow on my x64 system "ZB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
// "ZB" => "Z" | "ZiB" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
// Ok(1000*1000*1000*1000*1000*1000*1000), "YB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
// "Z" | "ZiB" => "Y" | "YiB" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
// Ok(1024*1024*1024*1024*1024*1024*1024), _ => return Err(ParseError::NoMatchingMultiplier(s.to_string())),
// "YB" => };
// Ok(1000*1000*1000*1000*1000*1000*1000*1000),
// "Y" | "YiB" => mult.try_into()
// Ok(1024*1024*1024*1024*1024*1024*1024*1024), .map_err(|_e| {
_ => Err(ParseError::NoMatchingMultiplier(s.to_string())), ParseError::MultiplierStringWouldOverflow(s.to_string())
} })
} }
fn parse_bytes_only(s: &str) -> Result<usize, ParseError> { fn parse_bytes_only(s: &str) -> Result<usize, ParseError> {