use crate::error::JavaGenError;
pub(crate) const JAVA_RESERVED: &[&str] = &[
"abstract",
"assert",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extends",
"final",
"finally",
"float",
"for",
"goto",
"if",
"implements",
"import",
"instanceof",
"int",
"interface",
"long",
"native",
"new",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"strictfp",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"try",
"void",
"volatile",
"while",
"true",
"false",
"null",
"record",
"sealed",
"var",
"yield",
"non-sealed",
"permits",
"exports",
"module",
"open",
"opens",
"provides",
"requires",
"to",
"transitive",
"uses",
"with",
];
#[must_use]
pub fn is_reserved(name: &str) -> bool {
JAVA_RESERVED.contains(&name)
}
pub fn sanitize_identifier(name: &str) -> Result<String, JavaGenError> {
if name.is_empty() {
return Err(JavaGenError::InvalidName {
name: name.to_string(),
reason: "empty identifier".to_string(),
});
}
if is_reserved(name) {
Ok(format!("{name}_"))
} else {
Ok(name.to_string())
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn class_is_reserved() {
assert!(is_reserved("class"));
}
#[test]
fn record_is_reserved() {
assert!(is_reserved("record"));
}
#[test]
fn sealed_is_reserved() {
assert!(is_reserved("sealed"));
}
#[test]
fn var_yield_reserved() {
assert!(is_reserved("var"));
assert!(is_reserved("yield"));
}
#[test]
fn foo_is_not_reserved() {
assert!(!is_reserved("Foo"));
}
#[test]
fn sanitize_keyword_appends_underscore() {
assert_eq!(sanitize_identifier("class").expect("ok"), "class_");
assert_eq!(sanitize_identifier("int").expect("ok"), "int_");
}
#[test]
fn sanitize_normal_passthrough() {
assert_eq!(sanitize_identifier("Foo").expect("ok"), "Foo");
}
#[test]
fn sanitize_empty_errors() {
assert!(matches!(
sanitize_identifier(""),
Err(JavaGenError::InvalidName { .. })
));
}
#[test]
fn list_contains_at_least_50_keywords() {
assert!(JAVA_RESERVED.len() >= 50);
}
}