tokencompact_core/
policy.rs1use crate::{Diagnostic, DiagnosticClass, EvidenceQuality, Severity};
2
3pub trait Redactor {
5 fn redact(&self, value: &str) -> String;
6}
7
8impl<F> Redactor for F
9where
10 F: Fn(&str) -> String,
11{
12 fn redact(&self, value: &str) -> String {
13 self(value)
14 }
15}
16
17#[derive(Clone, Copy, Debug, Default)]
19pub struct NoRedaction;
20
21impl Redactor for NoRedaction {
22 fn redact(&self, value: &str) -> String {
23 value.to_owned()
24 }
25}
26
27pub trait PathMapper {
29 fn map_path(&self, value: &str) -> String;
30}
31
32impl<F> PathMapper for F
33where
34 F: Fn(&str) -> String,
35{
36 fn map_path(&self, value: &str) -> String {
37 self(value)
38 }
39}
40
41#[derive(Clone, Copy, Debug, Default)]
43pub struct NoPathMapping;
44
45impl PathMapper for NoPathMapping {
46 fn map_path(&self, value: &str) -> String {
47 value.to_owned()
48 }
49}
50
51#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
53pub struct RankKey {
54 pub severity: Severity,
55 pub class: u8,
56 pub quality: EvidenceQuality,
57 pub ordinal: u64,
58}
59
60pub trait Ranker {
62 fn rank(&self, diagnostic: &Diagnostic) -> RankKey;
63}
64
65#[derive(Clone, Copy, Debug, Default)]
69pub struct GenericRanker;
70
71impl Ranker for GenericRanker {
72 fn rank(&self, diagnostic: &Diagnostic) -> RankKey {
73 let class = match diagnostic.class {
74 DiagnosticClass::Compiler => 0,
75 DiagnosticClass::Test => 1,
76 DiagnosticClass::Lint => 2,
77 DiagnosticClass::Infrastructure => 3,
78 DiagnosticClass::Tool => 4,
79 };
80 RankKey {
81 severity: diagnostic.severity,
82 class,
83 quality: diagnostic.quality,
84 ordinal: diagnostic
85 .provenance
86 .as_ref()
87 .and_then(|provenance| provenance.start_line)
88 .unwrap_or(u64::MAX),
89 }
90 }
91}
92
93#[derive(Clone, Copy)]
95pub struct OutputPolicy<'a> {
96 pub redactor: &'a dyn Redactor,
97 pub path_mapper: &'a dyn PathMapper,
98 pub ranker: &'a dyn Ranker,
99}
100
101impl<'a> OutputPolicy<'a> {
102 #[must_use]
103 pub const fn new(
104 redactor: &'a dyn Redactor,
105 path_mapper: &'a dyn PathMapper,
106 ranker: &'a dyn Ranker,
107 ) -> Self {
108 Self {
109 redactor,
110 path_mapper,
111 ranker,
112 }
113 }
114}