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-08-09 04:35:15 +00:00
|
|
|
mod merged_object;
|
2020-08-27 07:35:48 +00:00
|
|
|
mod merged_subscription;
|
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-09-13 03:41:15 +00:00
|
|
|
use crate::utils::parse_derive;
|
2020-03-01 10:54:34 +00:00
|
|
|
use proc_macro::TokenStream;
|
2020-08-10 06:11:46 +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)]
|
2020-09-18 00:52:13 +00:00
|
|
|
pub fn Object(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-01 10:54:34 +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-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-09-18 00:52:13 +00:00
|
|
|
#[proc_macro_derive(SimpleObject, attributes(field, graphql))]
|
2020-06-11 03:23:23 +00:00
|
|
|
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-09-18 00:52:13 +00:00
|
|
|
#[proc_macro_derive(Enum, attributes(item, graphql))]
|
2020-06-11 03:23:23 +00:00
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[proc_macro_derive(InputObject, attributes(field, graphql))]
|
2020-06-11 03:23:23 +00:00
|
|
|
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
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[proc_macro_derive(Interface, attributes(graphql))]
|
2020-06-11 07:08:51 +00:00
|
|
|
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
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[proc_macro_derive(Union, attributes(graphql))]
|
2020-06-11 07:08:51 +00:00
|
|
|
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)]
|
2020-09-18 00:52:13 +00:00
|
|
|
pub fn Subscription(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-17 09:26:59 +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(),
|
|
|
|
};
|
|
|
|
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
|
|
|
|
2020-04-27 09:58:10 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[allow(non_snake_case)]
|
2020-09-18 00:52:13 +00:00
|
|
|
pub fn Scalar(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-04-27 09:58:10 +00:00
|
|
|
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
|
|
|
}
|
2020-08-09 04:35:15 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[proc_macro_derive(MergedObject, attributes(item, graphql))]
|
2020-08-10 06:11:46 +00:00
|
|
|
pub fn derive_merged_object(input: TokenStream) -> TokenStream {
|
|
|
|
let (args, input) = match parse_derive(input.into()) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
2020-08-09 04:35:15 +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(),
|
|
|
|
};
|
|
|
|
match merged_object::generate(&object_args, &input) {
|
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 07:35:48 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[proc_macro_derive(MergedSubscription, attributes(item, graphql))]
|
2020-08-27 07:35:48 +00:00
|
|
|
pub fn derive_merged_subscription(input: TokenStream) -> TokenStream {
|
|
|
|
let (args, input) = match parse_derive(input.into()) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
|
|
|
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(),
|
|
|
|
};
|
|
|
|
match merged_subscription::generate(&object_args, &input) {
|
|
|
|
Ok(expanded) => expanded,
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|