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