mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-14 15:33:47 +00:00
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:
parent
b450a72ffa
commit
bd4b4bdc15
@ -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
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user