From 50af953e1b7245a3e474c6f100fe5ea8ad52bbb6 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 11 Jan 2020 11:09:27 +0100 Subject: [PATCH] src/section_config.rs: make set_data generic --- src/api2/config/datastore.rs | 2 +- src/api2/config/remotes.rs | 2 +- src/section_config.rs | 25 +++++++++++++++---------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs index 13048a6c..82c2266d 100644 --- a/src/api2/config/datastore.rs +++ b/src/api2/config/datastore.rs @@ -74,7 +74,7 @@ pub fn create_datastore(name: String, param: Value) -> Result<(), Error> { let backup_user = crate::backup::backup_user()?; let _store = ChunkStore::create(&name, path, backup_user.uid, backup_user.gid)?; - config.set_data(&name, "datastore", serde_json::to_value(datastore)?); + config.set_data(&name, "datastore", &datastore)?; datastore::save_config(&config)?; diff --git a/src/api2/config/remotes.rs b/src/api2/config/remotes.rs index 7e822789..38e448ab 100644 --- a/src/api2/config/remotes.rs +++ b/src/api2/config/remotes.rs @@ -65,7 +65,7 @@ pub fn create_remote(name: String, param: Value) -> Result<(), Error> { bail!("remote '{}' already exists.", name); } - config.set_data(&name, "remote", serde_json::to_value(&remote)?); + config.set_data(&name, "remote", &remote)?; remotes::save_config(&config)?; diff --git a/src/section_config.rs b/src/section_config.rs index 39209697..bfa6b082 100644 --- a/src/section_config.rs +++ b/src/section_config.rs @@ -6,6 +6,7 @@ use std::collections::VecDeque; use serde_json::{json, Value}; use serde::de::DeserializeOwned; +use serde::ser::Serialize; use proxmox::api::schema::*; use proxmox::tools::try_block; @@ -49,9 +50,15 @@ impl SectionConfigData { Self { sections: HashMap::new(), order: VecDeque::new() } } - pub fn set_data(&mut self, section_id: &str, type_name: &str, config: Value) { - // fixme: verify section_id schema here?? - self.sections.insert(section_id.to_string(), (type_name.to_string(), config)); + pub fn set_data( + &mut self, + section_id: &str, + type_name: &str, + config: T, + ) -> Result<(), Error> { + let json = serde_json::to_value(config)?; + self.sections.insert(section_id.to_string(), (type_name.to_string(), json)); + Ok(()) } pub fn lookup(&self, type_name: &str, remote: &str) -> Result { @@ -182,11 +189,6 @@ impl SectionConfig { let mut result = SectionConfigData::new(); - let mut create_section = |section_id: &str, type_name: &str, config| { - result.set_data(section_id, type_name, config); - result.record_order(section_id); - }; - try_block!({ for line in raw.lines() { line_no += 1; @@ -216,7 +218,9 @@ impl SectionConfig { if line.trim().is_empty() { // finish section test_required_properties(config, &plugin.properties)?; - create_section(section_id, &plugin.type_name, config.take()); + result.set_data(section_id, &plugin.type_name, config.take())?; + result.record_order(section_id); + state = ParseState::BeforeHeader; continue; } @@ -249,7 +253,8 @@ impl SectionConfig { if let ParseState::InsideSection(plugin, section_id, config) = state { // finish section test_required_properties(&config, &plugin.properties)?; - create_section(§ion_id, &plugin.type_name, config); + result.set_data(§ion_id, &plugin.type_name, config)?; + result.record_order(§ion_id); } Ok(())