libs/types:Option type to handle empty tomlconfig

loading from empty string is only used to identity that the config is
not initialized yet, so Option<TomlConfig> is a better option

Signed-off-by: Zhongtao Hu <zhongtaohu.tim@linux.alibaba.com>
This commit is contained in:
Zhongtao Hu 2022-01-26 14:21:55 +08:00 committed by Fupan Li
parent 626828696d
commit 8ffff40af4
2 changed files with 21 additions and 17 deletions

View File

@ -185,7 +185,7 @@ pub fn validate_path_pattern<P: AsRef<Path>>(patterns: &[String], path: P) -> Re
/// Kata configuration information.
pub struct KataConfig {
config: TomlConfig,
config: Option<TomlConfig>,
agent: String,
hypervisor: String,
}
@ -194,7 +194,7 @@ impl KataConfig {
/// Set the default Kata configuration object.
///
/// The default Kata configuration information is loaded from system configuration file.
pub fn set_default_config(config: TomlConfig, hypervisor: &str, agent: &str) {
pub fn set_default_config(config: Option<TomlConfig>, hypervisor: &str, agent: &str) {
let kata = KataConfig {
config,
agent: agent.to_string(),
@ -214,7 +214,7 @@ impl KataConfig {
///
/// The active Kata configuration information is default configuration information patched
/// with tunable configuration information from annotations.
pub fn set_active_config(config: TomlConfig, hypervisor: &str, agent: &str) {
pub fn set_active_config(config: Option<TomlConfig>, hypervisor: &str, agent: &str) {
let kata = KataConfig {
config,
agent: agent.to_string(),
@ -232,13 +232,13 @@ impl KataConfig {
}
/// Get the config in use
pub fn get_config(&self) -> &TomlConfig {
&self.config
self.config.as_ref().unwrap()
}
/// Get the agent configuration in use.
pub fn get_agent(&self) -> Option<&Agent> {
if !self.agent.is_empty() {
self.config.agent.get(&self.agent)
self.config.as_ref().unwrap().agent.get(&self.agent)
} else {
None
}
@ -247,7 +247,11 @@ impl KataConfig {
/// Get the hypervisor configuration in use.
pub fn get_hypervisor(&self) -> Option<&Hypervisor> {
if !self.hypervisor.is_empty() {
self.config.hypervisor.get(&self.hypervisor)
self.config
.as_ref()
.unwrap()
.hypervisor
.get(&self.hypervisor)
} else {
None
}
@ -256,7 +260,7 @@ impl KataConfig {
lazy_static! {
static ref KATA_DEFAULT_CONFIG: Mutex<Arc<KataConfig>> = {
let config = TomlConfig::load("").unwrap();
let config = Some(TomlConfig::load("").unwrap());
let kata = KataConfig {
config,
agent: String::new(),
@ -266,7 +270,7 @@ lazy_static! {
Mutex::new(Arc::new(kata))
};
static ref KATA_ACTIVE_CONFIG: Mutex<Arc<KataConfig>> = {
let config = TomlConfig::load("").unwrap();
let config = Some(TomlConfig::load("").unwrap());
let kata = KataConfig {
config,
agent: String::new(),

View File

@ -34,7 +34,7 @@ mod tests {
qemu.register();
let config = TomlConfig::load(&content).unwrap();
KataConfig::set_active_config(config, "qemu", "agent0");
KataConfig::set_active_config(Some(config), "qemu", "agent0");
std::process::Command::new("mkdir")
.arg("./hypervisor_path")
@ -175,7 +175,7 @@ mod tests {
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_ok());
KataConfig::set_active_config(config, "qemu", "agnet0");
KataConfig::set_active_config(Some(config), "qemu", "agnet0");
if let Some(ag) = KataConfig::get_default_config().get_agent() {
assert_eq!(
ag.kernel_modules[0],
@ -287,7 +287,7 @@ mod tests {
qemu.register();
let config = TomlConfig::load(&content).unwrap();
KataConfig::set_active_config(config, "qemu", "agent0");
KataConfig::set_active_config(Some(config), "qemu", "agent0");
let mut anno_hash = HashMap::new();
anno_hash.insert(
@ -312,7 +312,7 @@ mod tests {
qemu.register();
let config = TomlConfig::load(&content).unwrap();
KataConfig::set_active_config(config, "qemu", "agent0");
KataConfig::set_active_config(Some(config), "qemu", "agent0");
let mut anno_hash = HashMap::new();
anno_hash.insert(
@ -337,7 +337,7 @@ mod tests {
qemu.register();
let config = TomlConfig::load(&content).unwrap();
KataConfig::set_active_config(config, "qemu", "agent0");
KataConfig::set_active_config(Some(config), "qemu", "agent0");
let mut anno_hash = HashMap::new();
anno_hash.insert(
@ -365,7 +365,7 @@ mod tests {
qemu.register();
let config = TomlConfig::load(&content).unwrap();
KataConfig::set_active_config(config, "qemu", "agent0");
KataConfig::set_active_config(Some(config), "qemu", "agent0");
let mut anno_hash = HashMap::new();
anno_hash.insert(
@ -386,7 +386,7 @@ mod tests {
let path = Path::new(path).join("tests/texture/configuration-anno-0.toml");
let content = fs::read_to_string(&path).unwrap();
let config = TomlConfig::load(&content).unwrap();
KataConfig::set_active_config(config, "qemu", "agent0");
KataConfig::set_active_config(Some(config), "qemu", "agent0");
let qemu = QemuConfig::new();
qemu.register();
@ -414,7 +414,7 @@ mod tests {
qemu.register();
let config = TomlConfig::load(&content).unwrap();
KataConfig::set_active_config(config, "qemu", "agent0");
KataConfig::set_active_config(Some(config), "qemu", "agent0");
let mut anno_hash = HashMap::new();
anno_hash.insert(
@ -439,7 +439,7 @@ mod tests {
qemu.register();
let config = TomlConfig::load(&content).unwrap();
KataConfig::set_active_config(config, "qemu", "agent0");
KataConfig::set_active_config(Some(config), "qemu", "agent0");
let mut anno_hash = HashMap::new();
anno_hash.insert(