1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Shared helpers for command implementations.
//!
//! Common utilities for pagination, body building, field selection,
//! and other cross-cutting concerns used by multiple commands.
/// Truncates a string to `max_len` characters, appending "…" if truncated.
///
/// # Examples
///
/// ```
/// use gor::cmd::util::truncate;
///
/// assert_eq!(truncate("hello", 10), "hello");
/// assert_eq!(truncate("hello world", 5), "hello…");
/// ```
#[must_use]
pub fn truncate(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
s.to_string()
} else {
let mut truncated = s.chars().take(max_len).collect::<String>();
truncated.push('…');
truncated
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn truncate_short_string() {
assert_eq!(truncate("hi", 10), "hi");
}
#[test]
fn truncate_exact_length() {
assert_eq!(truncate("hello", 5), "hello");
}
#[test]
fn truncate_long_string() {
assert_eq!(truncate("hello world", 5), "hello…");
}
#[test]
fn truncate_empty() {
assert_eq!(truncate("", 5), "");
}
}