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

Fix code generation where enum variant name is a number #475

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions prost-build/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub fn to_snake(s: &str) -> String {
| "async" | "await" | "try" => ident.insert_str(0, "r#"),
// the following keywords are not supported as raw identifiers and are therefore suffixed with an underscore.
"self" | "super" | "extern" | "crate" => ident += "_",
// In case the identifier is a number or starts with one
_ if ident.chars().next().map_or(false, |c| c.is_numeric()) => ident.insert_str(0, "_"),
_ => (),
}
ident
Expand Down Expand Up @@ -64,6 +66,9 @@ mod tests {
assert_eq!("fuzz", &to_snake("fuzz_"));
assert_eq!("fuzz", &to_snake("Fuzz_"));
assert_eq!("fuz_z", &to_snake("FuzZ_"));
assert_eq!("_123", &to_snake("_123"));
assert_eq!("_123", &to_snake("123"));
assert_eq!("_123", &to_snake("123_"));

// From test_messages_proto3.proto.
assert_eq!("fieldname1", &to_snake("fieldname1"));
Expand Down