diff --git a/proxmox-api-macro/tests/api1.rs b/proxmox-api-macro/tests/api1.rs index 9bf88859..be215660 100644 --- a/proxmox-api-macro/tests/api1.rs +++ b/proxmox-api-macro/tests/api1.rs @@ -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 diff --git a/proxmox-api-macro/tests/options.rs b/proxmox-api-macro/tests/options.rs index 67fbd959..58ce39d1 100644 --- a/proxmox-api-macro/tests/options.rs +++ b/proxmox-api-macro/tests/options.rs @@ -41,15 +41,12 @@ pub fn test_default_macro(value: Option) -> Result { 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 diff --git a/proxmox/src/api/cli/environment.rs b/proxmox/src/api/cli/environment.rs index fdba039f..4831e3fc 100644 --- a/proxmox/src/api/cli/environment.rs +++ b/proxmox/src/api/cli/environment.rs @@ -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, + result_attributes: Value, user: Option, } @@ -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 { diff --git a/proxmox/src/api/rpc_environment.rs b/proxmox/src/api/rpc_environment.rs index 3eabc042..a1e9cf26 100644 --- a/proxmox/src/api/rpc_environment.rs +++ b/proxmox/src/api/rpc_environment.rs @@ -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) + } +}