Skip to main content

opensession_summary/
text.rs

1pub fn compact_summary_snippet(text: &str, max_chars: usize) -> String {
2    let compact = text
3        .split_whitespace()
4        .filter(|token| !token.is_empty())
5        .collect::<Vec<_>>()
6        .join(" ");
7    if compact.is_empty() {
8        return String::new();
9    }
10    if compact.chars().count() <= max_chars {
11        return compact;
12    }
13    let mut out = String::new();
14    for ch in compact.chars().take(max_chars.saturating_sub(1)) {
15        out.push(ch);
16    }
17    out.push('…');
18    out
19}
20
21#[cfg(test)]
22mod tests {
23    use super::compact_summary_snippet;
24
25    #[test]
26    fn compact_summary_snippet_collapses_whitespace() {
27        let compact = compact_summary_snippet("  hello   world   \n  from   test ", 120);
28        assert_eq!(compact, "hello world from test");
29    }
30
31    #[test]
32    fn compact_summary_snippet_truncates_with_ellipsis() {
33        let compact = compact_summary_snippet("abcdefghij", 6);
34        assert_eq!(compact, "abcde…");
35    }
36}