vmm: config: Add "ByteSized" type for simplifying parsing of byte sizes

Byte sizes are quantities ending in "K", "M", "G" and by implementing
this type with a FromStr implementation the values can be converted
using .parse().

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-03-30 18:27:35 +01:00
parent f01bd7d56d
commit be32065aa4

View File

@ -291,6 +291,22 @@ impl FromStr for HotplugMethod {
}
}
struct ByteSized(u64);
enum ByteSizedParseError {
InvalidValue(String),
}
impl FromStr for ByteSized {
type Err = ByteSizedParseError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(ByteSized(parse_size(s).map_err(|_| {
ByteSizedParseError::InvalidValue(s.to_owned())
})?))
}
}
fn parse_size(size: &str) -> Result<u64> {
let s = size.trim();