codedna 0.1.0

Rust-powered CLI for codebase intelligence: stack detection, architecture hints, LOC breakdown, repo maps, and dead-code heuristics.
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Reporter module.
//!
//! Renders `AnalysisResult` as either a human-readable CLI report or as
//! structured JSON matching the schema defined in `SPEC.md`.

use crate::analysis::AnalysisResult;

// ── Box-drawing header ────────────────────────────────────────────────────────

const HEADER_WIDTH: usize = 42;

fn print_header() {
    let title = "CodeDna Intelligence Report";
    let pad = (HEADER_WIDTH - 2 - title.len()) / 2;
    let top = "".repeat(HEADER_WIDTH - 2);
    let blank_pad = " ".repeat(HEADER_WIDTH - 2);

    println!("{top}");
    println!("{blank_pad}");
    println!("║{:>pad$}{title}{:>pad$}║", "", "");
    println!("{blank_pad}");
    println!("{top}");
}

fn section(title: &str) {
    println!("\n  \x1b[1m{title}\x1b[0m");
    println!("  {}", "".repeat(title.len()));
}

fn kv(label: &str, value: &str) {
    println!("  {label:<18} {value}");
}

// ── Full intelligence report ──────────────────────────────────────────────────

/// Print the full formatted intelligence report to stdout.
pub fn print_report(result: &AnalysisResult) {
    println!();
    print_header();

    // ── Summary ───────────────────────────────────────────────────────────────
    section("Summary");
    kv("Total LOC", &format_loc(result.total_loc));
    kv("Languages", &result.languages.len().to_string());
    kv("Frameworks", &result.frameworks.len().to_string());
    kv("Databases", &result.databases.len().to_string());
    kv("Infrastructure", &result.infrastructure.len().to_string());
    kv("Files scanned", &result.file_breakdown.len().to_string());
    kv("Dead files", &result.dead_code.len().to_string());
    kv(
        "Dependency links",
        &result
            .dependency_graph
            .values()
            .map(|v| v.len())
            .sum::<usize>()
            .to_string(),
    );

    // ── Project Type ─────────────────────────────────────────────────────────
    section("Project Type");
    println!("  {}", result.project_type);

    // ── Stack ─────────────────────────────────────────────────────────────────
    section("Stack");
    if result.frameworks.is_empty() {
        println!("  (no frameworks detected)");
    } else {
        println!("  {}", result.frameworks.join("  +  "));
    }

    // ── Databases ─────────────────────────────────────────────────────────────
    if !result.databases.is_empty() {
        section("Databases");
        println!("  {}", result.databases.join(",  "));
    }

    // ── Infrastructure ────────────────────────────────────────────────────────
    if !result.infrastructure.is_empty() {
        section("Infrastructure");
        println!("  {}", result.infrastructure.join(""));
    }

    // ── Architecture ──────────────────────────────────────────────────────────
    section("Architecture");
    println!("  {}", result.architecture);

    // ── Languages ─────────────────────────────────────────────────────────────
    section("Languages");
    if result.languages.is_empty() {
        println!("  (no source files detected)");
    } else {
        let mut langs: Vec<(&String, &usize)> = result.languages.iter().collect();
        langs.sort_by(|a, b| b.1.cmp(a.1));

        const BAR_WIDTH: usize = 28;
        for (lang, loc) in &langs {
            let pct = if result.total_loc > 0 {
                (**loc * 100) / result.total_loc
            } else {
                0
            };
            let filled = (pct * BAR_WIDTH) / 100;
            let empty = BAR_WIDTH - filled;
            let bar = format!(
                "\x1b[32m{}\x1b[90m{}\x1b[0m",
                "".repeat(filled),
                "".repeat(empty)
            );
            println!(
                "  {:<16}  {}  {:>3}%   {:>8} LOC",
                lang,
                bar,
                pct,
                format_loc(**loc),
            );
        }
    }

    // ── Top Files ─────────────────────────────────────────────────────────────
    let top_n = 5;
    let top_files: Vec<_> = result.file_breakdown.iter().take(top_n).collect();
    if !top_files.is_empty() {
        section(&format!("Top {top_n} Files by LOC"));
        for (i, info) in top_files.iter().enumerate() {
            println!(
                "  {}  {:<50}  {:>8} LOC   {}",
                i + 1,
                info.file.display(),
                format_loc(info.loc),
                info.language
            );
        }
    }

    // ── Dead Code ─────────────────────────────────────────────────────────────
    if !result.dead_code.is_empty() {
        section("Dead Code");
        for file in &result.dead_code {
            println!("  \x1b[33m{}\x1b[0m", file.display());
        }
    }

    println!();
}

// ── Stack-only output ─────────────────────────────────────────────────────────

