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
Prev Previous commit
Next Next commit
small fixes
  • Loading branch information
MixusMinimax committed Mar 15, 2024
commit 8e77510b7e57626b016e0242b89126f0d3126bde
9 changes: 4 additions & 5 deletions prost-build/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ pub fn sanitize_identifier(s: impl AsRef<str>) -> String {
// 2018 reserved keywords.
| "async" | "await" | "try" => format!("r#{}", ident),
// the following keywords are not supported as raw identifiers and are therefore suffixed with an underscore.
"Self" | "self" | "super" | "extern" | "crate" => format!("{}_", ident),
"_" | "super" | "self" | "Self" | "extern" | "crate" => format!("{}_", ident),
// the following keywords begin with a number and are therefore prefixed with an underscore.
s if s.len() > 0 && s.chars().next().unwrap().is_numeric() => format!("_{}", ident),
s if s.starts_with(|c: char| c.is_numeric()) => format!("_{}", ident),
_ => ident.to_string(),
}
}

/// Converts a `camelCase` or `SCREAMING_SNAKE_CASE` identifier to a `lower_snake` case Rust field
/// identifier.
pub fn to_snake(s: impl AsRef<str>) -> String {
sanitize_identifier(s.to_snake_case())
sanitize_identifier(s.as_ref().to_snake_case())
}

/// Converts a `snake_case` identifier to an `UpperCamel` case Rust type identifier.
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn strip_enum_prefix(prefix: &str, name: &str) -> String {
} else {
name
};
to_upper_camel(stripped)
sanitize_identifier(stripped)
}

#[cfg(test)]
Expand Down Expand Up @@ -198,6 +198,5 @@ mod tests {
fn test_strip_enum_prefix_resulting_in_keyword() {
assert_eq!(strip_enum_prefix("Foo", "FooBar"), "Bar");
assert_eq!(strip_enum_prefix("Foo", "FooSelf"), "Self_");
assert_eq!(strip_enum_prefix("Foo", "FooCrate"), "Crate");
}
}
Loading