//! Pure helpers for cgroup v2 **directory naming** (no filesystem I/O).

/// Sanitize `spec.id` for use as a single path segment under a cgroup parent.
pub fn sanitize_cgroup_leaf_segment(id: &str) -> String {
    let s: String = id
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '_' || c == '-' {
                c
            } else {
                '_'
            }
        })
        .take(64)
        .collect();
    if s.is_empty() {
        "cell".into()
    } else {
        s
    }
}

#[cfg(test)]
mod tests {
    use super::sanitize_cgroup_leaf_segment;

    #[test]
    fn replaces_special_chars_and_caps_length() {
        assert_eq!(sanitize_cgroup_leaf_segment("ab/cd@ef!"), "ab_cd_ef_");
        let long = "a".repeat(100);
        assert_eq!(sanitize_cgroup_leaf_segment(&long).len(), 64);
        assert_eq!(sanitize_cgroup_leaf_segment(""), "cell");
    }
}
