allow-report 0.1.7

Report and receipt rendering for cargo-allow source exception scans.
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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use crate::audit_remediation::audit_remediation_items;
use crate::evidence_repair::{
    BROKEN_EVIDENCE_LINK_COMMAND, WEAK_EVIDENCE_REFERENCE_COMMAND, evidence_repair_queues,
};
use crate::non_rust::{render_non_rust_human, render_non_rust_markdown};
use crate::text::markdown_inline_code;
use crate::{
    AUDIT_REVIEW_QUEUE_STATUSES, CLAIM_BOUNDARY_TEXT, ReportContext, ReviewSignals,
    STATUS_COUNT_ORDER, Summary, baseline_debt_count, broken_evidence_link_count,
    policy_missing_evidence_count, render_source_inventory_human, render_source_inventory_markdown,
    weak_evidence_reference_count,
};
use allow_core::{Finding, MatchOutcome, MatchStatus, json_escape};

const HUMAN_NON_MATCHED_OUTCOME_LIMIT: usize = 80;
const MARKDOWN_NON_MATCHED_OUTCOME_LIMIT: usize = 100;
const AUDIT_REVIEW_QUEUE_LIMIT: usize = 20;

pub fn render_human(
    command: &str,
    findings: &[Finding],
    outcomes: &[MatchOutcome],
    failed: bool,
) -> String {
    render_human_with_context(
        command,
        findings,
        outcomes,
        failed,
        ReportContext::default(),
    )
}

pub fn render_human_with_context(
    command: &str,
    findings: &[Finding],
    outcomes: &[MatchOutcome],
    failed: bool,
    context: ReportContext<'_>,
) -> String {
    let summary = Summary::from_outcomes(outcomes);
    let mut out = String::new();
    out.push_str(&format!("cargo-allow {command}\n\n"));
    out.push_str(&format!("Findings scanned: {}\n", findings.len()));
    out.push_str(&format!(
        "Inventory: source_tree/source_syntax via {}{}\n",
        context.inventory.source,
        inventory_files_suffix(context)
    ));
    if let Some(root) = context.inventory.root {
        out.push_str(&format!("Source tree root: {root}\n"));
    }
    for status in STATUS_COUNT_ORDER {
        let count = summary.count(status);
        if count > 0 {
            out.push_str(&format!("  {:24} {}\n", status.as_str(), count));
        }
    }
    if let Some(baseline_debt) = policy_baseline_debt_note(&summary, context) {
        out.push_str(&format!(
            "  {:24} {}\n",
            "policy_baseline_debt", baseline_debt
        ));
    }
    if let Some(policy_missing_evidence) = policy_missing_evidence_note(&summary, context) {
        out.push_str(&format!(
            "  {:24} {}\n",
            "policy_missing_evidence", policy_missing_evidence
        ));
    }
    let broken_evidence_links = broken_evidence_link_count(context);
    if broken_evidence_links > 0 {
        out.push_str(&format!(
            "  {:24} {}\n",
            "broken_evidence_links", broken_evidence_links
        ));
    }
    let weak_evidence_references = weak_evidence_reference_count(context);
    if weak_evidence_references > 0 {
        out.push_str(&format!(
            "  {:24} {}\n",
            "weak_evidence_references", weak_evidence_references
        ));
    }
    if outcomes.is_empty() {
        out.push_str("  no outcomes\n");
    }
    if command == "audit" {
        render_source_inventory_human(findings, outcomes, &mut out);
        render_audit_summary_human(&summary, outcomes, context, &mut out);
    }
    render_non_rust_human(findings, outcomes, &mut out);
    if command != "audit" {
        let signals = ReviewSignals::from_summary(&summary, context);
        append_evidence_repair_queues_human(&summary, signals, &mut out);
    }
    out.push('\n');
    let non_matched = outcomes
        .iter()
        .filter(|o| o.status != MatchStatus::Matched)
        .collect::<Vec<_>>();
    for outcome in non_matched.iter().take(HUMAN_NON_MATCHED_OUTCOME_LIMIT) {
        out.push_str(&format!(
            "{}: {}\n",
            outcome.status.as_str(),
            outcome.message
        ));
    }
    append_human_omitted_outcome_note(&mut out, non_matched.len());
    out.push('\n');
    out.push_str(CLAIM_BOUNDARY_TEXT);
    out.push('\n');
    out.push_str(if failed {
        "Result: failed\n"
    } else {
        "Result: passed/advisory\n"
    });
    out
}

