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:
ProofOfConcept 2026-03-08 21:13:02 -04:00
parent e24dee6bdf
commit 52523403c5
16 changed files with 85 additions and 129 deletions

View file

@ -637,10 +637,11 @@ fn cmd_search(terms: &[String], expand: bool) -> Result<(), String> {
let weight = view.node_weight(k);
println!(" ~ [{:.2}] {}", weight, k);
if let Some(content) = view.node_content(k) {
let snippet: String = content.lines()
.find(|l| !l.trim().is_empty() && !l.starts_with('#'))
.unwrap_or("")
.chars().take(100).collect();
let snippet = util::first_n_chars(
content.lines()
.find(|l| !l.trim().is_empty() && !l.starts_with('#'))
.unwrap_or(""),
100);
if !snippet.is_empty() {
println!(" {}", snippet);
}
@ -671,13 +672,11 @@ fn cmd_init() -> Result<(), String> {
let mut store = store::Store::load()?;
let count = store.init_from_markdown()?;
for key in &cfg.core_nodes {
if !store.nodes.contains_key(key.as_str()) {
if key == "identity" {
let default = include_str!("../defaults/identity.md");
store.upsert(key, default)
.map_err(|e| format!("seed {}: {}", key, e))?;
println!("Seeded {} in store", key);
}
if !store.nodes.contains_key(key) && key == "identity" {
let default = include_str!("../defaults/identity.md");
store.upsert(key, default)
.map_err(|e| format!("seed {}: {}", key, e))?;
println!("Seeded {} in store", key);
}
}
store.save()?;
@ -1319,12 +1318,7 @@ fn cmd_trace(key: &[String]) -> Result<(), String> {
}
// Show content preview
let preview = if node.content.len() > 200 {
let end = node.content.floor_char_boundary(200);
format!("{}...", &node.content[..end])
} else {
node.content.clone()
};
let preview = util::truncate(&node.content, 200, "...");
println!("\n{}\n", preview);
// Walk neighbors, grouped by node type
@ -1354,7 +1348,7 @@ fn cmd_trace(key: &[String]) -> Result<(), String> {
if !episodic_weekly.is_empty() {
println!("Weekly digests:");
for (k, s, n) in &episodic_weekly {
let preview = n.content.lines().next().unwrap_or("").chars().take(80).collect::<String>();
let preview = util::first_n_chars(n.content.lines().next().unwrap_or(""), 80);
println!(" [{:.2}] {}{}", s, k, preview);
}
}
@ -1362,7 +1356,7 @@ fn cmd_trace(key: &[String]) -> Result<(), String> {
if !episodic_daily.is_empty() {
println!("Daily digests:");
for (k, s, n) in &episodic_daily {
let preview = n.content.lines().next().unwrap_or("").chars().take(80).collect::<String>();
let preview = util::first_n_chars(n.content.lines().next().unwrap_or(""), 80);
println!(" [{:.2}] {}{}", s, k, preview);
}
}
@ -1370,9 +1364,11 @@ fn cmd_trace(key: &[String]) -> Result<(), String> {
if !episodic_session.is_empty() {
println!("Session entries:");
for (k, s, n) in &episodic_session {
let preview = n.content.lines()
.find(|l| !l.is_empty() && !l.starts_with("<!--"))
.unwrap_or("").chars().take(80).collect::<String>();
let preview = util::first_n_chars(
n.content.lines()
.find(|l| !l.is_empty() && !l.starts_with("<!--"))
.unwrap_or(""),
80);
println!(" [{:.2}] {}", s, k);
if !n.source_ref.is_empty() {
println!(" ↳ source: {}", n.source_ref);
@ -1625,7 +1621,7 @@ fn cmd_journal_ts_migrate() -> Result<(), String> {
}
}
if let Some(node) = store.nodes.get_mut(key) {
node.created_at = node.timestamp as i64;
node.created_at = node.timestamp;
node.version += 1;
updated += 1;
}
@ -1811,7 +1807,7 @@ fn cmd_history(key: &[String], full: bool) -> Result<(), String> {
node.version, ts, node.provenance.label(), node.weight, content_len);
eprintln!("{}", node.content);
} else {
let preview: String = node.content.chars().take(120).collect();
let preview = util::first_n_chars(&node.content, 120);
let preview = preview.replace('\n', "\\n");
eprintln!(" v{:<3} {} {:24} w={:.3} {}b",
node.version, ts, node.provenance.label(), node.weight, content_len);
@ -2065,18 +2061,12 @@ fn extract_title(content: &str) -> String {
let stripped = line.trim();
if stripped.is_empty() { continue; }
if date_re.is_match(stripped) && stripped.len() < 25 { continue; }
if stripped.starts_with("## ") {
return stripped[3..].to_string();
} else if stripped.starts_with("# ") {
return stripped[2..].to_string();
if let Some(h) = stripped.strip_prefix("## ") {
return h.to_string();
} else if let Some(h) = stripped.strip_prefix("# ") {
return h.to_string();
} else {
return if stripped.len() > 70 {
let mut end = 67;
while !stripped.is_char_boundary(end) { end -= 1; }
format!("{}...", &stripped[..end])
} else {
stripped.to_string()
};
return util::truncate(stripped, 67, "...");
}
}
String::from("(untitled)")