From be32065aa4a8a48f6d090364de6c97b762261505 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Mon, 30 Mar 2020 18:27:35 +0100 Subject: [PATCH] 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 --- vmm/src/config.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/vmm/src/config.rs b/vmm/src/config.rs index c2c147e2d..84d9c7b85 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -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 { + Ok(ByteSized(parse_size(s).map_err(|_| { + ByteSizedParseError::InvalidValue(s.to_owned()) + })?)) + } +} + fn parse_size(size: &str) -> Result { let s = size.trim();