api: move ReturnType from router to schema

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-08-26 11:39:55 +02:00
parent 0f2caafc4e
commit bf84e75603
2 changed files with 31 additions and 28 deletions

View File

@ -10,11 +10,14 @@ use hyper::Body;
use percent_encoding::percent_decode_str;
use serde_json::Value;
use crate::api::schema::{self, ObjectSchema, ParameterSchema, Schema};
use crate::api::schema::{ObjectSchema, ParameterSchema, Schema};
use crate::api::RpcEnvironment;
use super::Permission;
/// Deprecated reexport:
pub use super::schema::ReturnType;
/// A synchronous API handler gets a json Value as input and returns a json Value as output.
///
/// Most API handler are synchronous. Use this to define such handler:
@ -398,33 +401,6 @@ pub struct ApiAccess {
pub permission: &'static Permission,
}
#[cfg_attr(feature = "test-harness", derive(Eq, PartialEq))]
pub struct ReturnType {
/// A return type may be optional, meaning the method may return null or some fixed data.
///
/// If true, the return type in pseudo openapi terms would be `"oneOf": [ "null", "T" ]`.
pub optional: bool,
/// The method's return type.
pub schema: &'static schema::Schema,
}
impl std::fmt::Debug for ReturnType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.optional {
write!(f, "optional {:?}", self.schema)
} else {
write!(f, "{:?}", self.schema)
}
}
}
impl ReturnType {
pub const fn new(optional: bool, schema: &'static Schema) -> Self {
Self { optional, 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 {

View File

@ -1587,3 +1587,30 @@ impl<T> Updater for Option<T> {
self.is_none()
}
}
#[cfg_attr(feature = "test-harness", derive(Eq, PartialEq))]
pub struct ReturnType {
/// A return type may be optional, meaning the method may return null or some fixed data.
///
/// If true, the return type in pseudo openapi terms would be `"oneOf": [ "null", "T" ]`.
pub optional: bool,
/// The method's return type.
pub schema: &'static Schema,
}
impl std::fmt::Debug for ReturnType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.optional {
write!(f, "optional {:?}", self.schema)
} else {
write!(f, "{:?}", self.schema)
}
}
}
impl ReturnType {
pub const fn new(optional: bool, schema: &'static Schema) -> Self {
Self { optional, schema }
}
}