pub fn truncate(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
return s.to_string();
}
if max_len == 0 {
return String::new();
}
let mut end = max_len;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
if end == 0 {
return String::new();
}
let mut end = end.saturating_sub(1);
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
if end == 0 {
return s
.chars()
.next()
.map(|c| format!("{c}…"))
.unwrap_or_default();
}
format!("{}…", &s[..end])
}