1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
use darling::ast::Data; use proc_macro::TokenStream; use quote::quote; use syn::ext::IdentExt; use syn::Error; use crate::args::{self, RenameRuleExt, RenameTarget}; use crate::utils::{gen_deprecation, get_crate_name, get_rustdoc, visible_fn, GeneratorResult}; pub fn generate(enum_args: &args::Enum) -> GeneratorResult<TokenStream> { let crate_name = get_crate_name(enum_args.internal); let ident = &enum_args.ident; let e = match &enum_args.data { Data::Enum(e) => e, _ => return Err(Error::new_spanned(ident, "Enum can only be applied to an enum.").into()), }; let gql_typename = enum_args .name .clone() .unwrap_or_else(|| RenameTarget::Type.rename(ident.to_string())); let desc = get_rustdoc(&enum_args.attrs)? .map(|s| quote! { ::std::option::Option::Some(#s) }) .unwrap_or_else(|| quote! {::std::option::Option::None}); let mut enum_items = Vec::new(); let mut items = Vec::new(); let mut schema_enum_items = Vec::new(); for variant in e { if !variant.fields.is_empty() { return Err(Error::new_spanned( &variant.ident, format!( "Invalid enum variant {}.\nGraphQL enums may only contain unit variants.", variant.ident ), ) .into()); } let item_ident = &variant.ident; let gql_item_name = variant.name.clone().unwrap_or_else(|| { enum_args .rename_items .rename(variant.ident.unraw().to_string(), RenameTarget::EnumItem) }); let item_deprecation = gen_deprecation(&variant.deprecation, &crate_name); let item_desc = get_rustdoc(&variant.attrs)? .map(|s| quote! { ::std::option::Option::Some(#s) }) .unwrap_or_else(|| quote! {::std::option::Option::None}); enum_items.push(item_ident); items.push(quote! { #crate_name::resolver_utils::EnumItem { name: #gql_item_name, value: #ident::#item_ident, } }); let visible = visible_fn(&variant.visible); schema_enum_items.push(quote! { enum_items.insert(#gql_item_name, #crate_name::registry::MetaEnumValue { name: #gql_item_name, description: #item_desc, deprecation: #item_deprecation, visible: #visible, }); }); } 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 { return Err( Error::new_spanned(remote, format!("Invalid remote type: '{}'", remote)).into(), ); }; let local_to_remote_items = enum_items.iter().map(|item| { quote! { #ident::#item => #remote_ty::#item, } }); 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, "A GraphQL Enum type must define one or more unique enum values.", ) .into()); } let visible = visible_fn(&enum_args.visible); let expanded = quote! { #[allow(clippy::all, clippy::pedantic)] impl #crate_name::resolver_utils::EnumType for #ident { fn items() -> &'static [#crate_name::resolver_utils::EnumItem<#ident>] { &[#(#items),*] } } #[allow(clippy::all, clippy::pedantic)] impl #crate_name::Type for #ident { fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> { ::std::borrow::Cow::Borrowed(#gql_typename) } fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String { registry.create_type::<Self, _>(|registry| { #crate_name::registry::MetaType::Enum { name: ::std::borrow::ToOwned::to_owned(#gql_typename), description: #desc, enum_values: { let mut enum_items = #crate_name::indexmap::IndexMap::new(); #(#schema_enum_items)* enum_items }, visible: #visible, } }) } } #[allow(clippy::all, clippy::pedantic)] impl #crate_name::InputType for #ident { fn parse(value: ::std::option::Option<#crate_name::Value>) -> #crate_name::InputValueResult<Self> { #crate_name::resolver_utils::parse_enum(value.unwrap_or_default()) } fn to_value(&self) -> #crate_name::Value { #crate_name::resolver_utils::enum_value(*self) } } #[#crate_name::async_trait::async_trait] impl #crate_name::OutputType for #ident { 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)) } } #remote_conversion }; Ok(expanded.into()) }