const MAX_STEM_CHARS: usize = 64;
fn is_edge(c: char) -> bool {
c == '-' || c == '.'
}
#[must_use]
pub fn sanitize_stem(raw: Option<&str>, fallback: &str) -> String {
let cleaned: String = raw
.unwrap_or("")
.trim()
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-') {
c
} else {
'-'
}
})
.collect();
let capped: String = cleaned
.trim_matches(is_edge)
.chars()
.take(MAX_STEM_CHARS)
.collect();
let stem = capped.trim_matches(is_edge);
if stem.is_empty() {
fallback.to_string()
} else {
stem.to_string()
}
}
#[cfg(test)]
mod tests {
use super::sanitize_stem;
#[test]
fn path_traversal_is_flattened() {
assert_eq!(sanitize_stem(Some("../../etc/passwd"), "x"), "etc-passwd");
assert_eq!(sanitize_stem(Some("..\\..\\win.ini"), "x"), "win.ini");
}
#[test]
fn spaces_become_dashes() {
assert_eq!(
sanitize_stem(Some("Quarterly Report"), "x"),
"Quarterly-Report"
);
}
#[test]
fn bidi_and_control_characters_are_replaced() {
assert_eq!(sanitize_stem(Some("a\u{202e}gpj.exe"), "x"), "a-gpj.exe");
assert_eq!(sanitize_stem(Some("a\nb\0c"), "x"), "a-b-c");
}
#[test]
fn empty_and_missing_use_the_fallback() {
assert_eq!(sanitize_stem(None, "document"), "document");
assert_eq!(sanitize_stem(Some(" "), "document"), "document");
assert_eq!(sanitize_stem(Some("///"), "document"), "document");
}
#[test]
fn length_is_capped() {
assert_eq!(sanitize_stem(Some(&"a".repeat(100)), "x").len(), 64);
}
#[test]
fn a_cut_never_leaves_a_trailing_dot_or_dash() {
let raw = format!("{}.-b", "a".repeat(62));
let s = sanitize_stem(Some(&raw), "x");
assert_eq!(s, "a".repeat(62));
}
}