#[must_use]
pub fn truncate_to_chars(s: &str, max_chars: usize) -> String {
if max_chars == 0 {
return String::new();
}
let count = s.chars().count();
if count <= max_chars {
s.to_owned()
} else {
let truncated: String = s.chars().take(max_chars).collect();
format!("{truncated}\u{2026}")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn short_string_unchanged() {
assert_eq!(truncate_to_chars("hello", 10), "hello");
}
#[test]
fn exact_length_unchanged() {
assert_eq!(truncate_to_chars("hello", 5), "hello");
}
#[test]
fn appends_ellipsis() {
assert_eq!(truncate_to_chars("hello world", 5), "hello\u{2026}");
}
#[test]
fn zero_max_returns_empty() {
assert_eq!(truncate_to_chars("hello", 0), "");
}
#[test]
fn unicode_handled_correctly() {
let s = "πππππextra";
assert_eq!(truncate_to_chars(s, 5), "πππππ\u{2026}");
}
}