pub fn truncate_string(input: &str, max_chars: usize) -> String {
if input.chars().count() > max_chars {
if max_chars < 3 {
input.chars().take(max_chars).collect::<String>()
} else {
format!(
"{}...",
input.chars().take(max_chars - 3).collect::<String>()
)
}
} else {
input.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_truncate_no_truncation() {
assert_eq!(truncate_string("hello", 10), "hello");
assert_eq!(truncate_string("hello", 5), "hello");
}
#[test]
fn test_truncate_with_truncation() {
assert_eq!(truncate_string("hello world", 10), "hello w...");
assert_eq!(truncate_string("hello world", 5), "he...");
}
#[test]
fn test_truncate_short_limit() {
assert_eq!(truncate_string("hello world", 3), "..."); assert_eq!(truncate_string("hello world", 2), "he"); assert_eq!(truncate_string("hello world", 1), "h"); assert_eq!(truncate_string("hello world", 0), ""); }
#[test]
fn test_truncate_unicode() {
assert_eq!(truncate_string("你好世界", 10), "你好世界"); assert_eq!(truncate_string("你好世界", 4), "你好世界");
assert_eq!(truncate_string("你好世界", 3), "..."); assert_eq!(truncate_string("你好世界", 2), "你好"); }
#[test]
fn test_truncate_empty() {
assert_eq!(truncate_string("", 10), "");
assert_eq!(truncate_string("", 0), "");
}
}