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;
|
2020-07-02 02:24:54 +00:00
|
|
|
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};
|
2021-02-27 03:59:58 +00:00
|
|
|
use crate::utils::{gen_deprecation, get_crate_name, get_rustdoc, visible_fn, GeneratorResult};
|
2020-10-16 02:45:27 +00:00
|
|
|
|
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)
|
|
|
|
});
|
2021-02-27 03:59:58 +00:00
|
|
|
let item_deprecation = gen_deprecation(&variant.deprecation, &crate_name);
|
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-12-11 08:03:28 +00:00
|
|
|
|
|
|
|
let visible = visible_fn(&variant.visible);
|
2020-03-03 03:48:00 +00:00
|
|
|
schema_enum_items.push(quote! {
|
2020-05-15 02:08:37 +00:00
|
|
|
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-12-11 08:03:28 +00:00
|
|
|
visible: #visible,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-03 03:48:00 +00:00
|
|
|
});
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 08:05:25 +00:00
|
|
|
let remote_conversion = if let Some(remote) = &enum_args.remote {
|
2021-10-23 06:08:20 +00:00
|
|
|
let remote_ty = syn::parse_str::<syn::Type>(remote).map_err(|_| {
|
|
|
|
Error::new_spanned(remote, format!("Invalid remote type: '{}'", remote))
|
|
|
|
})?;
|
2020-09-27 08:05:25 +00:00
|
|
|
|
2020-09-28 01:56:15 +00:00
|
|
|
let local_to_remote_items = enum_items.iter().map(|item| {
|
2020-09-27 08:05:25 +00:00
|
|
|
quote! {
|
|
|
|
#ident::#item => #remote_ty::#item,
|
|
|
|
}
|
|
|
|
});
|
2020-09-28 01:56:15 +00:00
|
|
|
let remote_to_local_items = enum_items.iter().map(|item| {
|
2020-09-27 08:05:25 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2020-10-20 03:49:31 +00:00
|
|
|
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.",
|
2020-10-20 03:49:31 +00:00
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:03:28 +00:00
|
|
|
let visible = visible_fn(&enum_args.visible);
|
2020-03-01 10:54:34 +00:00
|
|
|
let expanded = quote! {
|
2020-06-28 06:28:53 +00:00
|
|
|
#[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),*]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2021-11-09 10:52:10 +00:00
|
|
|
impl #ident {
|
|
|
|
fn __type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
2020-06-24 06:04:48 +00:00
|
|
|
::std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-03 03:48:00 +00:00
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
fn __create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
|
|
|
registry.create_input_type::<Self, _>(|registry| {
|
2020-05-15 02:08:37 +00:00
|
|
|
#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: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut enum_items = #crate_name::indexmap::IndexMap::new();
|
2020-03-08 12:35:36 +00:00
|
|
|
#(#schema_enum_items)*
|
|
|
|
enum_items
|
|
|
|
},
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: #visible,
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: ::std::any::type_name::<Self>(),
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-12-11 07:37:50 +00:00
|
|
|
impl #crate_name::InputType for #ident {
|
2021-11-09 10:52:10 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
|
|
|
Self::__type_name()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
|
|
|
Self::__create_type_info(registry)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-05-26 10:34:43 +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-05-26 10:34:43 +00:00
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-12-11 07:37:50 +00:00
|
|
|
impl #crate_name::OutputType for #ident {
|
2021-11-09 10:52:10 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
|
|
|
Self::__type_name()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
|
|
|
Self::__create_type_info(registry)
|
|
|
|
}
|
|
|
|
|
2021-06-07 12:51:20 +00:00
|
|
|
async fn resolve(&self, _: &#crate_name::ContextSelectionSet<'_>, _field: &#crate_name::Positioned<#crate_name::parser::types::Field>) -> #crate_name::ServerResult<#crate_name::Value> {
|
|
|
|
::std::result::Result::Ok(#crate_name::resolver_utils::enum_value(*self))
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 07:32:55 +00:00
|
|
|
|
2021-09-17 01:16:22 +00:00
|
|
|
impl ::std::convert::From<#ident> for #crate_name::Value {
|
2021-09-17 01:02:09 +00:00
|
|
|
fn from(value: #ident) -> #crate_name::Value {
|
|
|
|
#crate_name::resolver_utils::enum_value(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 08:05:25 +00:00
|
|
|
#remote_conversion
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|