fn render_audit_summary_human(
    summary: &Summary,
    outcomes: &[MatchOutcome],
    context: ReportContext<'_>,
    out: &mut String,
) {
    let signals = ReviewSignals::from_summary(summary, context);
    let queue = outcomes
        .iter()
        .filter(|outcome| AUDIT_REVIEW_QUEUE_STATUSES.contains(&outcome.status))
        .collect::<Vec<_>>();
    out.push_str("\nAudit summary:\n");
    out.push_str(&format!("  {:24} {}\n", "match_outcomes", summary.total));
    out.push_str(&format!(
        "  {:24} {}\n",
        "review_items", signals.review_items
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "new_unreceipted",
        summary.count(MatchStatus::New)
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "expired",
        summary.count(MatchStatus::Expired)
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "review_due",
        summary.count(MatchStatus::ReviewDue)
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "stale",
        summary.count(MatchStatus::Stale)
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "ambiguous",
        summary.count(MatchStatus::Ambiguous)
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "invalid_selector",
        summary.count(MatchStatus::InvalidSelector)
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "missing_required_field",
        summary.count(MatchStatus::MissingRequiredField)
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "evidence_gaps",
        summary.count(MatchStatus::EvidenceMissing)
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "policy_missing_evidence", signals.policy_missing_evidence
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "broken_evidence_links", signals.broken_evidence_links
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "weak_evidence_references", signals.weak_evidence_references
    ));
    out.push_str(&format!(
        "  {:24} {}\n",
        "baseline_debt", signals.baseline_debt
    ));
    out.push_str(&audit_recommended_next_step(
        summary,
        signals,
        queue.is_empty(),
    ));
    append_audit_remediation_roadmap_human(summary, signals, out);
    append_evidence_repair_queues_human(summary, signals, out);
    if !queue.is_empty() {
        out.push_str("\nAudit review queue:\n");
        for outcome in queue.iter().take(AUDIT_REVIEW_QUEUE_LIMIT) {
            out.push_str(&format!(
                "  {}: {}\n",
                outcome.status.as_str(),
                outcome.message
            ));
        }
        append_human_omitted_review_queue_note(out, queue.len());
    }
}

pub fn render_markdown(
    command: &str,
    findings: &[Finding],
    outcomes: &[MatchOutcome],
    failed: bool,
) -> String {
    render_markdown_with_context(
        command,
        findings,
        outcomes,
        failed,
        ReportContext::default(),
    )
}

pub fn render_markdown_with_context(
    command: &str,
    findings: &[Finding],
    outcomes: &[MatchOutcome],
    failed: bool,
    context: ReportContext<'_>,
) -> String {
    let summary = Summary::from_outcomes(outcomes);
    let mut out = String::new();
    out.push_str(&format!("# cargo-allow {command}\n\n"));
    out.push_str(&format!(
        "**Result:** {}\n\n",
        if failed { "failed" } else { "passed/advisory" }
    ));
    out.push_str(&format!("Findings scanned: `{}`\n\n", findings.len()));
    out.push_str(&format!(
        "Inventory: `source_tree` / `source_syntax` via `{}`{}\n\n",
        json_escape(context.inventory.source),
        inventory_files_markdown_suffix(context)
    ));
    if let Some(root) = context.inventory.root {
        out.push_str(&format!(
            "Source tree root: `{}`\n\n",
            markdown_inline_code(root)
        ));
    }
    out.push_str("| Status | Count |\n|---|---:|\n");
    for status in STATUS_COUNT_ORDER {
        let count = summary.count(status);
        out.push_str(&format!("| `{}` | {} |\n", status.as_str(), count));
    }
    if let Some(baseline_debt) = policy_baseline_debt_note(&summary, context) {
        out.push_str(&format!("| `policy_baseline_debt` | {} |\n", baseline_debt));
    }
    if let Some(policy_missing_evidence) = policy_missing_evidence_note(&summary, context) {
        out.push_str(&format!(
            "| `policy_missing_evidence` | {} |\n",
            policy_missing_evidence
        ));
    }
    let broken_evidence_links = broken_evidence_link_count(context);
    if broken_evidence_links > 0 {
        out.push_str(&format!(
            "| `broken_evidence_links` | {} |\n",
            broken_evidence_links
        ));
    }
    let weak_evidence_references = weak_evidence_reference_count(context);
    if weak_evidence_references > 0 {
        out.push_str(&format!(
            "| `weak_evidence_references` | {} |\n",
            weak_evidence_references
        ));
    }
    if command == "audit" {
        render_source_inventory_markdown(findings, outcomes, &mut out);
        render_audit_summary_markdown(&summary, outcomes, context, &mut out);
    }
    render_non_rust_markdown(findings, outcomes, &mut out);
    if command != "audit" {
        let signals = ReviewSignals::from_summary(&summary, context);
        append_evidence_repair_queues_markdown(&summary, signals, &mut out);
    }
    let non_matched = outcomes
        .iter()
        .filter(|o| o.status != MatchStatus::Matched)
        .collect::<Vec<_>>();
    if !non_matched.is_empty() {
        out.push_str("\n## Non-matched outcomes\n\n");
        for outcome in non_matched.iter().take(MARKDOWN_NON_MATCHED_OUTCOME_LIMIT) {
            out.push_str(&format!(
                "- `{}`: {}\n",
                outcome.status.as_str(),
                outcome.message
            ));
        }
        append_markdown_omitted_outcome_note(&mut out, non_matched.len());
    }
    out.push_str("\n> ");
    out.push_str(CLAIM_BOUNDARY_TEXT);
    out.push('\n');
    out
}

