Skip to main content

allow_report/artifacts/
migrate.rs

1use allow_core::{AllowConfig, FindingKind};
2
3use crate::InventoryContext;
4
5#[derive(Debug, Clone, Copy)]
6pub struct MigrateReport<'a> {
7    pub inventory: InventoryContext<'a>,
8    pub input_kind: &'a str,
9    pub input_path: &'a str,
10    pub output_path: &'a str,
11    pub force: bool,
12    pub allow_entries: usize,
13    pub baseline_debt: usize,
14    pub unsafe_entries: usize,
15    pub lint_exception_entries: usize,
16    pub entries_with_evidence: usize,
17    pub broken_evidence_links: Option<usize>,
18    pub unsafe_broken_evidence_links: Option<usize>,
19    pub weak_evidence_references: Option<usize>,
20    pub unsafe_weak_evidence_references: Option<usize>,
21    pub notes: &'a str,
22}
23
24impl<'a> MigrateReport<'a> {
25    pub fn from_config(
26        inventory: InventoryContext<'a>,
27        cfg: &AllowConfig,
28        input_kind: &'a str,
29        input_path: &'a str,
30        output_path: &'a str,
31        force: bool,
32        notes: &'a str,
33    ) -> Self {
34        let counts = MigrateSummaryCounts::from_config(cfg);
35        Self {
36            inventory,
37            input_kind,
38            input_path,
39            output_path,
40            force,
41            allow_entries: counts.allow_entries,
42            baseline_debt: counts.baseline_debt,
43            unsafe_entries: counts.unsafe_entries,
44            lint_exception_entries: counts.lint_exception_entries,
45            entries_with_evidence: counts.entries_with_evidence,
46            broken_evidence_links: None,
47            unsafe_broken_evidence_links: None,
48            weak_evidence_references: None,
49            unsafe_weak_evidence_references: None,
50            notes,
51        }
52    }
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56struct MigrateSummaryCounts {
57    allow_entries: usize,
58    baseline_debt: usize,
59    unsafe_entries: usize,
60    lint_exception_entries: usize,
61    entries_with_evidence: usize,
62}
63
64impl MigrateSummaryCounts {
65    fn from_config(cfg: &AllowConfig) -> Self {
66        Self {
67            allow_entries: cfg.allow.len(),
68            baseline_debt: cfg
69                .allow
70                .iter()
71                .filter(|entry| entry.classification == "baseline_debt")
72                .count(),
73            unsafe_entries: cfg
74                .allow
75                .iter()
76                .filter(|entry| entry.kind == FindingKind::Unsafe)
77                .count(),
78            lint_exception_entries: cfg
79                .allow
80                .iter()
81                .filter(|entry| entry.kind == FindingKind::LintException)
82                .count(),
83            entries_with_evidence: cfg
84                .allow
85                .iter()
86                .filter(|entry| !entry.evidence.is_empty())
87                .count(),
88        }
89    }
90}