api: add a meta module to help with macro impls

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-07-19 14:08:55 +02:00
parent cb2c260f87
commit 7cbc6d0962
3 changed files with 32 additions and 0 deletions

View File

@ -186,6 +186,7 @@ pub trait ApiType {
Self::type_info()
}
#[inline]
fn should_skip_serialization(&self) -> bool {
false
}
@ -239,6 +240,7 @@ impl<T: ApiType> ApiType for Option<T> {
*/
}
#[inline]
fn should_skip_serialization(&self) -> bool {
self.is_none()
}

View File

@ -23,6 +23,8 @@ pub use router::*;
pub mod cli;
pub mod meta;
/// Return type of an API method.
pub type ApiOutput<Body> = Result<Response<Body>, Error>;

28
proxmox-api/src/meta.rs Normal file
View File

@ -0,0 +1,28 @@
//! Type related meta information, mostly used by the macro code.
use crate::ApiType;
/// Helper trait for entries with a `default` value in their api type definition.
pub trait OrDefault {
type Output;
fn or_default(&self, def: &'static Self::Output) -> &Self::Output;
fn set(&mut self, value: Self::Output);
}
impl<T> OrDefault for Option<T>
where
T: ApiType,
{
type Output = T;
#[inline]
fn or_default(&self, def: &'static Self::Output) -> &Self::Output {
self.as_ref().unwrap_or(def)
}
#[inline]
fn set(&mut self, value: Self::Output) {
*self = Some(value);
}
}