vmm: config: Consolidate on/off parsing

Now all parsing code makes use of the Toggle and it's FromStr support
move the helper function into the from_str() implementation.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-04-03 09:45:41 +01:00
parent c731a943d4
commit a4e0ce58c7

View File

@ -53,8 +53,6 @@ pub enum Error {
ParseVsockCidMissing,
/// Missing kernel configuration
ValidateMissingKernelConfig,
/// Failed parsing generic on|off parameter.
ParseOnOff,
/// Error parsing CPU options
ParseCpus(OptionParserError),
/// Error parsing memory options
@ -252,9 +250,12 @@ impl FromStr for Toggle {
type Err = ToggleParseError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(Toggle(parse_on_off(s).map_err(|_| {
ToggleParseError::InvalidValue(s.to_owned())
})?))
match s {
"" => Ok(Toggle(false)),
"on" => Ok(Toggle(true)),
"off" => Ok(Toggle(false)),
_ => Err(ToggleParseError::InvalidValue(s.to_owned())),
}
}
}
@ -321,20 +322,6 @@ fn parse_size(size: &str) -> Result<u64> {
Ok(res << shift)
}
fn parse_on_off(param: &str) -> Result<bool> {
if !param.is_empty() {
let res = match param {
"on" => true,
"off" => false,
_ => return Err(Error::ParseOnOff),
};
Ok(res)
} else {
Ok(false)
}
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct CpusConfig {
pub boot_vcpus: u8,