mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-10-01 02:55:45 +00:00
vmm: config: Consolidate size parsing code
The parse_size helper function can now be consolidated into the ByteSized FromStr implementation. Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
parent
f449486b9b
commit
1a10f16ad0
@ -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<Self, Self::Err> {
|
||||
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::<u64>()
|
||||
.map_err(|_| ByteSizedParseError::InvalidValue(s.to_owned()))?
|
||||
<< shift
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_size(size: &str) -> Result<u64> {
|
||||
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::<u64>().map_err(Error::ParseSizeParam)?;
|
||||
Ok(res << shift)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||
pub struct CpusConfig {
|
||||
pub boot_vcpus: u8,
|
||||
|
Loading…
Reference in New Issue
Block a user