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;
impl proxmox::api::RpcEnvironment for RpcEnv {
fn set_result_attrib(&mut self, name: &str, value: Value) {
let _ = (name, value);
panic!("set_result_attrib called");
fn result_attrib_mut(&mut self) -> &mut Value {
panic!("result_attrib_mut called");
}
/// Query additional result data.
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
let _ = name;
panic!("get_result_attrib called");
fn result_attrib(&self) -> &Value {
panic!("result_attrib called");
}
/// The environment type

View File

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

View File

@ -1,12 +1,11 @@
use serde_json::Value;
use std::collections::HashMap;
use crate::api::{RpcEnvironment, RpcEnvironmentType};
/// `RpcEnvironmet` implementation for command line tools
#[derive(Default)]
pub struct CliEnvironment {
result_attributes: HashMap<String, Value>,
result_attributes: Value,
user: Option<String>,
}
@ -17,12 +16,12 @@ impl CliEnvironment {
}
impl RpcEnvironment for CliEnvironment {
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 {

View File

@ -6,10 +6,10 @@ use serde_json::Value;
pub trait RpcEnvironment: std::any::Any + AsAny + Send {
/// Use this to pass additional result data. It is up to the environment
/// 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.
fn get_result_attrib(&self, name: &str) -> Option<&Value>;
/// Access result attribute immutable
fn result_attrib(&self) -> &Value;
/// The environment type
fn env_type(&self) -> RpcEnvironmentType;
@ -35,3 +35,26 @@ pub enum RpcEnvironmentType {
/// Access from privileged server (run as root)
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)
}
}