cargo-bless 0.2.1

Modernize your Rust dependencies with blessed.rs + live intel
Documentation
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
//! Output layer — renders the modernization report to the terminal
//! with colored output, emojis, and actionable links.

use std::collections::HashMap;
use std::path::Path;

use colored::*;
use serde::Serialize;

use crate::code_audit::{kind_label, CodeAuditReport};
use crate::intel::CrateIntel;
use crate::suggestions::{AutofixSafety, Confidence, Impact, MigrationRisk, SuggestionKind};
use crate::suggestions::{EvidenceSource, Suggestion};

fn print_suggestion_detail(suggestion: &Suggestion, intel: &HashMap<String, CrateIntel>) {
    let icon = match suggestion.kind {
        SuggestionKind::ModernAlternative => "",
        SuggestionKind::FeatureOptimization => "",
        SuggestionKind::StdReplacement => "",
        SuggestionKind::ComboWin => "",
        SuggestionKind::Unmaintained => "⚠️",
    };

    let impact_tag = match suggestion.impact {
        Impact::High => "[HIGH]".red().bold(),
        Impact::Medium => "[MED]".yellow().bold(),
        Impact::Low => "[LOW]".dimmed(),
    };
    let confidence_tag = match suggestion.confidence {
        Confidence::High => "[HIGH confidence]".green().bold(),
        Confidence::Medium => "[MED confidence]".yellow(),
        Confidence::Low => "[LOW confidence]".red(),
    };
    let risk_tag = match suggestion.migration_risk {
        MigrationRisk::High => "[HIGH risk]".red().bold(),
        MigrationRisk::Medium => "[MED risk]".yellow(),
        MigrationRisk::Low => "[LOW risk]".green(),
    };
    let autofix_tag = match suggestion.autofix_safety {
        AutofixSafety::CargoTomlOnly => "[autofix: Cargo.toml-only]".green(),
        AutofixSafety::ManualOnly => "[autofix: manual]".dimmed(),
    };
    let verb = match suggestion.confidence {
        Confidence::High => "",
        Confidence::Medium | Confidence::Low => "→ consider",
    };

    println!(
        " {} {} {} {} {}",
        icon,
        impact_tag,
        suggestion.current.yellow(),
        verb,
        suggestion.recommended.green(),
    );
    println!(
        "   {} {} {} {}",
        confidence_tag,
        risk_tag,
        autofix_tag,
        format!("evidence: {}", evidence_label(&suggestion.evidence_source)).dimmed()
    );
    println!("   {}", suggestion.reason.dimmed());

    let crate_names: Vec<&str> = suggestion.current.split('+').collect();
    for crate_name in crate_names {
        if let Some(info) = intel.get(crate_name) {
            let mut enrichment = format!("   latest: v{}", info.latest_version);
            if let Some(recent) = info.recent_downloads {
                enrichment.push_str(&format!(", {} recent downloads", format_downloads(recent)));
            }
            println!("   {}", enrichment.dimmed());
        }
    }
}

/// One workspace member’s dependency suggestions shown in plain text reports.
#[derive(Clone, Copy, Debug)]
pub struct PackageSuggestionView<'a> {
    pub name: &'a str,
    pub version: &'a str,
    pub manifest_path: &'a Path,
    pub suggestions: &'a [Suggestion],
}

