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

Handle keyword Self after stripping enum type prefix #998

Merged
merged 13 commits into from
Apr 9, 2024
Merged
18 changes: 15 additions & 3 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,21 +1191,27 @@ fn unescape_c_escape_string(s: &str) -> Vec<u8> {
///
/// It also tries to handle cases where the stripped name would be
/// invalid - for example, if it were to begin with a number.
///
/// The stripped name can be `Self`, which is sanitized analogously to [to_upper_camel]
fn strip_enum_prefix(prefix: &str, name: &str) -> String {
caspermeijn marked this conversation as resolved.
Show resolved Hide resolved
let stripped = name.strip_prefix(prefix).unwrap_or(name);

// If the next character after the stripped prefix is not
// uppercase, then it means that we didn't have a true prefix -
// for example, "Foo" should not be stripped from "Foobar".
if stripped
match if stripped
caspermeijn marked this conversation as resolved.
Show resolved Hide resolved
.chars()
.next()
.map(char::is_uppercase)
.unwrap_or(false)
{
stripped.to_owned()
stripped
} else {
name.to_owned()
name
} {
"Self" => "Self_".to_owned(),
// ... other cases to be added here as needed
s => s.to_owned(),
}
}

Expand Down Expand Up @@ -1329,4 +1335,10 @@ mod tests {
assert_eq!(strip_enum_prefix("Foo", "Bar"), "Bar");
assert_eq!(strip_enum_prefix("Foo", "Foo1"), "Foo1");
}

#[test]
fn test_strip_enum_prefix_resulting_in_keyword() {
assert_eq!(strip_enum_prefix("Foo", "FooBar"), "Bar");
assert_eq!(strip_enum_prefix("Foo", "FooSelf"), "Self_");
}
}
4 changes: 4 additions & 0 deletions tests/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ fn main() {
.compile_protos(&[src.join("default_enum_value.proto")], includes)
.unwrap();

config
.compile_protos(&[src.join("enum_keyword_variant.proto")], includes)
.unwrap();

config
.compile_protos(&[src.join("groups.proto")], includes)
.unwrap();
Expand Down
21 changes: 21 additions & 0 deletions tests/src/enum_keyword_variant.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

package enum_keyword_variant;

enum Feeding {
FEEDING_UNSPECIFIED = 0;
FEEDING_ASSISTED = 1;
// Careful: code generation resulted in "Self".
FEEDING_SELF = 2;
}

enum Grooming {
UNSPECIFIED = 0;
ASSISTED = 1;
SELF = 2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have a keyword as well? for example: ELSE = 3;

}

enum Number {
NUMBER_UNSPECIFIED = 0;
NUMBER_1 = 1;
}
7 changes: 7 additions & 0 deletions tests/src/enum_keyword_variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod enum_keyword_variant {
// #![deny(unused_results)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be removed

include!(concat!(env!("OUT_DIR"), "/enum_keyword_variant.rs"));
}

#[test]
fn dummy() {}
2 changes: 2 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ mod debug;
#[cfg(test)]
mod deprecated_field;
#[cfg(test)]
mod enum_keyword_variant;
#[cfg(test)]
mod generic_derive;
#[cfg(test)]
mod message_encoding;
Expand Down
Loading