async-graphql/derive/src/enum.rs

173 lines
6.0 KiB
Rust
Raw Normal View History

2020-09-28 09:44:00 +00:00
use darling::ast::Data;
2020-03-01 10:54:34 +00:00
use proc_macro::TokenStream;
use quote::quote;
use syn::ext::IdentExt;
2020-09-28 09:44:00 +00:00
use syn::Error;
2020-03-01 10:54:34 +00:00
2020-10-16 02:45:27 +00:00
use crate::args::{self, RenameRuleExt, RenameTarget};
use crate::utils::{get_crate_name, get_rustdoc, GeneratorResult};
2020-09-28 09:44:00 +00:00
pub fn generate(enum_args: &args::Enum) -> GeneratorResult<TokenStream> {
2020-03-02 00:24:49 +00:00
let crate_name = get_crate_name(enum_args.internal);
2020-09-28 09:44:00 +00:00
let ident = &enum_args.ident;
let e = match &enum_args.data {
2020-03-01 10:54:34 +00:00
Data::Enum(e) => e,
2020-09-28 09:44:00 +00:00
_ => return Err(Error::new_spanned(ident, "Enum can only be applied to an enum.").into()),
2020-03-01 10:54:34 +00:00
};
2020-10-14 09:08:57 +00:00
let gql_typename = enum_args
.name
.clone()
.unwrap_or_else(|| RenameTarget::Type.rename(ident.to_string()));
2020-03-19 09:20:12 +00:00
2020-09-28 09:44:00 +00:00
let desc = get_rustdoc(&enum_args.attrs)?
2020-10-16 10:37:59 +00:00
.map(|s| quote! { ::std::option::Option::Some(#s) })
.unwrap_or_else(|| quote! {::std::option::Option::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-09-28 09:44:00 +00:00
for variant in e {
2020-03-01 10:54:34 +00:00
if !variant.fields.is_empty() {
return Err(Error::new_spanned(
2020-09-28 09:44:00 +00:00
&variant.ident,
2020-03-01 10:54:34 +00:00
format!(
"Invalid enum variant {}.\nGraphQL enums may only contain unit variants.",
variant.ident
),
2020-09-28 09:44:00 +00:00
)
.into());
2020-03-01 10:54:34 +00:00
}
let item_ident = &variant.ident;
2020-10-14 09:08:57 +00:00
let gql_item_name = variant.name.clone().unwrap_or_else(|| {
enum_args
.rename_items
.rename(variant.ident.unraw().to_string(), RenameTarget::EnumItem)
});
2020-09-28 09:44:00 +00:00
let item_deprecation = variant
2020-03-03 03:48:00 +00:00
.deprecation
.as_ref()
2020-10-16 10:37:59 +00:00
.map(|s| quote! { ::std::option::Option::Some(#s) })
.unwrap_or_else(|| quote! {::std::option::Option::None});
2020-09-28 09:44:00 +00:00
let item_desc = get_rustdoc(&variant.attrs)?
2020-10-16 10:37:59 +00:00
.map(|s| quote! { ::std::option::Option::Some(#s) })
.unwrap_or_else(|| quote! {::std::option::Option::None});
2020-09-28 09:44:00 +00:00
2020-09-28 01:56:15 +00:00
enum_items.push(item_ident);
2020-03-01 10:54:34 +00:00
items.push(quote! {
2020-09-13 09:38:19 +00:00
#crate_name::resolver_utils::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! {
enum_items.insert(#gql_item_name, #crate_name::registry::MetaEnumValue {
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 remote_conversion = if let Some(remote) = &enum_args.remote {
let remote_ty = if let Ok(ty) = syn::parse_str::<syn::Type>(remote) {
ty
} else {
2020-09-28 09:44:00 +00:00
return Err(
Error::new_spanned(remote, format!("Invalid remote type: '{}'", remote)).into(),
);
};
2020-09-28 01:56:15 +00:00
let local_to_remote_items = enum_items.iter().map(|item| {
quote! {
#ident::#item => #remote_ty::#item,
}
});
2020-09-28 01:56:15 +00:00
let remote_to_local_items = enum_items.iter().map(|item| {
quote! {
#remote_ty::#item => #ident::#item,
}
});
Some(quote! {
impl ::std::convert::From<#ident> for #remote_ty {
fn from(value: #ident) -> Self {
match value {
#(#local_to_remote_items)*
}
}
}
impl ::std::convert::From<#remote_ty> for #ident {
fn from(value: #remote_ty) -> Self {
match value {
#(#remote_to_local_items)*
}
}
}
})
} else {
None
};
if schema_enum_items.is_empty() {
return Err(Error::new_spanned(
&ident,
2020-10-22 02:11:47 +00:00
"A GraphQL Enum type must define one or more unique enum values.",
)
.into());
}
2020-03-01 10:54:34 +00:00
let expanded = quote! {
#[allow(clippy::all, clippy::pedantic)]
2020-09-13 09:38:19 +00:00
impl #crate_name::resolver_utils::EnumType for #ident {
fn items() -> &'static [#crate_name::resolver_utils::EnumItem<#ident>] {
2020-03-01 10:54:34 +00:00
&[#(#items),*]
}
}
#[allow(clippy::all, clippy::pedantic)]
2020-03-19 09:20:12 +00:00
impl #crate_name::Type for #ident {
2020-10-16 10:37:59 +00:00
fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
::std::borrow::Cow::Borrowed(#gql_typename)
2020-03-01 10:54:34 +00:00
}
2020-03-03 03:48:00 +00:00
2020-10-16 10:37:59 +00:00
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
registry.create_type::<Self, _>(|registry| {
#crate_name::registry::MetaType::Enum {
2020-10-16 19:21:46 +00:00
name: ::std::borrow::ToOwned::to_owned(#gql_typename),
2020-03-03 11:15:18 +00:00
description: #desc,
2020-03-08 12:35:36 +00:00
enum_values: {
let mut enum_items = #crate_name::indexmap::IndexMap::new();
2020-03-08 12:35:36 +00:00
#(#schema_enum_items)*
enum_items
},
2020-03-03 03:48:00 +00:00
}
})
}
2020-03-01 10:54:34 +00:00
}
#[allow(clippy::all, clippy::pedantic)]
impl #crate_name::InputType for #ident {
2020-10-16 10:37:59 +00:00
fn parse(value: ::std::option::Option<#crate_name::Value>) -> #crate_name::InputValueResult<Self> {
2020-09-13 09:38:19 +00:00
#crate_name::resolver_utils::parse_enum(value.unwrap_or_default())
2020-03-17 09:26:59 +00:00
}
fn to_value(&self) -> #crate_name::Value {
2020-09-13 09:38:19 +00:00
#crate_name::resolver_utils::enum_value(*self)
}
2020-03-17 09:26:59 +00:00
}
#[#crate_name::async_trait::async_trait]
impl #crate_name::OutputType for #ident {
2020-10-10 02:32:43 +00:00
async fn resolve(&self, _: &#crate_name::ContextSelectionSet<'_>, _field: &#crate_name::Positioned<#crate_name::parser::types::Field>) -> #crate_name::ServerResult<#crate_name::Value> {
2020-10-16 10:37:59 +00:00
::std::result::Result::Ok(#crate_name::resolver_utils::enum_value(*self))
2020-03-17 09:26:59 +00:00
}
}
#remote_conversion
2020-03-01 10:54:34 +00:00
};
Ok(expanded.into())
}