Skip to content

Commit fe65248

Browse files
JakobDegenfacebook-github-bot
authored andcommitted
gazebo: Add variant_name_lowercase
Summary: As it says in the title, need it next diff. You can't really implement this on top of gazebo, at least not if you're hoping to get out a `&'static str` Reviewed By: stepancheg Differential Revision: D57251553 fbshipit-source-id: 63b2e4786aed7c06846b438fe7c3f1ec2a9d7d49
1 parent 3e0618f commit fe65248

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

gazebo/src/variants.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ pub use gazebo_derive::VariantName;
9292

9393
pub trait VariantName {
9494
fn variant_name(&self) -> &'static str;
95+
96+
fn variant_name_lowercase(&self) -> &'static str;
9597
}
9698

9799
impl<T> VariantName for Option<T> {
@@ -101,6 +103,13 @@ impl<T> VariantName for Option<T> {
101103
None => "None",
102104
}
103105
}
106+
107+
fn variant_name_lowercase(&self) -> &'static str {
108+
match self {
109+
Self::Some(_) => "some",
110+
None => "none",
111+
}
112+
}
104113
}
105114

106115
impl<T, E> VariantName for Result<T, E> {
@@ -110,6 +119,13 @@ impl<T, E> VariantName for Result<T, E> {
110119
Self::Err(_) => "Err",
111120
}
112121
}
122+
123+
fn variant_name_lowercase(&self) -> &'static str {
124+
match self {
125+
Self::Ok(_) => "ok",
126+
Self::Err(_) => "err",
127+
}
128+
}
113129
}
114130

115131
#[cfg(test)]
@@ -125,16 +141,19 @@ mod tests {
125141
enum MyEnum {
126142
Foo,
127143
Bar(usize),
128-
Baz { field: usize },
144+
FooBaz { field: usize },
129145
}
130146

131147
let x = MyEnum::Foo;
132148
assert_eq!(x.variant_name(), "Foo");
149+
assert_eq!(x.variant_name_lowercase(), "foo");
133150

134151
let x = MyEnum::Bar(1);
135152
assert_eq!(x.variant_name(), "Bar");
153+
assert_eq!(x.variant_name_lowercase(), "bar");
136154

137-
let x = MyEnum::Baz { field: 1 };
138-
assert_eq!(x.variant_name(), "Baz");
155+
let x = MyEnum::FooBaz { field: 1 };
156+
assert_eq!(x.variant_name(), "FooBaz");
157+
assert_eq!(x.variant_name_lowercase(), "foo_baz");
139158
}
140159
}

gazebo_derive/src/variant.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use syn::Ident;
1818
pub(crate) fn derive_variant_names(input: DeriveInput) -> syn::Result<proc_macro::TokenStream> {
1919
if let Data::Enum(data_enum) = input.data {
2020
let mut variant_body = Vec::new();
21+
let mut variant_lowercase_body = Vec::new();
2122
for variant in data_enum.variants {
2223
let variant_name = &variant.ident;
2324
let patterns = match variant.fields {
@@ -26,9 +27,13 @@ pub(crate) fn derive_variant_names(input: DeriveInput) -> syn::Result<proc_macro
2627
Fields::Unnamed(_) => quote! { (..) },
2728
};
2829
let variant_name_str = variant_name.to_string();
30+
let variant_name_lowercase_str = to_snake_case(&variant_name_str);
2931
variant_body.push(quote! {
3032
Self::#variant_name #patterns => #variant_name_str
3133
});
34+
variant_lowercase_body.push(quote! {
35+
Self::#variant_name #patterns => #variant_name_lowercase_str
36+
});
3237
}
3338

3439
let name = &input.ident;
@@ -41,6 +46,12 @@ pub(crate) fn derive_variant_names(input: DeriveInput) -> syn::Result<proc_macro
4146
#(#variant_body,)*
4247
}
4348
}
49+
50+
fn variant_name_lowercase(&self) -> &'static str {
51+
match self {
52+
#(#variant_lowercase_body,)*
53+
}
54+
}
4455
}
4556
};
4657

0 commit comments

Comments
 (0)