extract truncation helpers, fix clippy warnings, dedup batching loop
Add util::truncate() and util::first_n_chars() to replace 16 call sites doing the same floor_char_boundary or chars().take().collect() patterns. Deduplicate the batching loop in consolidate.rs (4 copies → 1 loop over an array). Fix all clippy warnings: redundant closures, needless borrows, collapsible if, unnecessary cast, manual strip_prefix. Net: -44 lines across 16 files.
This commit is contained in:
parent
e24dee6bdf
commit
52523403c5
16 changed files with 85 additions and 129 deletions
|
|
@ -12,3 +12,19 @@ pub fn memory_subdir(name: &str) -> Result<PathBuf, String> {
|
|||
.map_err(|e| format!("create {}: {}", dir.display(), e))?;
|
||||
Ok(dir)
|
||||
}
|
||||
|
||||
/// Truncate text to `max_len` bytes at a char boundary, appending `suffix`.
|
||||
/// Returns the original string if it's already short enough.
|
||||
pub fn truncate(text: &str, max_len: usize, suffix: &str) -> String {
|
||||
if text.len() <= max_len {
|
||||
text.to_string()
|
||||
} else {
|
||||
let end = text.floor_char_boundary(max_len);
|
||||
format!("{}{}", &text[..end], suffix)
|
||||
}
|
||||
}
|
||||
|
||||
/// Take the first `n` chars from a string.
|
||||
pub fn first_n_chars(s: &str, n: usize) -> String {
|
||||
s.chars().take(n).collect()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue