@@ -11,7 +11,9 @@ use super::{
1111use crate :: descriptor:: { EnumDescriptor , TypePath } ;
1212use crate :: generator:: write_fields_array;
1313use crate :: resolver:: Resolver ;
14+ use heck:: ToUpperCamelCase ;
1415use std:: collections:: HashSet ;
16+ use itertools:: Itertools ;
1517use std:: io:: { Result , Write } ;
1618
1719pub fn generate_enum < W : Write > (
@@ -20,6 +22,7 @@ pub fn generate_enum<W: Write>(
2022 descriptor : & EnumDescriptor ,
2123 writer : & mut W ,
2224 use_integers_for_enums : bool ,
25+ support_camel_case_for_enum_deserialization : bool ,
2326) -> Result < ( ) > {
2427 let rust_type = resolver. rust_type ( path) ;
2528
@@ -74,7 +77,13 @@ pub fn generate_enum<W: Write>(
7477 // Generate Deserialize
7578 write_deserialize_start ( 0 , & rust_type, writer) ?;
7679 write_fields_array ( writer, 2 , variants. iter ( ) . map ( |( name, _, _) | name. as_str ( ) ) ) ?;
77- write_visitor ( writer, 2 , & rust_type, & variants) ?;
80+ write_visitor (
81+ writer,
82+ 2 ,
83+ & rust_type,
84+ & variants,
85+ support_camel_case_for_enum_deserialization,
86+ ) ?;
7887
7988 // Use deserialize_any to allow users to provide integers or strings
8089 writeln ! (
@@ -92,6 +101,7 @@ fn write_visitor<W: Write>(
92101 indent : usize ,
93102 rust_type : & str ,
94103 variants : & [ ( String , i32 , String ) ] ,
104+ support_camel_case_for_enum_deserialization : bool ,
95105) -> Result < ( ) > {
96106 // Protobuf supports deserialization of enumerations both from string and integer values
97107 writeln ! (
@@ -139,14 +149,22 @@ fn write_visitor<W: Write>(
139149
140150 writeln ! ( writer, "{}match value {{" , Indent ( indent + 2 ) ) ?;
141151 for ( variant_name, _, rust_variant) in variants {
152+ let mut variants = vec ! [ variant_name. to_string( ) ] ;
153+ if support_camel_case_for_enum_deserialization {
154+ let camel_case = variant_name. to_upper_camel_case ( ) ;
155+ variants. push ( camel_case) ;
156+ variants. dedup ( ) ;
157+ }
158+ let variants = variants. into_iter ( ) . map ( |variant| format ! ( "\" {variant}\" " ) ) . join ( " | " ) ;
142159 writeln ! (
143160 writer,
144161 "{}\" {}\" => Ok({}::{})," ,
145162 Indent ( indent + 3 ) ,
146- variant_name ,
163+ variants ,
147164 rust_type,
148165 rust_variant
149166 ) ?;
167+
150168 }
151169
152170 writeln ! (
0 commit comments