diff --git a/proxmox-api/src/lib.rs b/proxmox-api/src/lib.rs index 8b5eb723..dc122d48 100644 --- a/proxmox-api/src/lib.rs +++ b/proxmox-api/src/lib.rs @@ -20,6 +20,8 @@ pub mod router; pub mod rpc_environment; pub mod schema; +use schema::{ObjectSchema, Schema}; + #[doc(inline)] pub use const_regex::ConstRegexPattern; @@ -104,6 +106,19 @@ pub enum ApiHandler { AsyncHttp(ApiAsyncHttpHandlerFn), } +const NULL_SCHEMA: Schema = Schema::Null; + +fn dummy_handler_fn( + _arg: Value, + _method: &ApiMethod, + _env: &mut dyn RpcEnvironment, +) -> Result { + // do nothing + Ok(Value::Null) +} + +const DUMMY_HANDLER: ApiHandler = ApiHandler::Sync(&dummy_handler_fn); + /// This struct defines synchronous API call which returns the restulkt as json `Value` pub struct ApiMethod { /// The protected flag indicates that the provides function should be forwarded @@ -129,3 +144,43 @@ impl std::fmt::Debug for ApiMethod { write!(f, "}}") } } + +impl ApiMethod { + pub const fn new(handler: &'static ApiHandler, parameters: &'static ObjectSchema) -> Self { + Self { + parameters, + handler, + returns: &NULL_SCHEMA, + protected: false, + reload_timezone: false, + } + } + + pub const fn new_dummy(parameters: &'static ObjectSchema) -> Self { + Self { + parameters, + handler: &DUMMY_HANDLER, + returns: &NULL_SCHEMA, + protected: false, + reload_timezone: false, + } + } + + pub const fn returns(mut self, schema: &'static Schema) -> Self { + self.returns = schema; + + self + } + + pub const fn protected(mut self, protected: bool) -> Self { + self.protected = protected; + + self + } + + pub const fn reload_timezone(mut self, reload_timezone: bool) -> Self { + self.reload_timezone = reload_timezone; + + self + } +} diff --git a/proxmox-api/src/router.rs b/proxmox-api/src/router.rs index afce3b4b..5a908859 100644 --- a/proxmox-api/src/router.rs +++ b/proxmox-api/src/router.rs @@ -1,64 +1,8 @@ use std::collections::HashMap; -use failure::Error; use hyper::Method; -use serde_json::Value; -use crate::schema::{ObjectSchema, Schema}; -use crate::{ApiHandler, ApiMethod, RpcEnvironment}; - -const NULL_SCHEMA: Schema = Schema::Null; - -fn dummy_handler_fn( - _arg: Value, - _method: &ApiMethod, - _env: &mut dyn RpcEnvironment, -) -> Result { - // do nothing - Ok(Value::Null) -} - -const DUMMY_HANDLER: ApiHandler = ApiHandler::Sync(&dummy_handler_fn); - -impl ApiMethod { - pub const fn new(handler: &'static ApiHandler, parameters: &'static ObjectSchema) -> Self { - Self { - parameters, - handler, - returns: &NULL_SCHEMA, - protected: false, - reload_timezone: false, - } - } - - pub const fn new_dummy(parameters: &'static ObjectSchema) -> Self { - Self { - parameters, - handler: &DUMMY_HANDLER, - returns: &NULL_SCHEMA, - protected: false, - reload_timezone: false, - } - } - - pub const fn returns(mut self, schema: &'static Schema) -> Self { - self.returns = schema; - - self - } - - pub const fn protected(mut self, protected: bool) -> Self { - self.protected = protected; - - self - } - - pub const fn reload_timezone(mut self, reload_timezone: bool) -> Self { - self.reload_timezone = reload_timezone; - - self - } -} +use crate::ApiMethod; pub type SubdirMap = &'static [(&'static str, &'static Router)];