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.
config: Implementation of Builder Pattern #60
Implemented Builder for VMMConfig (WIP). 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 as user input). Brought these changes to api (CLI) to demonstrate a use. Removed required from 'memory' and 'vcpu' config. TODO: Documentation, Examples and Test cases. Signed-off-by: Abhijit Gadgil <[email protected]>
- Loading branch information
Showing
4 changed files
with
153 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//! Config builder | ||
use std::convert::TryFrom; | ||
|
||
use super::{ | ||
BlockConfig, ConversionError, KernelConfig, MemoryConfig, NetConfig, VMMConfig, VcpuConfig, | ||
}; | ||
|
||
/// Builder structure for VMMConfig | ||
#[derive(Debug)] | ||
pub struct Builder { | ||
inner: Result<VMMConfig, ConversionError>, | ||
} | ||
|
||
impl Default for Builder { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Builder { | ||
pub fn new() -> Self { | ||
Builder { | ||
inner: Ok(VMMConfig::default()), | ||
} | ||
} | ||
|
||
pub fn build(&self) -> Result<VMMConfig, ConversionError> { | ||
// Check if there are any errors | ||
match &self.inner { | ||
Ok(vc) => { | ||
// Empty kernel image path. | ||
if vc.kernel_config.path.to_str().unwrap().is_empty() { | ||
return Err(ConversionError::ParseKernel( | ||
"Kernel Image Path is Empty.".to_string(), | ||
)); | ||
} | ||
} | ||
Err(_) => {} | ||
} | ||
|
||
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) -> Result<VMMConfig, ConversionError>, | ||
{ | ||
Builder { | ||
inner: self.inner.and_then(func), | ||
} | ||
} | ||
} |
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