2020-03-21 01:32:13 +00:00
|
|
|
#![allow(clippy::cognitive_complexity)]
|
2020-05-29 09:33:19 +00:00
|
|
|
#![forbid(unsafe_code)]
|
2020-03-21 01:32:13 +00:00
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
extern crate proc_macro;
|
|
|
|
|
|
|
|
mod args;
|
|
|
|
mod r#enum;
|
|
|
|
mod input_object;
|
2020-03-06 15:58:43 +00:00
|
|
|
mod interface;
|
2020-03-01 10:54:34 +00:00
|
|
|
mod object;
|
2020-03-06 15:58:43 +00:00
|
|
|
mod output_type;
|
2020-05-19 05:27:01 +00:00
|
|
|
mod scalar;
|
2020-03-27 02:20:20 +00:00
|
|
|
mod simple_object;
|
2020-03-17 09:26:59 +00:00
|
|
|
mod subscription;
|
2020-03-08 01:21:29 +00:00
|
|
|
mod union;
|
2020-03-02 00:24:49 +00:00
|
|
|
mod utils;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-06-11 03:23:23 +00:00
|
|
|
use crate::utils::{add_container_attrs, get_crate_name, parse_derive};
|
2020-03-01 10:54:34 +00:00
|
|
|
use proc_macro::TokenStream;
|
2020-04-17 03:06:33 +00:00
|
|
|
use quote::quote;
|
2020-03-01 10:54:34 +00:00
|
|
|
use syn::parse_macro_input;
|
2020-06-11 07:08:51 +00:00
|
|
|
use syn::{AttributeArgs, ItemImpl};
|
2020-03-01 10:54:34 +00:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn Object(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let object_args = match args::Object::parse(parse_macro_input!(args as AttributeArgs)) {
|
|
|
|
Ok(object_args) => object_args,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-03-05 06:23:55 +00:00
|
|
|
let mut item_impl = parse_macro_input!(input as ItemImpl);
|
|
|
|
match object::generate(&object_args, &mut item_impl) {
|
2020-03-01 10:54:34 +00:00
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 02:20:20 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn SimpleObject(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-06-11 03:23:23 +00:00
|
|
|
add_container_attrs(
|
|
|
|
quote!(GQLSimpleObject),
|
|
|
|
parse_macro_input!(args as AttributeArgs),
|
|
|
|
input.into(),
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|err| err.to_compile_error())
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_derive(GQLSimpleObject, attributes(field, graphql))]
|
|
|
|
pub fn derive_simple_object(input: TokenStream) -> TokenStream {
|
|
|
|
let (args, input) = match parse_derive(input.into()) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-03-27 02:20:20 +00:00
|
|
|
let object_args = match args::Object::parse(parse_macro_input!(args as AttributeArgs)) {
|
|
|
|
Ok(object_args) => object_args,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-06-11 03:23:23 +00:00
|
|
|
match simple_object::generate(&object_args, &input) {
|
2020-03-27 02:20:20 +00:00
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn Enum(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-06-11 03:23:23 +00:00
|
|
|
add_container_attrs(
|
|
|
|
quote!(GQLEnum, Copy, Clone, Eq, PartialEq),
|
|
|
|
parse_macro_input!(args as AttributeArgs),
|
|
|
|
input.into(),
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|err| err.to_compile_error())
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_derive(GQLEnum, attributes(item, graphql))]
|
|
|
|
pub fn derive_enum(input: TokenStream) -> TokenStream {
|
|
|
|
let (args, input) = match parse_derive(input.into()) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-03-01 10:54:34 +00:00
|
|
|
let enum_args = match args::Enum::parse(parse_macro_input!(args as AttributeArgs)) {
|
|
|
|
Ok(enum_args) => enum_args,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
|
|
|
match r#enum::generate(&enum_args, &input) {
|
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn InputObject(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-06-11 03:23:23 +00:00
|
|
|
add_container_attrs(
|
|
|
|
quote!(GQLInputObject),
|
|
|
|
parse_macro_input!(args as AttributeArgs),
|
|
|
|
input.into(),
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|err| err.to_compile_error())
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_derive(GQLInputObject, attributes(field, graphql))]
|
|
|
|
pub fn derive_input_object(input: TokenStream) -> TokenStream {
|
|
|
|
let (args, input) = match parse_derive(input.into()) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-03-03 03:48:00 +00:00
|
|
|
let object_args = match args::InputObject::parse(parse_macro_input!(args as AttributeArgs)) {
|
2020-06-11 03:23:23 +00:00
|
|
|
Ok(object_args) => object_args,
|
2020-03-01 10:54:34 +00:00
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
|
|
|
match input_object::generate(&object_args, &input) {
|
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn Interface(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-06-11 07:08:51 +00:00
|
|
|
add_container_attrs(
|
|
|
|
quote!(GQLInterface),
|
|
|
|
parse_macro_input!(args as AttributeArgs),
|
|
|
|
input.into(),
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|err| err.to_compile_error())
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_derive(GQLInterface, attributes(graphql))]
|
|
|
|
pub fn derive_interface(input: TokenStream) -> TokenStream {
|
|
|
|
let (args, input) = match parse_derive(input.into()) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-03-06 15:58:43 +00:00
|
|
|
let interface_args = match args::Interface::parse(parse_macro_input!(args as AttributeArgs)) {
|
|
|
|
Ok(interface_args) => interface_args,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
|
|
|
match interface::generate(&interface_args, &input) {
|
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 01:21:29 +00:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn Union(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-06-11 07:08:51 +00:00
|
|
|
add_container_attrs(
|
|
|
|
quote!(GQLUnion),
|
|
|
|
parse_macro_input!(args as AttributeArgs),
|
|
|
|
input.into(),
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|err| err.to_compile_error())
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_derive(GQLUnion, attributes(graphql))]
|
|
|
|
pub fn derive_union(input: TokenStream) -> TokenStream {
|
|
|
|
let (args, input) = match parse_derive(input.into()) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
|
|
|
let union_args = match args::Interface::parse(parse_macro_input!(args as AttributeArgs)) {
|
|
|
|
Ok(union_args) => union_args,
|
2020-03-08 01:21:29 +00:00
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-06-11 07:08:51 +00:00
|
|
|
match union::generate(&union_args, &input) {
|
2020-03-08 01:21:29 +00:00
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn Subscription(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let object_args = match args::Object::parse(parse_macro_input!(args as AttributeArgs)) {
|
|
|
|
Ok(object_args) => object_args,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
|
|
|
let mut item_impl = parse_macro_input!(input as ItemImpl);
|
|
|
|
match subscription::generate(&object_args, &mut item_impl) {
|
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 03:06:33 +00:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn DataSource(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let datasource_args = match args::DataSource::parse(parse_macro_input!(args as AttributeArgs)) {
|
|
|
|
Ok(datasource_args) => datasource_args,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-05-11 05:31:13 +00:00
|
|
|
let input2: proc_macro2::TokenStream = input.clone().into();
|
|
|
|
let _item_impl = parse_macro_input!(input as ItemImpl);
|
2020-04-17 03:06:33 +00:00
|
|
|
let crate_name = get_crate_name(datasource_args.internal);
|
|
|
|
let expanded = quote! {
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-05-11 05:31:13 +00:00
|
|
|
#input2
|
2020-04-17 03:06:33 +00:00
|
|
|
};
|
|
|
|
expanded.into()
|
|
|
|
}
|
2020-04-27 09:58:10 +00:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn Scalar(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let scalar_args = match args::Scalar::parse(parse_macro_input!(args as AttributeArgs)) {
|
|
|
|
Ok(scalar_args) => scalar_args,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-05-19 05:27:01 +00:00
|
|
|
let mut item_impl = parse_macro_input!(input as ItemImpl);
|
|
|
|
match scalar::generate(&scalar_args, &mut item_impl) {
|
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
2020-04-27 09:58:10 +00:00
|
|
|
}
|