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