#![recursion_limit = "512"]
extern crate proc_macro;
#[macro_use]
extern crate proc_macro_error2;
#[cfg(feature = "actix")]
#[macro_use]
mod actix;
#[cfg(feature = "v2")]
mod core;
use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
spanned::Spanned,
token::Comma,
DeriveInput, NestedMeta,
};
#[cfg(feature = "v2")]
#[proc_macro_attribute]
pub fn api_v2_schema_struct(_attr: TokenStream, input: TokenStream) -> TokenStream {
self::core::emit_v2_schema_struct(input)
}
#[cfg(feature = "actix")]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn api_v2_operation(attr: TokenStream, input: TokenStream) -> TokenStream {
self::actix::emit_v2_operation(attr, input)
}
#[cfg(feature = "actix")]
#[proc_macro_error]
#[proc_macro_derive(Apiv2Schema, attributes(openapi))]
pub fn api_v2_schema(input: TokenStream) -> TokenStream {
self::actix::emit_v2_definition(input)
}
#[cfg(feature = "actix")]
#[proc_macro_error]
#[proc_macro_derive(Apiv2Security, attributes(openapi))]
pub fn api_v2_security(input: TokenStream) -> TokenStream {
self::actix::emit_v2_security(input)
}
#[cfg(feature = "actix")]
#[proc_macro_error]
#[proc_macro_derive(Apiv2Header, attributes(openapi))]
pub fn api_v2_header(input: TokenStream) -> TokenStream {
self::actix::emit_v2_header(input)
}
#[cfg(feature = "actix")]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn api_v2_errors(attrs: TokenStream, input: TokenStream) -> TokenStream {
self::actix::emit_v2_errors(attrs, input)
}
#[cfg(feature = "actix")]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn api_v2_errors_overlay(attrs: TokenStream, input: TokenStream) -> TokenStream {
self::actix::emit_v2_errors_overlay(attrs, input)
}
#[allow(dead_code)]
fn span_error_with_msg<T: Spanned>(it: &T, msg: &str) -> TokenStream {
emit_error!(it.span().unwrap(), msg);
(quote! {}).into()
}
#[allow(dead_code)]
fn expect_struct_or_enum(ts: TokenStream) -> Result<DeriveInput, TokenStream> {
syn::parse(ts).map_err(|e| {
emit_error!(
e.span().unwrap(),
"expected struct or enum for deriving schema."
);
quote!().into()
})
}
#[derive(Default)]
struct MacroAttribute(#[allow(dead_code)] Punctuated<NestedMeta, Comma>);
impl Parse for MacroAttribute {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(MacroAttribute(input.call(Punctuated::parse_terminated)?))
}
}
#[cfg(feature = "actix")]
fn parse_input_attrs(ts: TokenStream) -> MacroAttribute {
syn::parse(ts)
.map_err(|e| {
emit_warning!(
e.span().unwrap(),
"cannot parse proc-macro input attributes."
);
})
.ok()
.unwrap_or_default()
}
#[cfg(feature = "actix")]
rest_methods! {
Get, get,
Post, post,
Put, put,
Delete, delete,
Patch, patch,
Head, head,
}