fn append_human_omitted_outcome_note(out: &mut String, outcome_count: usize) {
    if outcome_count > HUMAN_NON_MATCHED_OUTCOME_LIMIT {
        let omitted = outcome_count - HUMAN_NON_MATCHED_OUTCOME_LIMIT;
        let plural = if omitted == 1 { "" } else { "s" };
        out.push_str(&format!(
            "... {omitted} additional non-matched outcome{plural} omitted from this listing\n"
        ));
    }
}

fn append_markdown_omitted_outcome_note(out: &mut String, outcome_count: usize) {
    if outcome_count > MARKDOWN_NON_MATCHED_OUTCOME_LIMIT {
        let omitted = outcome_count - MARKDOWN_NON_MATCHED_OUTCOME_LIMIT;
        let plural = if omitted == 1 { "" } else { "s" };
        out.push_str(&format!(
            "\n{omitted} additional non-matched outcome{plural} omitted from this listing.\n"
        ));
    }
}

fn render_audit_summary_markdown(
    summary: &Summary,
    outcomes: &[MatchOutcome],
    context: ReportContext<'_>,
    out: &mut String,
) {
    let signals = ReviewSignals::from_summary(summary, context);
    let queue = outcomes
        .iter()
        .filter(|outcome| AUDIT_REVIEW_QUEUE_STATUSES.contains(&outcome.status))
        .collect::<Vec<_>>();
    out.push_str("\n## Audit Summary\n\n");
    out.push_str("| Signal | Count |\n|---|---:|\n");
    out.push_str(&format!("| Match outcomes | {} |\n", summary.total));
    out.push_str(&format!("| Review items | {} |\n", signals.review_items));
    out.push_str(&format!(
        "| New unreceipted | {} |\n",
        summary.count(MatchStatus::New)
    ));
    out.push_str(&format!(
        "| Expired | {} |\n",
        summary.count(MatchStatus::Expired)
    ));
    out.push_str(&format!(
        "| Review due | {} |\n",
        summary.count(MatchStatus::ReviewDue)
    ));
    out.push_str(&format!(
        "| Stale | {} |\n",
        summary.count(MatchStatus::Stale)
    ));
    out.push_str(&format!(
        "| Ambiguous | {} |\n",
        summary.count(MatchStatus::Ambiguous)
    ));
    out.push_str(&format!(
        "| Invalid selectors | {} |\n",
        summary.count(MatchStatus::InvalidSelector)
    ));
    out.push_str(&format!(
        "| Missing required fields | {} |\n",
        summary.count(MatchStatus::MissingRequiredField)
    ));
    out.push_str(&format!(
        "| Evidence gaps | {} |\n",
        summary.count(MatchStatus::EvidenceMissing)
    ));
    out.push_str(&format!(
        "| Policy missing evidence | {} |\n",
        signals.policy_missing_evidence
    ));
    out.push_str(&format!(
        "| Broken evidence links | {} |\n",
        signals.broken_evidence_links
    ));
    out.push_str(&format!(
        "| Weak evidence/link references | {} |\n",
        signals.weak_evidence_references
    ));
    out.push_str(&format!("| Baseline debt | {} |\n", signals.baseline_debt));
    out.push_str(&audit_recommended_next_step(
        summary,
        signals,
        queue.is_empty(),
    ));
    append_audit_remediation_roadmap_markdown(summary, signals, out);
    append_evidence_repair_queues_markdown(summary, signals, out);

    if !queue.is_empty() {
        out.push_str("\n## Audit Review Queue\n\n");
        for outcome in queue.iter().take(AUDIT_REVIEW_QUEUE_LIMIT) {
            out.push_str(&format!(
                "- `{}`: {}\n",
                outcome.status.as_str(),
                outcome.message
            ));
        }
        append_markdown_omitted_review_queue_note(out, queue.len());
    }
}

fn append_evidence_repair_queues_human(
    summary: &Summary,
    signals: ReviewSignals,
    out: &mut String,
) {
    let commands = evidence_repair_commands(summary, signals);
    if commands.is_empty() {
        return;
    }
    out.push_str("\nEvidence repair queues:\n");
    for command in commands {
        out.push_str(&format!("  {command}\n"));
    }
}

