generated from rust-vmm/crate-template
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of Builder pattern for Config
Implemented `Builder` for `VMMConfig`. (Note: This is an early implementation, more like prototype. But almost all functionality is present.) Removed `derive(Default)` from `MemoryConfig`, `KernelConfig` and `VcpuConfig` and added sensible defaults (memory = 256M, vcpu=1) (derive default will trivially initialize this to 0 and hence we are required to take a memory coniguration from CLI launcher. Brought these changes to `api` (`CLI`) to demonstrate a use. Removed `required` from 'memory' and 'vcpu' config. Changed `TryFrom<String>` to `TryFrom<&str>`. Believe this is better because it's quite possible to get `&str` from `String` than other way round and in fact often we'll get `&str` from CLI parsing etc. But the other way round is painful. Right now passes all 'existing' tests (`cargo test --all`, except one which likely fails because the right kernel image is not available to test.) TODO: Documentation with examples for using the `Builder` and Test cases for the `Builder`. Signed-off-by: Abhijit Gadgil <[email protected]>
- Loading branch information
Showing
5 changed files
with
205 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//! Config builder | ||
use std::convert::TryFrom; | ||
|
||
use super::{ | ||
BlockConfig, ConfigResult, ConversionError, KernelConfig, MemoryConfig, NetConfig, VMMConfig, | ||
VcpuConfig, | ||
}; | ||
|
||
/// Builder structure for VMMConfig | ||
#[derive(Debug)] | ||
pub struct Builder { | ||
inner: ConfigResult<VMMConfig>, | ||
} | ||
|
||
impl Default for Builder { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Builder { | ||
pub fn new() -> Self { | ||
Builder { | ||
inner: Ok(VMMConfig::default()), | ||
} | ||
} | ||
|
||
pub fn build(&self) -> ConfigResult<VMMConfig> { | ||
self.inner.clone() | ||
} | ||
|
||
pub fn memory_config<T>(self, memory: Option<T>) -> Self | ||
where | ||
MemoryConfig: TryFrom<T>, | ||
<MemoryConfig as TryFrom<T>>::Error: Into<ConversionError>, | ||
{ | ||
match memory { | ||
Some(m) => self.and_then(move |mut config| { | ||
config.memory_config = TryFrom::try_from(m).map_err(Into::into)?; | ||
Ok(config) | ||
}), | ||
None => self, | ||
} | ||
} | ||
|
||
pub fn vcpu_config<T>(self, vcpu: Option<T>) -> Self | ||
where | ||
VcpuConfig: TryFrom<T>, | ||
<VcpuConfig as TryFrom<T>>::Error: Into<ConversionError>, | ||
{ | ||
match vcpu { | ||
Some(v) => self.and_then(move |mut config| { | ||
config.vcpu_config = TryFrom::try_from(v).map_err(Into::into)?; | ||
Ok(config) | ||
}), | ||
None => self, | ||
} | ||
} | ||
|
||
pub fn kernel_config<T>(self, kernel: Option<T>) -> Self | ||
where | ||
KernelConfig: TryFrom<T>, | ||
<KernelConfig as TryFrom<T>>::Error: Into<ConversionError>, | ||
{ | ||
match kernel { | ||
Some(k) => self.and_then(move |mut config| { | ||
config.kernel_config = TryFrom::try_from(k).map_err(Into::into)?; | ||
Ok(config) | ||
}), | ||
None => self, | ||
} | ||
} | ||
|
||
pub fn network_config<T>(self, network: Option<T>) -> Self | ||
where | ||
NetConfig: TryFrom<T>, | ||
<NetConfig as TryFrom<T>>::Error: Into<ConversionError>, | ||
{ | ||
match network { | ||
Some(n) => self.and_then(move |mut config| { | ||
config.network_config = Some(TryFrom::try_from(n).map_err(Into::into)?); | ||
Ok(config) | ||
}), | ||
None => self, | ||
} | ||
} | ||
|
||
pub fn block_config<T>(self, block: Option<T>) -> Self | ||
where | ||
BlockConfig: TryFrom<T>, | ||
<BlockConfig as TryFrom<T>>::Error: Into<ConversionError>, | ||
{ | ||
match block { | ||
Some(b) => self.and_then(move |mut config| { | ||
config.block_config = Some(TryFrom::try_from(b).map_err(Into::into)?); | ||
Ok(config) | ||
}), | ||
None => self, | ||
} | ||
} | ||
|
||
fn and_then<F>(self, func: F) -> Self | ||
where | ||
F: FnOnce(VMMConfig) -> ConfigResult<VMMConfig>, | ||
{ | ||
Builder { | ||
inner: self.inner.and_then(func), | ||
} | ||
} | ||
} |
Oops, something went wrong.