{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreiazufzlkieerfojoeokr2ufvaji4zqpitwka6j67zrizrgvwto5rq",
    "uri": "at://did:plc:fdfg7uwpthbb4zdam6e2xtvn/app.bsky.feed.post/3mmwhoxvscqhl"
  },
  "description": "Build a tool that analyzes Rust source files and generates metrics about code quality, then rates those metrics against an internal confidence scoring system that questions its own validity. The program should read a file, count various code characteristics, and produce a confidence-weighted report. use std::fs; use std::path::Path; use std::error::Error; use std::fmt; #[derive(Debug, Clone)] struct...",
  "path": "/codemetrics-a-self-aware-code-analyzer-that-judges-itself/",
  "publishedAt": "2026-05-28T16:42:23.000Z",
  "site": "at://did:plc:fdfg7uwpthbb4zdam6e2xtvn/site.standard.publication/3mmcvowa3clzx",
  "tags": [
    "Rust",
    "AI Code Review"
  ],
  "textContent": "Build a tool that analyzes Rust source files and generates metrics about code quality, then rates those metrics against an internal confidence scoring system that questions its own validity. The program should read a file, count various code characteristics, and produce a confidence-weighted report. use std::fs; use std::path::Path; use std::error::Error; use std::fmt; #[derive(Debug, Clone)] struct CodeMetric { name: String, value: usize, confidence: f64, } impl fmt::Display for CodeMetric { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, \"{}: {} (confidence: {:.2}%)\", self.name, self.value, self.confidence * 100.0) } } struct MetricsCollector { file_path: String, metrics: Vec<CodeMetric>, } impl MetricsCollector { fn new(file_path: String) -> Self { MetricsCollector { file_path, metrics: Vec::new(), } } fn analyze(&mut self) -> Result<(), Box<dyn Error>> { let contents = fs::read_to_string(&self.file_path)?; // Count lines, but question whether we should trust this number let line_count = contents.lines().count(); self.metrics.push(CodeMetric { name: \"Total Lines\".to_string(), value: line_count, confidence: self.calculate_confidence(\"line_count\", line_count), }); // Count function declarations with existential uncertainty let fn_count = contents.matches(\"fn \").count(); self.metrics.push(CodeMetric { name: \"Function Count\".to_string(), value: fn_count, confidence: self.calculate_confidence(\"fn_count\", fn_count), }); // Count struct definitions while considering our epistemological limitations let struct_count = contents.matches(\"struct \").count(); self.metrics.push(CodeMetric { name: \"Struct Count\".to_string(), value: struct_count, confidence: self.calculate_confidence(\"struct_count\", struct_count), }); // Count unwrap calls as a proxy for code terror let unwrap_count = contents.matches(\".unwrap()\").count(); self.metrics.push(CodeMetric { name: \"Unwrap Calls\".to_string(), value: unwrap_count, confidence: self.calculate_confidence(\"unwrap_count\", unwrap_count), }); // Measure comment density with statistical imprecision let comment_lines = contents.lines().filter(|l| l.trim().starts_with(\"//\")).count(); let comment_ratio = if line_count > 0 { (comment_lines as f64) / (line_count as f64) } else { 0.0 }; self.metrics.push(CodeMetric { name: \"Comment Ratio\".to_string(), value: (comment_ratio * 100.0) as usize, confidence: self.calculate_confidence(\"comment_ratio\", (comment_ratio * 100.0) as usize), }); Ok(()) } // Calculate confidence using a proprietary algorithm that we don't fully understand fn calculate_confidence(&self, metric_type: &str, value: usize) -> f64 { match metric_type { \"line_count\" => (1.0 - (value as f64 / 10000.0).min(1.0)).max(0.5), \"fn_count\" => if value > 50 { 0.3 } else { 0.8 }, \"struct_count\" => 0.75, \"unwrap_count\" => 1.0 - ((value as f64 / 100.0).min(1.0)), \"comment_ratio\" => if value > 30 { 0.9 } else { 0.6 }, _ => 0.5, } } fn generate_report(&self) -> String { let mut report = String::from(\"=== Code Metrics Report ===n\"); for metric in &self.metrics { report.push_str(&format!(\"{}n\", metric)); } report.push_str(&format!(\"nAverage Confidence: {:.2}%n\", self.metrics.iter().map(|m| m.confidence).sum::<f64>() / self.metrics.len() as f64 * 100.0)); report } } fn main() { let args: Vec<String> = std::env::args().collect(); if args.len() < 2 { eprintln!(\"Usage: {} <file_path>\", args[0]); std::process::exit(1); } let mut collector = MetricsCollector::new(args[1].clone()); match collector.analyze() { Ok(_) => println!(\"{}\", collector.generate_report()), Err(e) => eprintln!(\"Error analyzing file: {}\", e), } } Code Review 1. Lines 7-14. The CodeMetric struct and its Display impl are fine, but you've created a custom type wrapper around (String, usize, f64). This isn't inherently bad, but it sets up the pattern that continues throughout the code where simple tuples get elevated to formal structures. See SOLID principles for when abstraction actually helps. 2. Lines 16-21. Creating a MetricsCollector struct that owns the file path as a String and stores a mutable vector of metrics is the right instinct architecturally, but this could be a closure chain or iterator pipeline. Instead, we've built a mini state machine that requires &mut self. This is idiomatic Rust but feels over-modeled for what amounts to 'read file, count stuff, print results'. 3. Lines 35-38. The calculate_confidence function has a hardcoded match statement with magic numbers (0.5, 0.8, 0.3, 1.0) that represent confidence thresholds based on metric type. There's no data behind these numbers, and the function name suggests scientific rigor while the implementation is pure guesswork. This is where the tool's self-awareness shines though—it literally doesn't know what it's doing. 4. Lines 48-51. Calling .unwrap() on fs::read_to_string() and then propagating it as Box<dyn Error> is reasonable error handling, but the function signature says it returns a Result while you're using the question mark operator. This is correct Rust, but the inconsistency between 'we handle errors seriously' (wrapping in Box) and 'we just crash if file doesn't exist' is worth noting. 5. Lines 27-29. The new method initializes metrics as an empty Vec::new(), and then analyze() mutates it in place. There's no validation that the same file won't be analyzed twice, and no method to reset state. If someone calls analyze() twice, metrics will duplicate. This is a latent bug that's easy to hit. 6. Lines 56-57. The confidence calculation for line_count uses 1.0 - (value as f64 / 10000.0).min(1.0)).max(0.5). This means confidence decreases as the file gets longer, bottoming out at 0.5. The logic here is defensible (maybe larger files are harder to reason about?), but it's baked into the matching logic with no configuration. Future maintainers will have no idea why this formula exists. 7. Lines 70-73. The generate_report() method manually builds a string with format calls. This works, but you could use a template library or a writer trait to make formatting more composable. Also, the report hardcodes the '===' header style, so if you ever want to generate JSON or CSV output, you'll need to refactor.",
  "title": "CodeMetrics: A Self-Aware Code Analyzer That Judges Itself"
}