/// Render modernization output for one or many packages (`--workspace` / `--package` use multi headers).
fn use_multi_headers(packages: &[PackageSuggestionView<'_>]) -> bool {
    if packages.len() > 1 {
        return true;
    }
    packages
        .first()
        .is_some_and(|p| p.suggestions.iter().any(|s| s.package.is_some()))
}

/// Single-root modernization report (`manifest_path` is only useful in grouped layouts).
pub fn render_report(
    project_name: &str,
    version: &str,
    suggestions: &[Suggestion],
    intel: &HashMap<String, CrateIntel>,
) {
    render_packages_modernization(
        &[PackageSuggestionView {
            name: project_name,
            version,
            manifest_path: Path::new("Cargo.toml"),
            suggestions,
        }],
        intel,
    );
}

pub fn render_packages_modernization(
    packages: &[PackageSuggestionView<'_>],
    intel: &HashMap<String, CrateIntel>,
) {
    let all_empty = packages.iter().all(|p| p.suggestions.is_empty());

    if all_empty {
        println!(
            "{}",
            "✅ Your dependencies are already blessed! Nothing to modernize.".green()
        );
        return;
    }

    let multi = use_multi_headers(packages);

    if !multi {
        let p = &packages[0];
        println!(
            "{}",
            format!("🚀 Modernization report for {} v{}", p.name, p.version).bold()
        );
        println!();
        for suggestion in p.suggestions {
            print_suggestion_detail(suggestion, intel);
        }
    } else {
        for p in packages {
            if p.suggestions.is_empty() {
                continue;
            }
            println!(
                "{}",
                format!(
                    "📦 {} v{} ({})",
                    p.name,
                    p.version,
                    p.manifest_path.display()
                )
                .bold()
            );
            println!();
            for suggestion in p.suggestions {
                print_suggestion_detail(suggestion, intel);
            }
            println!();
        }
    }

    let high_count = packages
        .iter()
        .flat_map(|p| p.suggestions)
        .filter(|s| matches!(s.impact, Impact::High))
        .count();

    println!();
    println!(
        "{}",
        format!(
            "{} high-impact upgrade{} available. `--fix` only edits Cargo.toml (never Rust source). Run `cargo bless --fix --dry-run` to preview.",
            high_count,
            if high_count == 1 { "" } else { "s" }
        )
        .bold()
    );
}

fn suggestion_kind_slug(kind: &SuggestionKind) -> &'static str {
    match kind {
        SuggestionKind::ModernAlternative => "modern_alternative",
        SuggestionKind::FeatureOptimization => "feature_opt",
        SuggestionKind::StdReplacement => "std_replace",
        SuggestionKind::ComboWin => "combo_win",
        SuggestionKind::Unmaintained => "unmaintained",
    }
}

/// Paste-friendly condensed output (`--summary`).
pub fn render_summary(scan_stats: &[(&str, usize, usize)], suggestions: &[Suggestion]) {
    let pkg_ct = scan_stats.len();
    println!(
        "{}",
        format!(
            "📊 Summary — scanned {} workspace member{}",
            pkg_ct,
            if pkg_ct == 1 { "" } else { "s" }
        )
        .bold()
    );
    for (name, direct_ct, total_ct) in scan_stats {
        println!(
            "{}{} direct deps, {} total in resolve",
            name.bold(),
            direct_ct,
            total_ct
        );
    }

    let mut hi = 0usize;
    let mut med = 0usize;
    let mut low = 0usize;
    for s in suggestions {
        match s.impact {
            Impact::High => hi += 1,
            Impact::Medium => med += 1,
            Impact::Low => low += 1,
        }
    }

    println!();
    println!("Suggestions after policy: {}", suggestions.len());
    println!("By impact — high: {hi}, medium: {med}, low: {low}");

    let mut kind_counts = HashMap::<&'static str, usize>::new();
    for s in suggestions {
        *kind_counts
            .entry(suggestion_kind_slug(&s.kind))
            .or_default() += 1;
    }
    let mut kind_pairs: Vec<_> = kind_counts.into_iter().collect();
    kind_pairs.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(b.0)));
    if !kind_pairs.is_empty() {
        println!(
            "By kind — {}",
            kind_pairs
                .iter()
                .map(|(k, c)| format!("{k}: {c}"))
                .collect::<Vec<_>>()
                .join(", ")
        );
    }

    println!();
    println!("{}", "Top patterns:".bold());
    let mut patterns: Vec<String> = suggestions
        .iter()
        .map(|s| format!("{}{}", s.current, s.recommended))
        .collect();
    patterns.sort();
    patterns.dedup();
    const MAX: usize = 14usize;
    for line in patterns.iter().take(MAX) {
        println!("{}", line);
    }
    if patterns.len() > MAX {
        println!("   … and {} more", patterns.len() - MAX);
    }

    println!();
    println!(
        "{}",
        "`--fix` changes Cargo.toml entries only — never Rust source. Check `autofix_safety` on each suggestion."
            .dimmed()
    );
}

fn evidence_label(source: &EvidenceSource) -> &'static str {
    match source {
        EvidenceSource::BlessedRs => "blessed.rs",
        EvidenceSource::RustSec => "RustSec",
        EvidenceSource::StdDocs => "std docs",
        EvidenceSource::CrateDocs => "crate docs",
        EvidenceSource::CratesIo => "crates.io",
        EvidenceSource::Heuristic => "heuristic",
    }
}

