mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-03-20 07:58:55 +00:00
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:
parent
f01bd7d56d
commit
be32065aa4
@ -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();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user