fn append_evidence_repair_queues_markdown(
    summary: &Summary,
    signals: ReviewSignals,
    out: &mut String,
) {
    let commands = evidence_repair_commands(summary, signals);
    if commands.is_empty() {
        return;
    }
    out.push_str("\n### Evidence Repair Queues\n\n");
    for command in commands {
        out.push_str(&format!("- `{command}`\n"));
    }
}

fn evidence_repair_commands(summary: &Summary, signals: ReviewSignals) -> Vec<&'static str> {
    evidence_repair_queues(summary, signals)
        .into_iter()
        .map(|queue| queue.command)
        .collect()
}

fn append_audit_remediation_roadmap_human(
    summary: &Summary,
    signals: ReviewSignals,
    out: &mut String,
) {
    let items = audit_remediation_items(summary, signals);
    if items.is_empty() {
        return;
    }
    out.push_str("\nAudit remediation roadmap:\n");
    for item in items {
        out.push_str(&format!("  {}: {}\n", item.label, item.command));
    }
}

fn append_audit_remediation_roadmap_markdown(
    summary: &Summary,
    signals: ReviewSignals,
    out: &mut String,
) {
    let items = audit_remediation_items(summary, signals);
    if items.is_empty() {
        return;
    }
    out.push_str("\n## Audit Remediation Roadmap\n\n");
    out.push_str("| Signal | Command |\n|---|---|\n");
    for item in items {
        out.push_str(&format!("| {} | `{}` |\n", item.label, item.command));
    }
}

fn append_human_omitted_review_queue_note(out: &mut String, queue_count: usize) {
    if queue_count > AUDIT_REVIEW_QUEUE_LIMIT {
        let omitted = queue_count - AUDIT_REVIEW_QUEUE_LIMIT;
        let plural = if omitted == 1 { "" } else { "s" };
        out.push_str(&format!(
            "  ... {omitted} additional audit review item{plural} omitted from this queue\n"
        ));
    }
}

fn append_markdown_omitted_review_queue_note(out: &mut String, queue_count: usize) {
    if queue_count > AUDIT_REVIEW_QUEUE_LIMIT {
        let omitted = queue_count - AUDIT_REVIEW_QUEUE_LIMIT;
        let plural = if omitted == 1 { "" } else { "s" };
        out.push_str(&format!(
            "\n{omitted} additional audit review item{plural} omitted from this queue.\n"
        ));
    }
}

fn audit_recommended_next_step(
    summary: &Summary,
    signals: ReviewSignals,
    queue_empty: bool,
) -> String {
    if signals.review_items == 0 {
        "\nRecommended next step: keep `cargo-allow check --mode no-new` in CI.\n".to_string()
    } else if queue_empty && signals.broken_evidence_links > 0 {
        format!(
            "\nRecommended next step: run `{BROKEN_EVIDENCE_LINK_COMMAND}` to repair broken local evidence/link references.\n"
        )
    } else if queue_empty
        && signals.policy_missing_evidence > summary.count(MatchStatus::EvidenceMissing)
    {
        "\nRecommended next step: run `cargo-allow worklist --format json` to route retained entries with no evidence references; add `--missing-evidence` to focus that queue.\n".to_string()
    } else if queue_empty && signals.weak_evidence_references > 0 {
        format!(
            "\nRecommended next step: run `{WEAK_EVIDENCE_REFERENCE_COMMAND}` to replace unstructured or unknown-prefix evidence/link references.\n"
        )
    } else if queue_empty && signals.baseline_debt > 0 {
        "\nRecommended next step: run `cargo-allow worklist --format json` to review generated baseline debt.\n".to_string()
    } else {
        "\nRecommended next step: review the queue below before tightening policy.\n".to_string()
    }
}

fn inventory_files_suffix(context: ReportContext<'_>) -> String {
    context
        .inventory
        .files_scanned
        .map(|files| format!("; files scanned: {files}"))
        .unwrap_or_default()
}

fn inventory_files_markdown_suffix(context: ReportContext<'_>) -> String {
    context
        .inventory
        .files_scanned
        .map(|files| format!("; files scanned: `{files}`"))
        .unwrap_or_default()
}

fn policy_baseline_debt_note(summary: &Summary, context: ReportContext<'_>) -> Option<usize> {
    let baseline_debt = baseline_debt_count(summary, context);
    (baseline_debt > summary.count(MatchStatus::BaselineDebt)).then_some(baseline_debt)
}

fn policy_missing_evidence_note(summary: &Summary, context: ReportContext<'_>) -> Option<usize> {
    let policy_missing_evidence = policy_missing_evidence_count(summary, context);
    (policy_missing_evidence > summary.count(MatchStatus::EvidenceMissing))
        .then_some(policy_missing_evidence)
}