/// Print detected tech stack (languages, frameworks, databases).
pub fn print_stack(result: &AnalysisResult) {
    println!();
    println!("  \x1b[1mStack\x1b[0m");
    println!();

    // Languages
    if !result.languages.is_empty() {
        let mut langs: Vec<(&String, &usize)> = result.languages.iter().collect();
        langs.sort_by(|a, b| b.1.cmp(a.1));
        println!("  Languages:");
        for (lang, loc) in langs {
            let pct = if result.total_loc > 0 {
                (loc * 100) / result.total_loc
            } else {
                0
            };
            println!("    {:<18} {:>7} LOC   ({}%)", lang, format_loc(*loc), pct);
        }
        println!();
    }

    // Frameworks
    if result.frameworks.is_empty() {
        println!("  Frameworks:  (none detected)");
    } else {
        println!("  Frameworks:");
        for fw in &result.frameworks {
            println!("{fw}");
        }
    }
    println!();

    // Databases
    if result.databases.is_empty() {
        println!("  Databases:   (none detected)");
    } else {
        println!("  Databases:");
        for db in &result.databases {
            println!("{db}");
        }
    }
    println!();
}

// ── File LOC breakdown ────────────────────────────────────────────────────────

/// Print per-file LOC breakdown, sorted by LOC descending.
pub fn print_files(result: &AnalysisResult) {
    println!();
    if result.file_breakdown.is_empty() {
        println!("  No source files found.");
        println!();
        return;
    }

    let col_w = longest_path_len(result) + 2;
    println!("  {:<col_w$} {:>8}   Language", "File", "LOC");
    println!("  {}", "".repeat(col_w + 22));

    for info in &result.file_breakdown {
        println!(
            "  {:<col_w$} {:>8}   {}",
            info.file.display(),
            format_loc(info.loc),
            info.language
        );
    }

    println!();
    println!("  {:<col_w$} {:>8}", "TOTAL", format_loc(result.total_loc));
    println!();
}

// ── Dead code output ──────────────────────────────────────────────────────────

/// Print dead code list.
pub fn print_dead_code(result: &AnalysisResult) {
    println!();
    if result.dead_code.is_empty() {
        println!("  \x1b[32m✓ No dead code detected.\x1b[0m");
        println!();
        return;
    }

    println!(
        "  \x1b[1mUnused files detected\x1b[0m  ({} file{})",
        result.dead_code.len(),
        if result.dead_code.len() == 1 { "" } else { "s" }
    );
    println!();
    for file in &result.dead_code {
        println!("  \x1b[33m  {}\x1b[0m", file.display());
    }
    println!();
}

// ── Framework usage output ────────────────────────────────────────────────────

/// Print files that import the given framework (by scanning the file breakdown
/// for files whose language matches the framework's ecosystem).
/// Print a list of files that import/use the given framework.
///
/// `matching_files` is the pre-computed result from
/// `framework_detector::detect_files_using_framework`.
pub fn print_framework_usage(name: &str, matching_files: &[std::path::PathBuf]) {
    println!();

    if matching_files.is_empty() {
        println!("  No files found using framework '\x1b[1m{name}\x1b[0m'.");
        println!();
        println!("  Possible reasons:");
        println!("    • The framework is not used in this repository");
        println!("    • The framework name was not recognised (try lowercase, e.g. 'react')");
        println!("    • Imports use a non-standard pattern not yet covered");
        println!();
        return;
    }

    println!(
        "  \x1b[1m{name}\x1b[0m used in {} file{}:",
        matching_files.len(),
        if matching_files.len() == 1 { "" } else { "s" }
    );
    println!();
    for file in matching_files {
        println!("  \x1b[33m  {}\x1b[0m", file.display());
    }
    println!();
}

// ── JSON output (Prompt 12) ───────────────────────────────────────────────────

/// Serialize `AnalysisResult` to JSON and print to stdout.
///
/// When `compact` is `false` (default) output is pretty-printed with 2-space
/// indentation.  When `compact` is `true` output is a single line — useful for
/// piping directly into other tools.
///
/// The JSON schema matches `SPEC.md`:
/// ```json
/// {
///   "project_type": "...",
///   "total_loc": 15780,
///   "languages": { "TypeScript": 12450 },
///   "frameworks": ["React"],
///   "databases": ["PostgreSQL"],
///   "architecture": "Frontend → API → Database",
///   "dead_code": ["src/utils/oldHelper.ts"],
///   "dependency_graph": { "src/server.ts": ["src/api/routes.ts"] },
///   "file_breakdown": [{ "file": "src/server.ts", "loc": 340, "language": "TypeScript" }]
/// }
/// ```
pub fn print_json(result: &AnalysisResult, compact: bool) {
    let output = if compact {
        serde_json::to_string(result)
    } else {
        serde_json::to_string_pretty(result)
    };

    match output {
        Ok(json) => println!("{json}"),
        Err(e) => eprintln!("error: JSON serialisation failed: {e}"),
    }
}

