From 3f633844fa45f67f1a665359e390ff4c6e017956 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 18 May 2020 09:57:35 +0200 Subject: [PATCH] depend on proxmox 0.1.31 - use Value to store result metadata --- src/server/environment.rs | 15 +++++++-------- src/server/formatter.rs | 15 ++++++--------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/server/environment.rs b/src/server/environment.rs index 9e026997..10ff958c 100644 --- a/src/server/environment.rs +++ b/src/server/environment.rs @@ -1,19 +1,18 @@ -use std::collections::HashMap; -use serde_json::Value; +use serde_json::{json, Value}; use proxmox::api::{RpcEnvironment, RpcEnvironmentType}; /// Encapsulates information about the runtime environment pub struct RestEnvironment { env_type: RpcEnvironmentType, - result_attributes: HashMap, + result_attributes: Value, user: Option, } impl RestEnvironment { pub fn new(env_type: RpcEnvironmentType) -> Self { Self { - result_attributes: HashMap::new(), + result_attributes: json!({}), user: None, env_type, } @@ -22,12 +21,12 @@ impl RestEnvironment { impl RpcEnvironment for RestEnvironment { - fn set_result_attrib(&mut self, name: &str, value: Value) { - self.result_attributes.insert(name.into(), value); + fn result_attrib_mut (&mut self) -> &mut Value { + &mut self.result_attributes } - fn get_result_attrib(&self, name: &str) -> Option<&Value> { - self.result_attributes.get(name) + fn result_attrib(&self) -> &Value { + &self.result_attributes } fn env_type(&self) -> RpcEnvironmentType { diff --git a/src/server/formatter.rs b/src/server/formatter.rs index 47b9b998..8818e671 100644 --- a/src/server/formatter.rs +++ b/src/server/formatter.rs @@ -41,16 +41,13 @@ pub fn json_data_response(data: Value) -> Response { fn add_result_attributes(result: &mut Value, rpcenv: &dyn RpcEnvironment) { - if let Some(total) = rpcenv.get_result_attrib("total").and_then(|v| v.as_u64()) { - result["total"] = Value::from(total); - } + let attributes = match rpcenv.result_attrib().as_object() { + Some(attr) => attr, + None => return, + }; - if let Some(active) = rpcenv.get_result_attrib("active").and_then(|v| v.as_bool()) { - result["active"] = Value::from(active); - } - - if let Some(changes) = rpcenv.get_result_attrib("changes") { - result["changes"] = changes.clone(); + for (key, value) in attributes { + result[key] = value.clone(); } }