From e17d5d864df135a3e62f5b2861b3442cceaf25e0 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sun, 13 Oct 2019 08:39:49 +0200 Subject: [PATCH] src/backup/manifest.rs: improve parser (try_from) --- src/backup/manifest.rs | 15 +++++++++++---- src/tools.rs | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/backup/manifest.rs b/src/backup/manifest.rs index 8d5d4488..7f55aaca 100644 --- a/src/backup/manifest.rs +++ b/src/backup/manifest.rs @@ -52,13 +52,20 @@ impl TryFrom for BackupManifest { fn try_from(data: Value) -> Result { - 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 }) } diff --git a/src/tools.rs b/src/tools.rs index 06301d21..fb2154d8 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -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 { 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(param: &'a Value, name: &str) -> Result { + 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, 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(param: &'a Value, name: &str) -> Result, 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) -> Vec { let mut result = vec![];