// ── JSON tests ────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analysis::FileInfo;
    use std::collections::HashMap;
    use std::path::PathBuf;

    fn minimal_result() -> AnalysisResult {
        let mut languages = HashMap::new();
        languages.insert("TypeScript".to_string(), 12450_usize);
        languages.insert("JavaScript".to_string(), 2100_usize);

        let mut dep_graph = HashMap::new();
        dep_graph.insert(
            PathBuf::from("src/server.ts"),
            vec![PathBuf::from("src/api/routes.ts")],
        );

        AnalysisResult {
            project_type: "Full-stack web application".to_string(),
            total_loc: 14550,
            languages,
            frameworks: vec!["React".to_string(), "Express".to_string()],
            databases: vec!["PostgreSQL".to_string()],
            infrastructure: vec!["Docker".to_string(), "GitHub Actions".to_string()],
            architecture: "Frontend → API → Database".to_string(),
            dead_code: vec![PathBuf::from("src/utils/oldHelper.ts")],
            dependency_graph: dep_graph,
            file_breakdown: vec![FileInfo {
                file: PathBuf::from("src/server.ts"),
                loc: 340,
                language: "TypeScript".to_string(),
            }],
        }
    }

    #[test]
    fn json_output_is_valid_and_contains_required_keys() {
        let result = minimal_result();
        let json = serde_json::to_string_pretty(&result).expect("serialisation failed");

        let parsed: serde_json::Value =
            serde_json::from_str(&json).expect("output is not valid JSON");

        // Required top-level keys from SPEC.md
        for key in &[
            "project_type",
            "total_loc",
            "languages",
            "frameworks",
            "databases",
            "architecture",
            "dead_code",
            "dependency_graph",
            "file_breakdown",
        ] {
            assert!(
                parsed.get(key).is_some(),
                "missing required key: {key}\nJSON:\n{json}"
            );
        }
    }

    #[test]
    fn json_languages_map_has_correct_values() {
        let result = minimal_result();
        let json = serde_json::to_string(&result).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

        assert_eq!(parsed["languages"]["TypeScript"], 12450);
        assert_eq!(parsed["languages"]["JavaScript"], 2100);
    }

    #[test]
    fn json_frameworks_is_array_of_strings() {
        let result = minimal_result();
        let json = serde_json::to_string(&result).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

        let fw = parsed["frameworks"]
            .as_array()
            .expect("frameworks must be array");
        assert!(fw.contains(&serde_json::json!("React")));
        assert!(fw.contains(&serde_json::json!("Express")));
    }

    #[test]
    fn json_dead_code_is_array_of_strings() {
        let result = minimal_result();
        let json = serde_json::to_string(&result).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

        let dc = parsed["dead_code"]
            .as_array()
            .expect("dead_code must be array");
        assert_eq!(dc.len(), 1);
        assert_eq!(dc[0], "src/utils/oldHelper.ts");
    }

    #[test]
    fn json_dependency_graph_keys_and_values_are_strings() {
        let result = minimal_result();
        let json = serde_json::to_string(&result).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

        let dg = parsed["dependency_graph"]
            .as_object()
            .expect("dependency_graph must be object");

        assert!(dg.contains_key("src/server.ts"));
        let deps = dg["src/server.ts"].as_array().expect("deps must be array");
        assert_eq!(deps[0], "src/api/routes.ts");
    }

    #[test]
    fn json_file_breakdown_entries_have_correct_shape() {
        let result = minimal_result();
        let json = serde_json::to_string(&result).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

        let fb = parsed["file_breakdown"]
            .as_array()
            .expect("file_breakdown must be array");
        assert_eq!(fb.len(), 1);

        let entry = &fb[0];
        assert_eq!(entry["file"], "src/server.ts");
        assert_eq!(entry["loc"], 340);
        assert_eq!(entry["language"], "TypeScript");
    }

    #[test]
    fn compact_json_is_single_line() {
        let result = minimal_result();
        let json = serde_json::to_string(&result).unwrap();
        // Compact output must not contain newlines
        assert!(!json.contains('\n'), "compact JSON should be a single line");
    }

    #[test]
    fn pretty_json_is_multi_line() {
        let result = minimal_result();
        let json = serde_json::to_string_pretty(&result).unwrap();
        assert!(
            json.contains('\n'),
            "pretty JSON should span multiple lines"
        );
    }

    #[test]
    fn json_round_trips_losslessly() {
        let result = minimal_result();
        let json = serde_json::to_string(&result).unwrap();
        let restored: AnalysisResult = serde_json::from_str(&json).unwrap();

        assert_eq!(restored.project_type, result.project_type);
        assert_eq!(restored.total_loc, result.total_loc);
        assert_eq!(restored.frameworks, result.frameworks);
        assert_eq!(restored.databases, result.databases);
        assert_eq!(restored.architecture, result.architecture);
        assert_eq!(restored.dead_code, result.dead_code);
        assert_eq!(
            restored.languages.get("TypeScript"),
            result.languages.get("TypeScript")
        );
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

/// Format a LOC number with thousands separators (e.g. 12450 → "12,450").
fn format_loc(n: usize) -> String {
    let s = n.to_string();
    let mut result = String::new();
    let chars: Vec<char> = s.chars().collect();
    for (i, c) in chars.iter().enumerate() {
        if i > 0 && (chars.len() - i).is_multiple_of(3) {
            result.push(',');
        }
        result.push(*c);
    }
    result
}

/// Return the display length of the longest file path in the breakdown.
fn longest_path_len(result: &AnalysisResult) -> usize {
    result
        .file_breakdown
        .iter()
        .map(|f| f.file.display().to_string().len())
        .max()
        .unwrap_or(20)
        .max(4) // at least "File"
}