use heck::{ToLowerCamelCase, ToPascalCase, ToSnakeCase};
pub fn to_snake_case(s: &str) -> String {
s.to_snake_case()
}
pub fn to_pascal_case(s: &str) -> String {
s.to_pascal_case()
}
pub fn to_camel_case(s: &str) -> String {
s.to_lower_camel_case()
}
pub fn encode(s: &str) -> String {
s.replace('~', "~0").replace('/', "~1")
}
pub fn decode(s: &str) -> String {
s.replace("~1", "/").replace("~0", "~")
}
pub fn ref_name(ref_path: &str) -> String {
ref_path
.rsplit('/')
.next()
.unwrap_or(ref_path)
.to_pascal_case()
}
pub fn re_replace(content: &str, re: &str, replace: &str) -> String {
regex::Regex::new(re)
.unwrap()
.replace_all(content, replace)
.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encode_decode() {
assert_eq!(encode("application/json"), "application~1json");
assert_eq!(decode("application~1json"), "application/json");
assert_eq!(decode(&encode("a/b~c")), "a/b~c");
}
#[test]
fn test_case_conversion() {
assert_eq!(to_snake_case("MyPascalCase"), "my_pascal_case");
assert_eq!(to_pascal_case("my_snake_case"), "MySnakeCase");
assert_eq!(to_camel_case("my_snake_case"), "mySnakeCase");
}
#[test]
fn test_re_replace() {
assert_eq!(
re_replace("/files/{path_file}", r"(\{\s*)path", r"$1*path"),
"/files/{*path_file}"
);
}
}