chore: Cleanup parse function
All checks were successful
Build / Check format (push) Successful in 37s
Build / Check release (push) Successful in 43s

This commit is contained in:
Xavier Moffett 2025-07-19 17:44:34 -04:00
parent 77b749c896
commit c43fc368e8
Signed by: Sapphirus
GPG key ID: E967DD18119C6EEA

View file

@ -35,31 +35,30 @@ pub fn suffix<'a, T: Copy>(unit: &ByteUnit<T>, i: i8) -> &'a str {
} }
} }
pub fn parse(s: &str) -> Result<(f64, f64, i8, bool), Error> { pub fn parse(input: &str) -> Result<(f64, f64, i8, bool), Error> {
let v = match s.to_lowercase() { let (value, suffix, power, iec) = match input.to_lowercase() {
string if string.ends_with("kib") => ("kib", K, true), value if value.ends_with("kib") => (value, "kib", K, true),
string if string.ends_with("mib") => ("mib", M, true), value if value.ends_with("mib") => (value, "mib", M, true),
string if string.ends_with("gib") => ("gib", G, true), value if value.ends_with("gib") => (value, "gib", G, true),
string if string.ends_with("tib") => ("tib", T, true), value if value.ends_with("tib") => (value, "tib", T, true),
string if string.ends_with("pib") => ("pib", P, true), value if value.ends_with("pib") => (value, "pib", P, true),
string if string.ends_with("eib") => ("eib", E, true), value if value.ends_with("eib") => (value, "eib", E, true),
string if string.ends_with("kb") => ("kb", K, false), value if value.ends_with("kb") => (value, "kb", K, false),
string if string.ends_with("mb") => ("mb", M, false), value if value.ends_with("mb") => (value, "mb", M, false),
string if string.ends_with("gb") => ("gb", G, false), value if value.ends_with("gb") => (value, "gb", G, false),
string if string.ends_with("tb") => ("tb", T, false), value if value.ends_with("tb") => (value, "tb", T, false),
string if string.ends_with("pb") => ("pb", P, false), value if value.ends_with("pb") => (value, "pb", P, false),
string if string.ends_with("eb") => ("eb", E, false), value if value.ends_with("eb") => (value, "eb", E, false),
string if string.ends_with("b") => ("b", B, false), value if value.ends_with("b") => (value, "b", B, false),
_ => Err(Error::InvalidUnit(format!("'{s}' contains no supported nor valid byteunits.")))?, _ => Err(Error::InvalidUnit(format!("'{input}' contains no supported nor valid byteunits.")))?,
}; };
let s = s.to_lowercase().replace(v.0, ""); let multiplier = match iec {
let multiplier = match v.2 {
true => 1024.0, true => 1024.0,
false => 1000.0, false => 1000.0,
}; };
match s.trim().parse() { match value.replace(suffix, "").trim().parse() {
Ok(val) => Ok((val, multiplier, v.1, v.2)), Ok(val) => Ok((val, multiplier, power, iec)),
Err(_) => Err(Error::ErroroneousInput(format!("'{s}' contains an invalid float or integer value."))), Err(_) => Err(Error::ErroroneousInput(format!("'{input}' contains an invalid float or integer value."))),
} }
} }