mirror of
https://git.dirksys.ovh/dirk/bankserver.git
synced 2025-12-20 11:09:21 +01:00
96 lines
3.6 KiB
Rust
96 lines
3.6 KiB
Rust
#[macro_export]
|
|
macro_rules! make_schemas {
|
|
(($($request_bodies:ty),*); ($($response_bodies:ty),*)$(, [$($deps:expr),*])? ) => {
|
|
#[cfg(feature = "schemas")]
|
|
#[allow(unused)]
|
|
pub const fn schemas() -> $crate::Schemas {
|
|
fn request_bodies(generator: &mut schemars::SchemaGenerator, schemas: &mut std::collections::HashMap<std::borrow::Cow<'static, str>, schemars::Schema>) {
|
|
$(
|
|
generator.subschema_for::<$request_bodies>();
|
|
// schemas.insert(<$request_bodies as schemars::JsonSchema>::schema_name(), generator.root_schema_for::<$request_bodies>());
|
|
)*
|
|
}
|
|
fn response_bodies(generator: &mut schemars::SchemaGenerator, schemas: &mut std::collections::HashMap<std::borrow::Cow<'static, str>, schemars::Schema>) {
|
|
$(
|
|
generator.subschema_for::<$response_bodies>();
|
|
// schemas.insert(<$response_bodies as schemars::JsonSchema>::schema_name(), generator.root_schema_for::<$response_bodies>());
|
|
)*
|
|
}
|
|
static DEPS: &'static [$crate::Schemas] = &[$($($deps()),*)?];
|
|
$crate::Schemas {
|
|
request_bodies,
|
|
response_bodies,
|
|
contains: DEPS
|
|
}
|
|
}
|
|
#[cfg(not(feature = "schemas"))]
|
|
pub const fn schemas() -> $crate::Schemas {
|
|
#[allow(unused)]
|
|
const fn test<T>() {}
|
|
$(
|
|
test::<$request_bodies>();
|
|
)*
|
|
$(
|
|
test::<$response_bodies>();
|
|
)*
|
|
const _DEPS: &'static [$crate::Schemas] = &[$($($deps()),*)?];
|
|
$crate::Schemas
|
|
}
|
|
};
|
|
([$($deps:expr),*]) => {
|
|
#[cfg(feature = "schemas")]
|
|
pub const fn schemas() -> $crate::Schemas {
|
|
fn ident(_: &mut schemars::SchemaGenerator, _: &mut std::collections::HashMap<std::borrow::Cow<'static, str>, schemars::Schema>) {}
|
|
static DEPS: &'static [$crate::Schemas] = &[$($deps()),*];
|
|
$crate::Schemas {
|
|
request_bodies: ident,
|
|
response_bodies: ident,
|
|
contains: DEPS
|
|
}
|
|
}
|
|
#[cfg(not(feature = "schemas"))]
|
|
pub const fn schemas() -> $crate::Schemas {
|
|
const _DEPS: &'static [$crate::Schemas] = &[$($deps()),*];
|
|
$crate::Schemas
|
|
}
|
|
};
|
|
}
|
|
|
|
#[cfg(not(feature = "schemas"))]
|
|
pub struct Schemas;
|
|
#[cfg(feature = "schemas")]
|
|
pub struct Schemas {
|
|
pub request_bodies: fn(
|
|
&mut schemars::SchemaGenerator,
|
|
&mut std::collections::HashMap<std::borrow::Cow<'static, str>, schemars::Schema>,
|
|
),
|
|
pub response_bodies: fn(
|
|
&mut schemars::SchemaGenerator,
|
|
&mut std::collections::HashMap<std::borrow::Cow<'static, str>, schemars::Schema>,
|
|
),
|
|
pub contains: &'static [Self],
|
|
}
|
|
|
|
#[cfg(feature = "schemas")]
|
|
impl Schemas {
|
|
pub fn generate(
|
|
&self,
|
|
requests: &mut schemars::SchemaGenerator,
|
|
responses: &mut schemars::SchemaGenerator,
|
|
request_schemas: &mut std::collections::HashMap<
|
|
std::borrow::Cow<'static, str>,
|
|
schemars::Schema,
|
|
>,
|
|
response_schemas: &mut std::collections::HashMap<
|
|
std::borrow::Cow<'static, str>,
|
|
schemars::Schema,
|
|
>,
|
|
) {
|
|
for schemas in self.contains {
|
|
schemas.generate(requests, responses, request_schemas, response_schemas);
|
|
}
|
|
(self.request_bodies)(requests, request_schemas);
|
|
(self.response_bodies)(requests, response_schemas);
|
|
}
|
|
}
|