Skip to main content

logcompact_core/
policy.rs

1use crate::{Diagnostic, DiagnosticClass, EvidenceQuality, Severity};
2
3/// Infallible transformation applied to every untrusted returned string.
4pub 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/// Explicit identity redactor for already-sanitized or non-sensitive inputs.
18#[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
27/// Pure provider-supplied path normalization performed before redaction.
28pub 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/// Identity path policy used by generic CLI and CI consumers.
42#[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/// Stable typed rank key; lower values are retained first.
52#[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
60/// Provider-selectable ranking policy that never inspects raw log state.
61pub trait Ranker {
62    fn rank(&self, diagnostic: &Diagnostic) -> RankKey;
63}
64
65/// Generic ranking prefers compiler, test, lint, infrastructure, then tool
66/// diagnostics and uses parser-emitted evidence quality instead of message
67/// substring heuristics.
68#[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/// Output transformations and ranking shared by batch and streaming callers.
94#[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}