forge_core_utils/text.rs
1use regex::Regex;
2use uuid::Uuid;
3
4pub fn git_branch_id(input: &str) -> String {
5 // 1. lowercase
6 let lower = input.to_lowercase();
7
8 // 2. replace non-alphanumerics with hyphens
9 let re = Regex::new(r"[^a-z0-9]+").unwrap();
10 let slug = re.replace_all(&lower, "-");
11
12 // 3. trim extra hyphens
13 let trimmed = slug.trim_matches('-');
14
15 // 4. take up to 16 chars, then trim trailing hyphens again
16 let cut: String = trimmed.chars().take(16).collect();
17 cut.trim_end_matches('-').to_string()
18}
19
20pub fn short_uuid(u: &Uuid) -> String {
21 // to_simple() gives you a 32-char hex string with no hyphens
22 let full = u.simple().to_string();
23 full.chars().take(4).collect() // grab the first 4 chars
24}