2020-03-21 01:32:13 +00:00
|
|
|
#![allow(clippy::cognitive_complexity)]
|
|
|
|
|
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-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-04-17 03:06:33 +00:00
|
|
|
use crate::utils::get_crate_name;
|
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-03-05 06:23:55 +00:00
|
|
|
use syn::{AttributeArgs, DeriveInput, 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 {
|
|
|
|
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 derive_input = parse_macro_input!(input as DeriveInput);
|
|
|
|
match simple_object::generate(&object_args, &mut derive_input) {
|
|
|
|
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 {
|
|
|
|
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(),
|
|
|
|
};
|
|
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
|
|
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-03-03 03:48:00 +00:00
|
|
|
let object_args = match args::InputObject::parse(parse_macro_input!(args as AttributeArgs)) {
|
2020-03-01 10:54:34 +00:00
|
|
|
Ok(enum_args) => enum_args,
|
|
|
|
Err(err) => return err.to_compile_error().into(),
|
|
|
|
};
|
|
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
|
|
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 {
|
|
|
|
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(),
|
|
|
|
};
|
|
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
|
|
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 {
|
|
|
|
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(),
|
|
|
|
};
|
|
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
|
|
match union::generate(&interface_args, &input) {
|
|
|
|
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(),
|
|
|
|
};
|
|
|
|
let item_impl = parse_macro_input!(input as ItemImpl);
|
|
|
|
let crate_name = get_crate_name(datasource_args.internal);
|
|
|
|
let expanded = quote! {
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
#item_impl
|
|
|
|
};
|
|
|
|
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(),
|
|
|
|
};
|
|
|
|
let item_impl = parse_macro_input!(input as ItemImpl);
|
|
|
|
let self_ty = &item_impl.self_ty;
|
|
|
|
let generic = &item_impl.generics;
|
|
|
|
let where_clause = &item_impl.generics.where_clause;
|
|
|
|
let crate_name = get_crate_name(scalar_args.internal);
|
|
|
|
let expanded = quote! {
|
|
|
|
#item_impl
|
|
|
|
|
|
|
|
impl #generic #crate_name::Type for #self_ty #where_clause {
|
|
|
|
fn type_name() -> std::borrow::Cow<'static, str> {
|
|
|
|
std::borrow::Cow::Borrowed(<#self_ty as #crate_name::ScalarType>::type_name())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
|
|
|
|
registry.create_type::<#self_ty, _>(|_| #crate_name::registry::Type::Scalar {
|
|
|
|
name: <#self_ty as #crate_name::ScalarType>::type_name().to_string(),
|
|
|
|
description: <#self_ty>::description(),
|
|
|
|
is_valid: |value| <#self_ty as #crate_name::ScalarType>::is_valid(value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl #generic #crate_name::InputValueType for #self_ty #where_clause {
|
2020-05-10 10:27:46 +00:00
|
|
|
fn parse(value: &#crate_name::Value) -> #crate_name::InputValueResult<Self> {
|
2020-04-27 09:58:10 +00:00
|
|
|
<#self_ty as #crate_name::ScalarType>::parse(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::ptr_arg)]
|
2020-04-27 10:54:22 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-04-27 09:58:10 +00:00
|
|
|
impl #generic #crate_name::OutputValueType for #self_ty #where_clause {
|
|
|
|
async fn resolve(
|
2020-05-05 13:06:48 +00:00
|
|
|
&self,
|
2020-04-27 09:58:10 +00:00
|
|
|
_: &#crate_name::ContextSelectionSet<'_>,
|
|
|
|
_pos: #crate_name::Pos,
|
|
|
|
) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
2020-05-05 13:06:48 +00:00
|
|
|
self.to_json()
|
2020-04-27 09:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
expanded.into()
|
|
|
|
}
|