Truncate a string to a maximum length, adding ellipsis if needed. This function handles Unicode characters correctly by counting grapheme clusters rather than bytes, ensuring we never split a multi-byte character. # Arguments `s` - The string to truncate `max_len` - Maximum character length including ellipsis # Returns The truncated string. # Example ```rust,ignore assert_eq!(truncate_strin
(s: &str, max_len: usize)
| 561 | /// assert_eq!(truncate_string("Short", 8), "Short"); |
| 562 | /// ``` |
| 563 | pub fn truncate_string(s: &str, max_len: usize) -> String { |
| 564 | let char_count = s.chars().count(); |
| 565 | if char_count <= max_len { |
| 566 | s.to_string() |
| 567 | } else if max_len <= 3 { |
| 568 | s.chars().take(max_len).collect() |
| 569 | } else { |
| 570 | let truncated: String = s.chars().take(max_len - 3).collect(); |
| 571 | format!("{}...", truncated) |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /// Format an author for display from an `Author` struct. |
| 576 | /// |