Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(juniper_codegen): add conversions for enum variants to strings #540

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions juniper_codegen/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub fn impl_enum(ast: &syn::DeriveInput, is_internal: bool) -> TokenStream {
let mut resolves = TokenStream::new();
let mut from_inputs = TokenStream::new();
let mut to_inputs = TokenStream::new();
let mut to_strings = TokenStream::new();

for variant in variants {
match variant.fields {
Expand Down Expand Up @@ -204,6 +205,11 @@ pub fn impl_enum(ast: &syn::DeriveInput, is_internal: bool) -> TokenStream {
&#ident::#var_ident =>
#juniper_path::InputValue::scalar(#name.to_string()),
});

// Build to_strings clause.
to_strings.extend(quote! {
&#ident::#var_ident => #name.to_string(),
})
}

let _async = quote!(
Expand Down Expand Up @@ -281,6 +287,14 @@ pub fn impl_enum(ast: &syn::DeriveInput, is_internal: bool) -> TokenStream {
}
}

impl From<#ident> for String {
fn from(&self) -> String {
match self {
#to_strings
}
}
}

#_async
};

Expand Down