chore: Cleanup parse function
This commit is contained in:
parent
77b749c896
commit
c43fc368e8
1 changed files with 20 additions and 21 deletions
|
@ -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."))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue