//! Utility helpers for claudbg. /// Returns the first 8 characters of a session/agent ID for compact display. /// /// If the ID is shorter than 8 characters, returns the full ID. pub fn short_id(id: &str) -> &str { &id[..id.len().min(8)] } /// Escapes special XML characters in a string for safe embedding in XML content. /// /// The following substitutions are applied: /// - `&` → `&` /// - `<` → `<` /// - `>` → `>` /// - `"` → `"` /// - `'` → `'` pub fn xml_escape(s: &str) -> String { let mut out = String::with_capacity(s.len()); for ch in s.chars() { match ch { '&' => out.push_str("&"), '<' => out.push_str("<"), '>' => out.push_str(">"), '"' => out.push_str("""), '\'' => out.push_str("'"), other => out.push(other), } } out } #[cfg(test)] mod tests { use super::*; /// `short_id` returns the first 8 characters of a 36-character UUID. #[test] fn short_id_uuid() { let uuid = "550e8400-e29b-41d4-a716-446655440000"; assert_eq!(short_id(uuid), "550e8400"); } /// `short_id` returns the full string when it is shorter than 8 characters. #[test] fn short_id_short_string() { assert_eq!(short_id("abc"), "abc"); } /// `short_id` returns an empty string for an empty input. #[test] fn short_id_empty_string() { assert_eq!(short_id(""), ""); } /// `short_id` returns exactly 8 characters when input is exactly 8 characters. #[test] fn short_id_exact_length() { assert_eq!(short_id("12345678"), "12345678"); } /// `xml_escape` replaces `&` with `&`. #[test] fn xml_escape_ampersand() { assert_eq!(xml_escape("a & b"), "a & b"); } /// `xml_escape` replaces `<` with `<` and `>` with `>`. #[test] fn xml_escape_angle_brackets() { assert_eq!(xml_escape(""), "<tag>"); } /// `xml_escape` replaces `"` with `"`. #[test] fn xml_escape_double_quote() { assert_eq!(xml_escape(r#"say "hello""#), "say "hello""); } /// `xml_escape` replaces `'` with `'`. #[test] fn xml_escape_single_quote() { assert_eq!(xml_escape("it's"), "it's"); } /// `xml_escape` handles a string with multiple special characters. #[test] fn xml_escape_multiple() { assert_eq!( xml_escape(r#"it's"#), "<a href="x&y">it's</a>" ); } /// `xml_escape` returns the input unchanged if no special characters are present. #[test] fn xml_escape_no_special() { assert_eq!(xml_escape("hello world"), "hello world"); } }