diff --git a/vmm/src/config.rs b/vmm/src/config.rs index 5d56da98d..1ab9c6387 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -43,8 +43,6 @@ pub enum Error { ParsePmemFileMissing, /// Missing persistant memory size parameter. ParsePmemSizeMissing, - /// Failed parsing size parameter. - ParseSizeParam(std::num::ParseIntError), /// Both console and serial are tty. ParseTTYParam, /// Missing vsock socket path parameter. @@ -300,30 +298,26 @@ 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()) - })?)) + Ok(ByteSized({ + let s = s.trim(); + let shift = if s.ends_with('K') { + 10 + } else if s.ends_with('M') { + 20 + } else if s.ends_with('G') { + 30 + } else { + 0 + }; + + let s = s.trim_end_matches(|c| c == 'K' || c == 'M' || c == 'G'); + s.parse::() + .map_err(|_| ByteSizedParseError::InvalidValue(s.to_owned()))? + << shift + })) } } -fn parse_size(size: &str) -> Result { - let s = size.trim(); - - let shift = if s.ends_with('K') { - 10 - } else if s.ends_with('M') { - 20 - } else if s.ends_with('G') { - 30 - } else { - 0 - }; - - let s = s.trim_end_matches(|c| c == 'K' || c == 'M' || c == 'G'); - let res = s.parse::().map_err(Error::ParseSizeParam)?; - Ok(res << shift) -} - #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct CpusConfig { pub boot_vcpus: u8,