From 0fe5d605e75efb0309df05b08cb9ae741ccf8411 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 17 Jan 2019 12:14:02 +0100 Subject: [PATCH] tools.rs: new helper required_string_param() To extract parameters from Value. --- src/api3/admin/datastore/upload_catar.rs | 5 +++-- src/bin/proxmox-backup-client.rs | 11 +++-------- src/tools.rs | 9 +++++++++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/api3/admin/datastore/upload_catar.rs b/src/api3/admin/datastore/upload_catar.rs index 5434cb45..c87b4c57 100644 --- a/src/api3/admin/datastore/upload_catar.rs +++ b/src/api3/admin/datastore/upload_catar.rs @@ -1,5 +1,6 @@ use failure::*; +use crate::tools; use crate::backup::datastore::*; use crate::backup::archive_index::*; //use crate::server::rest::*; @@ -41,8 +42,8 @@ impl Future for UploadCaTar { fn upload_catar(req_body: hyper::Body, param: Value, _info: &ApiUploadMethod) -> Result { - let store = param["name"].as_str().unwrap(); - let archive_name = param["archive_name"].as_str().unwrap(); + let store = tools::required_string_param(¶m, "name")?; + let archive_name = tools::required_string_param(¶m, "archive_name")?; println!("Upload {}.catar to {} ({}.aidx)", archive_name, store, archive_name); diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index a4b91e05..f75f1e25 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -19,11 +19,6 @@ use serde_json::{Value}; use hyper::Body; -fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str { - param[name].as_str().expect(&format!("missing parameter '{}'", name)) -} - - fn backup_directory(body: Body, store: &str, archive_name: &str) -> Result<(), Error> { let client = HttpClient::new("localhost"); @@ -63,9 +58,9 @@ fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target fn create_backup(param: Value, _info: &ApiMethod) -> Result { - let filename = required_string_param(¶m, "filename"); - let store = required_string_param(¶m, "store"); - let target = required_string_param(¶m, "target"); + let filename = tools::required_string_param(¶m, "filename")?; + let store = tools::required_string_param(¶m, "store")?; + let target = tools::required_string_param(¶m, "target")?; let mut chunk_size = 4*1024*1024; diff --git a/src/tools.rs b/src/tools.rs index 6ba62320..6f14f812 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -15,6 +15,8 @@ use std::time::Duration; use std::os::unix::io::AsRawFd; +use serde_json::Value; + pub mod timer; /// The `BufferedReader` trait provides a single function @@ -224,3 +226,10 @@ pub fn file_chunker( Ok(()) } + +pub fn required_string_param<'a>(param: &'a Value, name: &str) -> Result<&'a str, Error> { + match param[name].as_str() { + Some(s) => Ok(s), + None => bail!("missing parameter '{}'", name), + } +}