pub enum WriteAction {
Insert,
Update,
}
pub fn sanitize_identifier(s: &str) -> String {
let mut result = String::new();
for c in s.chars() {
if c.is_alphanumeric() || c == '_' {
result.push(c);
} else {
result.push_str("__");
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sanitize_identifier_empty() {
assert_eq!(sanitize_identifier(""), "");
}
#[test]
fn test_sanitize_identifier_alphanumeric() {
assert_eq!(sanitize_identifier("HelloWorld123"), "HelloWorld123");
}
#[test]
fn test_sanitize_identifier_underscores() {
assert_eq!(sanitize_identifier("hello_world_123"), "hello_world_123");
}
#[test]
fn test_sanitize_identifier_non_alphanumeric() {
assert_eq!(sanitize_identifier("hello-world.123"), "hello__world__123");
}
#[test]
fn test_sanitize_identifier_only_non_alphanumeric() {
assert_eq!(sanitize_identifier("!@#"), "______");
}
#[test]
fn test_sanitize_identifier_unicode() {
assert_eq!(sanitize_identifier("hello🚀"), "hello__");
assert_eq!(sanitize_identifier("helloö"), "helloö");
}
}