1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-19 22:44:38 +00:00
deprecated-gotham-restful/src/helper.rs

101 lines
2.4 KiB
Rust
Raw Normal View History

2019-09-30 23:53:55 +02:00
#[cfg(feature = "openapi")]
pub mod openapi
{
pub use indexmap::IndexMap;
pub use openapiv3::{ObjectType, ReferenceOr, Schema, SchemaData, SchemaKind, Type};
}
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();
if let Some(name) = schema.name.clone()
{
properties.insert(
stringify!($field_id).to_string(),
ReferenceOr::Reference { reference: format!("#/components/schemas/{}", name) }
);
if schema.nullable
{
required.push(stringify!($field_id).to_string());
schema.nullable = false;
}
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 {
($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()
}
fn setup<D : ::gotham_restful::DrawResourceRoutes>(mut $route : D) $setup
2019-09-30 19:19:06 +02:00
}
}
}