Skip to content

Commit

Permalink
add display attribute for Enum macro #1518
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed May 9, 2024
1 parent 733729a commit a355af3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions derive/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ pub struct Enum {
#[darling(default)]
pub name: Option<String>,
#[darling(default)]
pub display: bool,
#[darling(default)]
pub name_type: bool,
#[darling(default)]
pub rename_items: Option<RenameRule>,
Expand Down
22 changes: 22 additions & 0 deletions derive/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn generate(enum_args: &args::Enum) -> GeneratorResult<TokenStream> {
.unwrap_or_else(|| quote! {::std::option::Option::None});

let mut enum_items = Vec::new();
let mut enum_names = Vec::new();
let mut items = Vec::new();
let mut schema_enum_items = Vec::new();

Expand Down Expand Up @@ -75,6 +76,7 @@ pub fn generate(enum_args: &args::Enum) -> GeneratorResult<TokenStream> {
.unwrap_or_else(|| quote! {::std::option::Option::None});

enum_items.push(item_ident);
enum_names.push(gql_item_name.clone());
items.push(quote! {
#crate_name::resolver_utils::EnumItem {
name: #gql_item_name,
Expand Down Expand Up @@ -136,6 +138,25 @@ pub fn generate(enum_args: &args::Enum) -> GeneratorResult<TokenStream> {
.into());
}

let display = if enum_args.display {
let items = enum_items.iter().zip(&enum_names).map(|(item, name)| {
quote! {
#ident::#item => #name,
}
});
Some(quote! {
impl ::std::fmt::Display for #ident {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
f.write_str(match self {
#(#items)*
})
}
}
})
} else {
None
};

let visible = visible_fn(&enum_args.visible);
let expanded = quote! {
#[allow(clippy::all, clippy::pedantic)]
Expand Down Expand Up @@ -217,6 +238,7 @@ pub fn generate(enum_args: &args::Enum) -> GeneratorResult<TokenStream> {
}

#remote_conversion
#display
};
Ok(expanded.into())
}
1 change: 1 addition & 0 deletions src/docs/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Define a GraphQL enum
|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------|----------|
| name | Enum name | string | Y |
| name_type | If `true`, the enum name will be specified from [`async_graphql::TypeName`](https://docs.rs/async-graphql/latest/async_graphql/trait.TypeName.html) trait | bool | Y |
| display | Implements `std::fmt::Display` for the enum type | bool | Y |
| rename_items | Rename all the fields according to the given case convention. The possible values are "lowercase", "UPPERCASE", "PascalCase", "camelCase", "snake_case", "SCREAMING_SNAKE_CASE". | string | Y |
| remote | Derive a remote enum | string | Y |
| visible | If `false`, it will not be displayed in introspection. *[See also the Book](https://async-graphql.github.io/async-graphql/en/visibility.html).* | bool | Y |
Expand Down
16 changes: 16 additions & 0 deletions tests/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,19 @@ pub async fn test_remote_enum() {
let _: remote::RemoteEnum = LocalEnum::A.into();
let _: LocalEnum = remote::RemoteEnum::A.into();
}

#[tokio::test]
pub async fn test_display() {
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
#[graphql(display)]
enum MyEnum {
A,
#[graphql(name = "bbb")]
B,
C,
}

assert_eq!(MyEnum::A.to_string(), "A");
assert_eq!(MyEnum::B.to_string(), "bbb");
assert_eq!(MyEnum::C.to_string(), "C");
}

0 comments on commit a355af3

Please sign in to comment.