From 43e9cf10af7da02e8c6199acb476cc72a37bbe2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Wed, 13 Jan 2021 11:30:29 +0100 Subject: [PATCH] move ParameterSchema from router to schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit it's the place where it belongs, and unbreaks the --no-default-features build Signed-off-by: Fabian Grünbichler --- proxmox-api-macro/Cargo.toml | 2 +- proxmox-api-macro/src/api/method.rs | 4 +-- proxmox-api-macro/tests/allof.rs | 4 +-- proxmox/src/api/cli/completion.rs | 1 - proxmox/src/api/cli/getopts.rs | 1 - proxmox/src/api/mod.rs | 3 +- proxmox/src/api/router.rs | 55 +---------------------------- proxmox/src/api/schema.rs | 54 +++++++++++++++++++++++++++- 8 files changed, 60 insertions(+), 64 deletions(-) diff --git a/proxmox-api-macro/Cargo.toml b/proxmox-api-macro/Cargo.toml index ab6c590c..fb2e842f 100644 --- a/proxmox-api-macro/Cargo.toml +++ b/proxmox-api-macro/Cargo.toml @@ -19,7 +19,7 @@ syn = { version = "1.0", features = [ "full", "visit-mut" ] } [dev-dependencies] futures = "0.3" -proxmox = { path = "../proxmox", features = [ "test-harness" ] } +proxmox = { version = "0.9.1", path = "../proxmox", features = [ "test-harness" ] } serde = "1.0" serde_derive = "1.0" serde_json = "1.0" diff --git a/proxmox-api-macro/src/api/method.rs b/proxmox-api-macro/src/api/method.rs index 23501bc2..a03e6542 100644 --- a/proxmox-api-macro/src/api/method.rs +++ b/proxmox-api-macro/src/api/method.rs @@ -677,7 +677,7 @@ fn serialize_input_schema( pub const #input_schema_name: ::proxmox::api::schema::ObjectSchema = #ts; }, quote_spanned! { func_sig_span => - ::proxmox::api::router::ParameterSchema::Object(&#input_schema_name) + ::proxmox::api::schema::ParameterSchema::Object(&#input_schema_name) }, )); } @@ -750,7 +750,7 @@ fn serialize_input_schema( ); }, quote_spanned! { func_sig_span => - ::proxmox::api::router::ParameterSchema::AllOf(&#input_schema_name) + ::proxmox::api::schema::ParameterSchema::AllOf(&#input_schema_name) }, )) } diff --git a/proxmox-api-macro/tests/allof.rs b/proxmox-api-macro/tests/allof.rs index 1c1b9a97..9fcf9795 100644 --- a/proxmox-api-macro/tests/allof.rs +++ b/proxmox-api-macro/tests/allof.rs @@ -136,7 +136,7 @@ pub fn hello(it: IndexText, nv: NameValue) -> Result<(NameValue, IndexText), Err fn hello_schema_check() { const TEST_METHOD: ::proxmox::api::ApiMethod = ::proxmox::api::ApiMethod::new_full( &::proxmox::api::ApiHandler::Sync(&api_function_hello), - ::proxmox::api::router::ParameterSchema::AllOf(&::proxmox::api::schema::AllOfSchema::new( + ::proxmox::api::schema::ParameterSchema::AllOf(&::proxmox::api::schema::AllOfSchema::new( "Hello method.", &[&IndexText::API_SCHEMA, &NameValue::API_SCHEMA], )), @@ -176,7 +176,7 @@ fn with_extra_schema_check() { const TEST_METHOD: ::proxmox::api::ApiMethod = ::proxmox::api::ApiMethod::new_full( &::proxmox::api::ApiHandler::Sync(&api_function_with_extra), - ::proxmox::api::router::ParameterSchema::AllOf(&::proxmox::api::schema::AllOfSchema::new( + ::proxmox::api::schema::ParameterSchema::AllOf(&::proxmox::api::schema::AllOfSchema::new( "Extra method.", &[ &INNER_SCHEMA, diff --git a/proxmox/src/api/cli/completion.rs b/proxmox/src/api/cli/completion.rs index 42b3915e..a660473e 100644 --- a/proxmox/src/api/cli/completion.rs +++ b/proxmox/src/api/cli/completion.rs @@ -1,6 +1,5 @@ use super::*; -use crate::api::router::ParameterSchema; use crate::api::schema::*; fn record_done_argument( diff --git a/proxmox/src/api/cli/getopts.rs b/proxmox/src/api/cli/getopts.rs index adf0658f..6248fe53 100644 --- a/proxmox/src/api/cli/getopts.rs +++ b/proxmox/src/api/cli/getopts.rs @@ -3,7 +3,6 @@ use std::collections::HashMap; use anyhow::*; use serde_json::Value; -use crate::api::router::ParameterSchema; use crate::api::schema::*; #[derive(Debug)] diff --git a/proxmox/src/api/mod.rs b/proxmox/src/api/mod.rs index 5319cc14..8c6f597f 100644 --- a/proxmox/src/api/mod.rs +++ b/proxmox/src/api/mod.rs @@ -43,8 +43,7 @@ pub mod router; #[cfg(feature = "router")] #[doc(inline)] pub use router::{ - ApiFuture, ApiHandler, ApiMethod, ApiResponseFuture, ParameterSchema, Router, SubRoute, - SubdirMap, + ApiFuture, ApiHandler, ApiMethod, ApiResponseFuture, Router, SubRoute, SubdirMap, }; #[cfg(feature = "cli")] diff --git a/proxmox/src/api/router.rs b/proxmox/src/api/router.rs index 2f4b6c4e..609a89e1 100644 --- a/proxmox/src/api/router.rs +++ b/proxmox/src/api/router.rs @@ -10,7 +10,7 @@ use hyper::Body; use percent_encoding::percent_decode_str; use serde_json::Value; -use crate::api::schema::{self, AllOfSchema, ObjectSchema, Schema}; +use crate::api::schema::{self, ObjectSchema, ParameterSchema, Schema}; use crate::api::RpcEnvironment; use super::Permission; @@ -430,59 +430,6 @@ impl ReturnType { } } -/// Parameters are objects, but we have two types of object schemas, the regular one and the -/// `AllOf` schema. -#[derive(Clone, Copy, Debug)] -#[cfg_attr(feature = "test-harness", derive(Eq, PartialEq))] -pub enum ParameterSchema { - Object(&'static ObjectSchema), - AllOf(&'static AllOfSchema), -} - -impl schema::ObjectSchemaType for ParameterSchema { - type PropertyIter = Box>; - - fn description(&self) -> &'static str { - match self { - ParameterSchema::Object(o) => o.description(), - ParameterSchema::AllOf(o) => o.description(), - } - } - - fn lookup(&self, key: &str) -> Option<(bool, &Schema)> { - match self { - ParameterSchema::Object(o) => o.lookup(key), - ParameterSchema::AllOf(o) => o.lookup(key), - } - } - - fn properties(&self) -> Self::PropertyIter { - match self { - ParameterSchema::Object(o) => Box::new(o.properties()), - ParameterSchema::AllOf(o) => Box::new(o.properties()), - } - } - - fn additional_properties(&self) -> bool { - match self { - ParameterSchema::Object(o) => o.additional_properties(), - ParameterSchema::AllOf(o) => o.additional_properties(), - } - } -} - -impl From<&'static ObjectSchema> for ParameterSchema { - fn from(schema: &'static ObjectSchema) -> Self { - ParameterSchema::Object(schema) - } -} - -impl From<&'static AllOfSchema> for ParameterSchema { - fn from(schema: &'static AllOfSchema) -> Self { - ParameterSchema::AllOf(schema) - } -} - /// This struct defines a synchronous API call which returns the result as json `Value` #[cfg_attr(feature = "test-harness", derive(Eq, PartialEq))] pub struct ApiMethod { diff --git a/proxmox/src/api/schema.rs b/proxmox/src/api/schema.rs index 1378d78f..c2cc61e0 100644 --- a/proxmox/src/api/schema.rs +++ b/proxmox/src/api/schema.rs @@ -10,7 +10,6 @@ use anyhow::{bail, format_err, Error}; use serde_json::{json, Value}; use url::form_urlencoded; -use super::router::ParameterSchema; use crate::api::const_regex::ConstRegexPattern; /// Error type for schema validation @@ -752,6 +751,59 @@ impl PartialEq for ApiStringFormat { } } +/// Parameters are objects, but we have two types of object schemas, the regular one and the +/// `AllOf` schema. +#[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "test-harness", derive(Eq, PartialEq))] +pub enum ParameterSchema { + Object(&'static ObjectSchema), + AllOf(&'static AllOfSchema), +} + +impl ObjectSchemaType for ParameterSchema { + type PropertyIter = Box>; + + fn description(&self) -> &'static str { + match self { + ParameterSchema::Object(o) => o.description(), + ParameterSchema::AllOf(o) => o.description(), + } + } + + fn lookup(&self, key: &str) -> Option<(bool, &Schema)> { + match self { + ParameterSchema::Object(o) => o.lookup(key), + ParameterSchema::AllOf(o) => o.lookup(key), + } + } + + fn properties(&self) -> Self::PropertyIter { + match self { + ParameterSchema::Object(o) => Box::new(o.properties()), + ParameterSchema::AllOf(o) => Box::new(o.properties()), + } + } + + fn additional_properties(&self) -> bool { + match self { + ParameterSchema::Object(o) => o.additional_properties(), + ParameterSchema::AllOf(o) => o.additional_properties(), + } + } +} + +impl From<&'static ObjectSchema> for ParameterSchema { + fn from(schema: &'static ObjectSchema) -> Self { + ParameterSchema::Object(schema) + } +} + +impl From<&'static AllOfSchema> for ParameterSchema { + fn from(schema: &'static AllOfSchema) -> Self { + ParameterSchema::AllOf(schema) + } +} + /// Helper function to parse boolean values /// /// - true: `1 | on | yes | true`