src/backup/manifest.rs: improve parser (try_from)

This commit is contained in:
Dietmar Maurer 2019-10-13 08:39:49 +02:00
parent 59e9ba01c6
commit e17d5d864d
2 changed files with 32 additions and 4 deletions

View File

@ -52,13 +52,20 @@ impl TryFrom<Value> for BackupManifest {
fn try_from(data: Value) -> Result<Self, Error> {
let backup_type = data["backup_type"].as_str().unwrap();
let backup_id = data["backup_id"].as_str().unwrap();
let backup_time = data["backup_time"].as_i64().unwrap();
let backup_type = crate::tools::required_string_property(&data, "backup_type")?;
let backup_id = crate::tools::required_string_property(&data, "backup_id")?;
let backup_time = crate::tools::required_integer_property(&data, "backup_time")?;
let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
let files = Vec::new();
let mut files = Vec::new();
for item in crate::tools::required_array_property(&data, "files")?.iter() {
let filename = crate::tools::required_string_property(item, "filename")?.to_owned();
let csum = crate::tools::required_string_property(item, "csum")?;
let csum = proxmox::tools::hex_to_digest(csum)?;
let size = crate::tools::required_integer_property(item, "size")? as u64;
files.push(FileInfo { filename, size, csum });
}
Ok(Self { files, snapshot })
}

View File

@ -261,6 +261,13 @@ pub fn required_string_param<'a>(param: &'a Value, name: &str) -> Result<&'a str
}
}
pub fn required_string_property<'a>(param: &'a Value, name: &str) -> Result<&'a str, Error> {
match param[name].as_str() {
Some(s) => Ok(s),
None => bail!("missing property '{}'", name),
}
}
pub fn required_integer_param<'a>(param: &'a Value, name: &str) -> Result<i64, Error> {
match param[name].as_i64() {
Some(s) => Ok(s),
@ -268,6 +275,13 @@ pub fn required_integer_param<'a>(param: &'a Value, name: &str) -> Result<i64, E
}
}
pub fn required_integer_property<'a>(param: &'a Value, name: &str) -> Result<i64, Error> {
match param[name].as_i64() {
Some(s) => Ok(s),
None => bail!("missing property '{}'", name),
}
}
pub fn required_array_param<'a>(param: &'a Value, name: &str) -> Result<Vec<Value>, Error> {
match param[name].as_array() {
Some(s) => Ok(s.to_vec()),
@ -275,6 +289,13 @@ pub fn required_array_param<'a>(param: &'a Value, name: &str) -> Result<Vec<Valu
}
}
pub fn required_array_property<'a>(param: &'a Value, name: &str) -> Result<Vec<Value>, Error> {
match param[name].as_array() {
Some(s) => Ok(s.to_vec()),
None => bail!("missing property '{}'", name),
}
}
pub fn complete_file_name(arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
let mut result = vec![];