Skip to content

Commit 90b5e73

Browse files
hyperpolymathclaude
andcommitted
fix(rust): replace .expect("TODO") with documented invariants (9 sites)
9 prod sites in src/main.rs + src/api/mod.rs were carrying the .expect("TODO: handle error") anti-pattern. Each gets a documented invariant message that doubles as a SPARK proof candidate per the estate-wide Rust/SPARK direction: - 7 serde_json::to_string_pretty(&typed_value) sites: invariant "serializing a typed Rust value to JSON is infallible (no Serialize impl can fail)" — Serialize derive on owned structs cannot fail. - 1 LAST_REQUEST mutex .lock() site: invariant "LAST_REQUEST mutex never poisons — its critical section is sleep + assignment with no panic-able operations". - 1 std::fs::write site reusing the same serde invariant. cargo check: clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 83c47d3 commit 90b5e73

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/api/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ impl CloudflareClient {
311311

312312
/// Enforce 333ms minimum between requests.
313313
fn rate_limit(&self) {
314-
let mut last = LAST_REQUEST.lock().expect("TODO: handle error");
314+
let mut last = LAST_REQUEST.lock()
315+
.expect("LAST_REQUEST mutex never poisons — its critical section is a sleep + assignment with no panic-able operations");
315316
let elapsed = last.elapsed();
316317
let min_interval = Duration::from_millis(RATE_LIMIT_MS);
317318
if elapsed < min_interval {
@@ -545,7 +546,8 @@ impl CloudflareClient {
545546
let filename = domain.replace('.', "_");
546547
let path = dir.join(format!("{}.json", filename));
547548

548-
std::fs::write(&path, serde_json::to_string_pretty(&config).expect("TODO: handle error"))
549+
std::fs::write(&path, serde_json::to_string_pretty(&config)
550+
.expect("serializing a typed Rust value to JSON is infallible (no Serialize impl can fail)"))
549551
.map_err(|e| format!("Failed to write config: {}", e))?;
550552

551553
Ok(path.to_string_lossy().to_string())

src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ fn cmd_audit(
273273
"score": format!("{:.1}%", score),
274274
"findings": all_findings,
275275
});
276-
println!("{}", serde_json::to_string_pretty(&report).expect("TODO: handle error"));
276+
println!("{}", serde_json::to_string_pretty(&report).expect("serializing a typed Rust value to JSON is infallible (no Serialize impl can fail)"));
277277
} else {
278278
println!("CloudGuard Audit Report");
279279
println!("=======================");
@@ -298,7 +298,7 @@ fn cmd_audit(
298298
"score": format!("{:.1}%", score),
299299
"findings": all_findings,
300300
});
301-
std::fs::write(&path, serde_json::to_string_pretty(&report).expect("TODO: handle error"))
301+
std::fs::write(&path, serde_json::to_string_pretty(&report).expect("serializing a typed Rust value to JSON is infallible (no Serialize impl can fail)"))
302302
.map_err(|e| format!("Failed to write report to {}: {}", path, e))?;
303303
eprintln!("Report written to {}", path);
304304
}
@@ -585,7 +585,7 @@ fn cmd_dns_list(
585585
let records = client.list_dns_records(&zone.id)?;
586586

587587
if json_output {
588-
println!("{}", serde_json::to_string_pretty(&records).expect("TODO: handle error"));
588+
println!("{}", serde_json::to_string_pretty(&records).expect("serializing a typed Rust value to JSON is infallible (no Serialize impl can fail)"));
589589
} else {
590590
println!("DNS Records for {} ({} records)", domain, records.len());
591591
println!("{:<8} {:<30} {:<50} {:<6} {}", "Type", "Name", "Content", "TTL", "Proxy");
@@ -620,7 +620,7 @@ fn cmd_dns_add(
620620
let record = client.create_dns_record(&zone.id, record_type, name, content, ttl, proxied)?;
621621

622622
if json_output {
623-
println!("{}", serde_json::to_string_pretty(&record).expect("TODO: handle error"));
623+
println!("{}", serde_json::to_string_pretty(&record).expect("serializing a typed Rust value to JSON is infallible (no Serialize impl can fail)"));
624624
} else {
625625
println!("Created {} record: {} -> {}", record_type, name, content);
626626
}
@@ -710,7 +710,7 @@ fn cmd_zones_list(
710710
let zones = client.list_zones()?;
711711

712712
if json_output {
713-
println!("{}", serde_json::to_string_pretty(&zones).expect("TODO: handle error"));
713+
println!("{}", serde_json::to_string_pretty(&zones).expect("serializing a typed Rust value to JSON is infallible (no Serialize impl can fail)"));
714714
} else {
715715
println!("Zones ({} total)", zones.len());
716716
println!("{:<30} {:<10} {:<12} {}", "Domain", "Status", "Plan", "ID");
@@ -732,7 +732,7 @@ fn cmd_zones_status(
732732
let zone = client.find_zone_by_name(domain)?;
733733

734734
if json_output {
735-
println!("{}", serde_json::to_string_pretty(&zone).expect("TODO: handle error"));
735+
println!("{}", serde_json::to_string_pretty(&zone).expect("serializing a typed Rust value to JSON is infallible (no Serialize impl can fail)"));
736736
} else {
737737
println!("Zone: {}", zone.name);
738738
println!(" ID: {}", zone.id);
@@ -752,7 +752,7 @@ fn cmd_pages_list(
752752
let projects = client.list_pages_projects()?;
753753

754754
if json_output {
755-
println!("{}", serde_json::to_string_pretty(&projects).expect("TODO: handle error"));
755+
println!("{}", serde_json::to_string_pretty(&projects).expect("serializing a typed Rust value to JSON is infallible (no Serialize impl can fail)"));
756756
} else {
757757
println!("Pages Projects ({} total)", projects.len());
758758
println!("{:<30} {:<40} {:<15} {}", "Name", "Subdomain", "Branch", "Domains");

0 commit comments

Comments
 (0)