2019-09-30 23:53:55 +02:00
|
|
|
#[cfg(feature = "openapi")]
|
|
|
|
pub mod openapi
|
|
|
|
{
|
|
|
|
pub use indexmap::IndexMap;
|
2019-10-03 00:04:33 +02:00
|
|
|
pub use openapiv3::{ObjectType, ReferenceOr, Schema, SchemaData, SchemaKind, StringType, Type, VariantOrUnknownOrEmpty};
|
2019-09-30 23:53:55 +02:00
|
|
|
}
|
2019-09-30 19:19:06 +02:00
|
|
|
|
2019-09-30 23:53:55 +02:00
|
|
|
#[cfg(not(feature = "openapi"))]
|
2019-09-30 19:19:06 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! rest_struct {
|
|
|
|
($struct_name:ident { $($field_id:ident : $field_ty:ty),* }) => {
|
|
|
|
#[derive(serde::Deserialize, serde::Serialize)]
|
2019-10-01 00:54:45 +02:00
|
|
|
pub struct $struct_name
|
2019-09-30 19:19:06 +02:00
|
|
|
{
|
|
|
|
$($field_id : $field_ty),*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:53:55 +02:00
|
|
|
#[cfg(feature = "openapi")]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! rest_struct {
|
|
|
|
($struct_name:ident { $($field_id:ident : $field_ty:ty),* }) => {
|
|
|
|
#[derive(serde::Deserialize, serde::Serialize)]
|
|
|
|
struct $struct_name
|
|
|
|
{
|
|
|
|
$($field_id : $field_ty),*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::gotham_restful::OpenapiType for $struct_name
|
|
|
|
{
|
2019-10-01 15:33:05 +02:00
|
|
|
fn to_schema() -> ::gotham_restful::OpenapiSchema
|
2019-09-30 23:53:55 +02:00
|
|
|
{
|
2019-10-01 15:33:05 +02:00
|
|
|
use ::gotham_restful::{helper::openapi::*, OpenapiSchema};
|
2019-09-30 23:53:55 +02:00
|
|
|
|
|
|
|
let mut properties : IndexMap<String, ReferenceOr<Box<Schema>>> = IndexMap::new();
|
|
|
|
let mut required : Vec<String> = Vec::new();
|
2019-10-01 16:13:13 +02:00
|
|
|
let mut dependencies : IndexMap<String, OpenapiSchema> = IndexMap::new();
|
2019-09-30 23:53:55 +02:00
|
|
|
|
|
|
|
$(
|
2019-10-01 16:13:13 +02:00
|
|
|
{
|
|
|
|
let mut schema = <$field_ty>::to_schema();
|
2019-10-01 18:03:44 +02:00
|
|
|
|
|
|
|
if schema.nullable
|
|
|
|
{
|
|
|
|
schema.nullable = false;
|
|
|
|
schema.name = schema.name.map(|name|
|
|
|
|
if name.ends_with("OrNull") {
|
|
|
|
name[..(name.len()-6)].to_string()
|
|
|
|
} else { name });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
required.push(stringify!($field_id).to_string());
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:13:13 +02:00
|
|
|
if let Some(name) = schema.name.clone()
|
|
|
|
{
|
|
|
|
properties.insert(
|
|
|
|
stringify!($field_id).to_string(),
|
|
|
|
ReferenceOr::Reference { reference: format!("#/components/schemas/{}", name) }
|
|
|
|
);
|
|
|
|
dependencies.insert(name, schema);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
properties.insert(
|
|
|
|
stringify!($field_id).to_string(),
|
|
|
|
ReferenceOr::Item(Box::new(<$field_ty>::to_schema().to_schema()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 23:53:55 +02:00
|
|
|
)*
|
|
|
|
|
2019-10-01 15:33:05 +02:00
|
|
|
let schema = SchemaKind::Type(Type::Object(ObjectType {
|
2019-09-30 23:53:55 +02:00
|
|
|
properties,
|
|
|
|
required,
|
|
|
|
additional_properties: None,
|
|
|
|
min_properties: None,
|
|
|
|
max_properties: None
|
2019-10-01 15:33:05 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
OpenapiSchema {
|
|
|
|
name: Some(stringify!($struct_name).to_string()),
|
|
|
|
nullable: false,
|
2019-10-01 16:13:13 +02:00
|
|
|
schema,
|
|
|
|
dependencies
|
2019-10-01 15:33:05 +02:00
|
|
|
}
|
2019-09-30 23:53:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 19:19:06 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! rest_resource {
|
2019-09-30 20:10:51 +02:00
|
|
|
($res_name:ident, $route:ident => $setup:block) => {
|
2019-10-01 00:54:45 +02:00
|
|
|
pub struct $res_name;
|
2019-09-30 19:19:06 +02:00
|
|
|
|
|
|
|
impl ::gotham_restful::Resource for $res_name
|
|
|
|
{
|
|
|
|
fn name() -> String
|
|
|
|
{
|
|
|
|
stringify!($res_name).to_string()
|
|
|
|
}
|
|
|
|
|
2019-09-30 20:10:51 +02:00
|
|
|
fn setup<D : ::gotham_restful::DrawResourceRoutes>(mut $route : D) $setup
|
2019-09-30 19:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|