2020-03-01 10:54:34 +00:00
|
|
|
use crate::args;
|
2020-03-19 09:20:12 +00:00
|
|
|
use crate::utils::{check_reserved_name, get_crate_name};
|
2020-03-09 12:00:57 +00:00
|
|
|
use inflector::Inflector;
|
2020-03-01 10:54:34 +00:00
|
|
|
use proc_macro::TokenStream;
|
2020-04-07 13:50:10 +00:00
|
|
|
use proc_macro2::{Ident, Span};
|
2020-03-01 10:54:34 +00:00
|
|
|
use quote::quote;
|
2020-04-07 13:50:10 +00:00
|
|
|
use syn::{Data, DeriveInput, Error, Meta, NestedMeta, Result};
|
|
|
|
|
|
|
|
const DERIVES: &[&str] = &["Copy", "Clone", "Eq", "PartialEq", "Debug"];
|
2020-03-01 10:54:34 +00:00
|
|
|
|
|
|
|
pub fn generate(enum_args: &args::Enum, input: &DeriveInput) -> Result<TokenStream> {
|
2020-03-02 00:24:49 +00:00
|
|
|
let crate_name = get_crate_name(enum_args.internal);
|
2020-03-01 10:54:34 +00:00
|
|
|
let attrs = &input.attrs;
|
|
|
|
let vis = &input.vis;
|
|
|
|
let ident = &input.ident;
|
|
|
|
let e = match &input.data {
|
|
|
|
Data::Enum(e) => e,
|
|
|
|
_ => return Err(Error::new_spanned(input, "It should be a enum")),
|
|
|
|
};
|
|
|
|
|
2020-04-07 13:50:10 +00:00
|
|
|
let mut new_attrs = Vec::new();
|
|
|
|
let mut has_derive = false;
|
|
|
|
for attr in attrs {
|
|
|
|
match attr.parse_meta()? {
|
|
|
|
Meta::List(ls) if ls.path.is_ident("derive") => {
|
|
|
|
let mut items = ls
|
|
|
|
.nested
|
|
|
|
.iter()
|
|
|
|
.map(|item| quote! { #item })
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
for name in DERIVES {
|
|
|
|
let mut found = false;
|
|
|
|
for nested_meta in &ls.nested {
|
|
|
|
if let NestedMeta::Meta(meta) = nested_meta {
|
|
|
|
if meta.path().is_ident(name) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
let name_ident = Ident::new(name, Span::call_site());
|
|
|
|
items.push(quote! { #name_ident })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_attrs.push(quote! { #[derive(#(#items),*)] });
|
|
|
|
has_derive = true;
|
|
|
|
}
|
|
|
|
_ => new_attrs.push(quote! { #attr }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has_derive {
|
|
|
|
let mut items = Vec::new();
|
|
|
|
for name in DERIVES {
|
|
|
|
let name_ident = Ident::new(name, Span::call_site());
|
|
|
|
items.push(quote! { #name_ident });
|
|
|
|
}
|
|
|
|
new_attrs.push(quote! { #[derive(#(#items),*)] });
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
let gql_typename = enum_args.name.clone().unwrap_or_else(|| ident.to_string());
|
2020-03-19 09:20:12 +00:00
|
|
|
check_reserved_name(&gql_typename, enum_args.internal)?;
|
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
let desc = enum_args
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! { Some(#s) })
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-03-01 10:54:34 +00:00
|
|
|
|
|
|
|
let mut enum_items = Vec::new();
|
|
|
|
let mut items = Vec::new();
|
2020-03-03 03:48:00 +00:00
|
|
|
let mut schema_enum_items = Vec::new();
|
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
for variant in &e.variants {
|
|
|
|
if !variant.fields.is_empty() {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&variant,
|
|
|
|
format!(
|
|
|
|
"Invalid enum variant {}.\nGraphQL enums may only contain unit variants.",
|
|
|
|
variant.ident
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let item_ident = &variant.ident;
|
|
|
|
let mut item_args = args::EnumItem::parse(&variant.attrs)?;
|
|
|
|
let gql_item_name = item_args
|
|
|
|
.name
|
|
|
|
.take()
|
2020-03-09 12:00:57 +00:00
|
|
|
.unwrap_or_else(|| variant.ident.to_string().to_screaming_snake_case());
|
2020-03-03 03:48:00 +00:00
|
|
|
let item_deprecation = item_args
|
|
|
|
.deprecation
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! { Some(#s) })
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let item_desc = item_args
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! { Some(#s) })
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-03-02 00:24:49 +00:00
|
|
|
enum_items.push(&variant.ident);
|
2020-03-01 10:54:34 +00:00
|
|
|
items.push(quote! {
|
2020-03-19 09:20:12 +00:00
|
|
|
#crate_name::EnumItem {
|
2020-03-01 10:54:34 +00:00
|
|
|
name: #gql_item_name,
|
|
|
|
value: #ident::#item_ident,
|
|
|
|
}
|
|
|
|
});
|
2020-03-03 03:48:00 +00:00
|
|
|
schema_enum_items.push(quote! {
|
2020-03-08 12:35:36 +00:00
|
|
|
enum_items.insert(#gql_item_name, #crate_name::registry::EnumValue {
|
2020-03-03 03:48:00 +00:00
|
|
|
name: #gql_item_name,
|
|
|
|
description: #item_desc,
|
|
|
|
deprecation: #item_deprecation,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-03 03:48:00 +00:00
|
|
|
});
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let expanded = quote! {
|
2020-04-07 13:50:10 +00:00
|
|
|
#(#new_attrs)*
|
2020-03-01 10:54:34 +00:00
|
|
|
#vis enum #ident {
|
|
|
|
#(#enum_items),*
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #crate_name::EnumType for #ident {
|
|
|
|
fn items() -> &'static [#crate_name::EnumItem<#ident>] {
|
2020-03-01 10:54:34 +00:00
|
|
|
&[#(#items),*]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #crate_name::Type for #ident {
|
2020-03-02 00:24:49 +00:00
|
|
|
fn type_name() -> std::borrow::Cow<'static, str> {
|
|
|
|
std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-03 03:48:00 +00:00
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
|
2020-03-04 06:24:44 +00:00
|
|
|
registry.create_type::<Self, _>(|registry| {
|
2020-03-03 11:15:18 +00:00
|
|
|
#crate_name::registry::Type::Enum {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: #gql_typename.to_string(),
|
2020-03-03 11:15:18 +00:00
|
|
|
description: #desc,
|
2020-03-08 12:35:36 +00:00
|
|
|
enum_values: {
|
|
|
|
let mut enum_items = std::collections::HashMap::new();
|
|
|
|
#(#schema_enum_items)*
|
|
|
|
enum_items
|
|
|
|
},
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #crate_name::InputValueType for #ident {
|
2020-03-04 02:38:07 +00:00
|
|
|
fn parse(value: &#crate_name::Value) -> Option<Self> {
|
2020-03-19 09:20:12 +00:00
|
|
|
#crate_name::EnumType::parse_enum(value)
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #crate_name::OutputValueType for #ident {
|
2020-04-02 02:21:04 +00:00
|
|
|
async fn resolve(value: &Self, _: &#crate_name::ContextSelectionSet<'_>, _pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
2020-03-25 03:39:28 +00:00
|
|
|
#crate_name::EnumType::resolve_enum(value)
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|