proxmox/src/api/rpc_environment.rs: use Value to store metadata

And impl. Index and IndexMut to make it easy to access/set the metadata.
This commit is contained in:
Dietmar Maurer 2020-05-18 09:36:33 +02:00
parent b450a72ffa
commit bd4b4bdc15
4 changed files with 39 additions and 23 deletions

View File

@ -274,15 +274,12 @@ fn func_with_option_schema_check() {
struct RpcEnv; struct RpcEnv;
impl proxmox::api::RpcEnvironment for RpcEnv { impl proxmox::api::RpcEnvironment for RpcEnv {
fn set_result_attrib(&mut self, name: &str, value: Value) { fn result_attrib_mut(&mut self) -> &mut Value {
let _ = (name, value); panic!("result_attrib_mut called");
panic!("set_result_attrib called");
} }
/// Query additional result data. fn result_attrib(&self) -> &Value {
fn get_result_attrib(&self, name: &str) -> Option<&Value> { panic!("result_attrib called");
let _ = name;
panic!("get_result_attrib called");
} }
/// The environment type /// The environment type

View File

@ -41,15 +41,12 @@ pub fn test_default_macro(value: Option<isize>) -> Result<isize, Error> {
struct RpcEnv; struct RpcEnv;
impl proxmox::api::RpcEnvironment for RpcEnv { impl proxmox::api::RpcEnvironment for RpcEnv {
fn set_result_attrib(&mut self, name: &str, value: Value) { fn result_attrib_mut(&mut self) -> &mut Value {
let _ = (name, value); panic!("result_attrib_mut called");
panic!("set_result_attrib called");
} }
/// Query additional result data. fn result_attrib(&self) -> &Value {
fn get_result_attrib(&self, name: &str) -> Option<&Value> { panic!("result_attrib called");
let _ = name;
panic!("get_result_attrib called");
} }
/// The environment type /// The environment type

View File

@ -1,12 +1,11 @@
use serde_json::Value; use serde_json::Value;
use std::collections::HashMap;
use crate::api::{RpcEnvironment, RpcEnvironmentType}; use crate::api::{RpcEnvironment, RpcEnvironmentType};
/// `RpcEnvironmet` implementation for command line tools /// `RpcEnvironmet` implementation for command line tools
#[derive(Default)] #[derive(Default)]
pub struct CliEnvironment { pub struct CliEnvironment {
result_attributes: HashMap<String, Value>, result_attributes: Value,
user: Option<String>, user: Option<String>,
} }
@ -17,12 +16,12 @@ impl CliEnvironment {
} }
impl RpcEnvironment for CliEnvironment { impl RpcEnvironment for CliEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) { fn result_attrib_mut(&mut self) -> &mut Value {
self.result_attributes.insert(name.into(), value); &mut self.result_attributes
} }
fn get_result_attrib(&self, name: &str) -> Option<&Value> { fn result_attrib(&self) -> &Value {
self.result_attributes.get(name) &self.result_attributes
} }
fn env_type(&self) -> RpcEnvironmentType { fn env_type(&self) -> RpcEnvironmentType {

View File

@ -6,10 +6,10 @@ use serde_json::Value;
pub trait RpcEnvironment: std::any::Any + AsAny + Send { pub trait RpcEnvironment: std::any::Any + AsAny + Send {
/// Use this to pass additional result data. It is up to the environment /// Use this to pass additional result data. It is up to the environment
/// how the data is used. /// how the data is used.
fn set_result_attrib(&mut self, name: &str, value: Value); fn result_attrib_mut(&mut self) -> &mut Value;
/// Query additional result data. /// Access result attribute immutable
fn get_result_attrib(&self, name: &str) -> Option<&Value>; fn result_attrib(&self) -> &Value;
/// The environment type /// The environment type
fn env_type(&self) -> RpcEnvironmentType; fn env_type(&self) -> RpcEnvironmentType;
@ -35,3 +35,26 @@ pub enum RpcEnvironmentType {
/// Access from privileged server (run as root) /// Access from privileged server (run as root)
PRIVILEGED, PRIVILEGED,
} }
impl core::ops::Index<&str> for &dyn RpcEnvironment
{
type Output = Value;
fn index(&self, index: &str) -> &Value {
&self.result_attrib().index(index)
}
}
impl core::ops::Index<&str> for &mut dyn RpcEnvironment
{
type Output = Value;
fn index(&self, index: &str) -> &Value {
&self.result_attrib().index(index)
}
}
impl core::ops::IndexMut<&str> for &mut dyn RpcEnvironment
{
fn index_mut(&mut self, index: &str) -> &mut Value {
self.result_attrib_mut().index_mut(index)
}
}