pub fn render_code_audit_report(report: &CodeAuditReport, verbose: bool) {
    println!();
    println!("{}", "🧨 Bullshit detector code audit".bold());
    println!(
        "{}",
        format!(
            "Scanned {} Rust file{}.",
            report.files_scanned,
            if report.files_scanned == 1 { "" } else { "s" }
        )
        .dimmed()
    );

    if report.is_clean() {
        println!("{}", "✅ No bullshit detected in Rust source.".green());
        return;
    }

    println!(
        "{}",
        format!(
            "🚨 Bullshit detected: {} finding{} · heat {:.1}",
            report.alerts.len(),
            if report.alerts.len() == 1 { "" } else { "s" },
            report.alerts.iter().map(|a| a.severity).sum::<f32>() * 10.0
        )
        .red()
        .bold()
    );

    let mut counts = HashMap::<&'static str, usize>::new();
    for alert in &report.alerts {
        *counts.entry(kind_label(alert.kind)).or_default() += 1;
    }
    let mut counts: Vec<_> = counts.into_iter().collect();
    counts.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(b.0)));
    let summary = counts
        .iter()
        .map(|(kind, count)| format!("{kind}: {count}"))
        .collect::<Vec<_>>()
        .join(", ");
    println!("{}", summary.dimmed());
    println!();

    let shown = if verbose {
        report.alerts.len()
    } else {
        report.alerts.len().min(5)
    };

    for alert in report.alerts.iter().take(shown) {
        println!(
            " {} {} {}:{}:{}",
            "".red(),
            kind_label(alert.kind).yellow().bold(),
            alert.file.display().to_string().dimmed(),
            alert.line,
            alert.column
        );
        println!("   {}", alert.why_bs);
        println!("   {}", format!("Fix: {}", alert.suggestion).green());
        if !alert.context_snippet.is_empty() {
            println!("   {}", alert.context_snippet.dimmed());
        }
    }

    if !verbose && report.alerts.len() > shown {
        println!();
        println!(
            "{}",
            format!(
                "Showing top {shown}. Run with --verbose for all {} findings, or --json for machine output.",
                report.alerts.len()
            )
            .dimmed()
        );
    }
}

/// Format download counts in a human-readable way (e.g., "1.2M", "456K").
fn format_downloads(count: u64) -> String {
    if count >= 1_000_000 {
        format!("{:.1}M", count as f64 / 1_000_000.0)
    } else if count >= 1_000 {
        format!("{:.1}K", count as f64 / 1_000.0)
    } else {
        count.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_downloads() {
        assert_eq!(format_downloads(0), "0");
        assert_eq!(format_downloads(500), "500");
        assert_eq!(format_downloads(1_500), "1.5K");
        assert_eq!(format_downloads(1_200_000), "1.2M");
        assert_eq!(format_downloads(100_000_000), "100.0M");
    }
}

#[derive(Serialize)]
pub struct JsonPackageOutput<'a> {
    pub name: &'a str,
    pub version: &'a str,
    pub manifest_path: String,
    pub dependency_suggestions: &'a [Suggestion],
}

/// Machine-readable report: `cargo_bless_version`, `workspace_scan`, `packages`, optional `code_audit`.
#[derive(Serialize)]
pub struct JsonReportUnified<'a> {
    pub cargo_bless_version: &'a str,
    pub workspace_scan: bool,
    pub packages: Vec<JsonPackageOutput<'a>>,
    pub code_audit: Option<&'a CodeAuditReport>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hardcoded_values: Option<&'a [crate::bs_detector::BSHit]>,
}

pub fn render_unified_json(report: JsonReportUnified<'_>) {
    match serde_json::to_string_pretty(&report) {
        Ok(json) => println!("{}", json),
        Err(e) => eprintln!("cargo-bless: failed to serialize JSON output: {}", e),
    }
}

/// Legacy shape for compatibility (flat `dependency_suggestions` at top level).
#[derive(Serialize)]
pub struct JsonReport<'a> {
    pub dependency_suggestions: &'a [Suggestion],
    pub code_audit: Option<&'a CodeAuditReport>,
}

/// Legacy narrow JSON (**`dependency_suggestions`** at top level) for crates embedding v0.1 output.
pub fn render_json_report(suggestions: &[Suggestion], code_audit: Option<&CodeAuditReport>) {
    let report = JsonReport {
        dependency_suggestions: suggestions,
        code_audit,
    };
    match serde_json::to_string_pretty(&report) {
        Ok(json) => println!("{}", json),
        Err(e) => eprintln!("cargo-bless: failed to serialize JSON output: {}", e),
    }
}

/// Render suggestions as a JSON array to stdout.
/// Kept for library callers that rely on the old narrow JSON shape.
pub fn render_json(suggestions: &[Suggestion]) {
    match serde_json::to_string_pretty(suggestions) {
        Ok(json) => println!("{}", json),
        Err(e) => eprintln!("cargo-bless: failed to serialize JSON output: {}", e),
    }
}