pub fn from_branch(branch: &str) -> String {
let mut out = String::new();
let mut pending_sep = false;
for ch in branch.chars() {
if ch.is_ascii_alphanumeric() {
if pending_sep && !out.is_empty() {
out.push('-');
}
pending_sep = false;
out.extend(ch.to_lowercase());
} else {
pending_sep = true;
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slashes_become_kebab() {
assert_eq!(from_branch("feat/auth/oauth"), "feat-auth-oauth");
}
#[test]
fn plain_branch_lowercased() {
assert_eq!(from_branch("Hotfix"), "hotfix");
}
#[test]
fn underscores_and_dots() {
assert_eq!(from_branch("feat_auth.oauth"), "feat-auth-oauth");
}
#[test]
fn trims_leading_trailing_separators() {
assert_eq!(from_branch("/feat/login/"), "feat-login");
}
#[test]
fn empty_and_only_separators() {
assert_eq!(from_branch(""), "");
assert_eq!(from_branch("///"), "");
}
}