src/section_config.rs: make set_data generic
This commit is contained in:
parent
688fbe07a1
commit
50af953e1b
|
@ -74,7 +74,7 @@ pub fn create_datastore(name: String, param: Value) -> Result<(), Error> {
|
||||||
let backup_user = crate::backup::backup_user()?;
|
let backup_user = crate::backup::backup_user()?;
|
||||||
let _store = ChunkStore::create(&name, path, backup_user.uid, backup_user.gid)?;
|
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)?;
|
datastore::save_config(&config)?;
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ pub fn create_remote(name: String, param: Value) -> Result<(), Error> {
|
||||||
bail!("remote '{}' already exists.", name);
|
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)?;
|
remotes::save_config(&config)?;
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ use std::collections::VecDeque;
|
||||||
|
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
use serde::ser::Serialize;
|
||||||
|
|
||||||
use proxmox::api::schema::*;
|
use proxmox::api::schema::*;
|
||||||
use proxmox::tools::try_block;
|
use proxmox::tools::try_block;
|
||||||
|
@ -49,9 +50,15 @@ impl SectionConfigData {
|
||||||
Self { sections: HashMap::new(), order: VecDeque::new() }
|
Self { sections: HashMap::new(), order: VecDeque::new() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_data(&mut self, section_id: &str, type_name: &str, config: Value) {
|
pub fn set_data<T: Serialize>(
|
||||||
// fixme: verify section_id schema here??
|
&mut self,
|
||||||
self.sections.insert(section_id.to_string(), (type_name.to_string(), config));
|
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<T: DeserializeOwned>(&self, type_name: &str, remote: &str) -> Result<T, Error> {
|
pub fn lookup<T: DeserializeOwned>(&self, type_name: &str, remote: &str) -> Result<T, Error> {
|
||||||
|
@ -182,11 +189,6 @@ impl SectionConfig {
|
||||||
|
|
||||||
let mut result = SectionConfigData::new();
|
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!({
|
try_block!({
|
||||||
for line in raw.lines() {
|
for line in raw.lines() {
|
||||||
line_no += 1;
|
line_no += 1;
|
||||||
|
@ -216,7 +218,9 @@ impl SectionConfig {
|
||||||
if line.trim().is_empty() {
|
if line.trim().is_empty() {
|
||||||
// finish section
|
// finish section
|
||||||
test_required_properties(config, &plugin.properties)?;
|
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;
|
state = ParseState::BeforeHeader;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -249,7 +253,8 @@ impl SectionConfig {
|
||||||
if let ParseState::InsideSection(plugin, section_id, config) = state {
|
if let ParseState::InsideSection(plugin, section_id, config) = state {
|
||||||
// finish section
|
// finish section
|
||||||
test_required_properties(&config, &plugin.properties)?;
|
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(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in New Issue