Skip to main content

logcompact_builtins/
lib.rs

1//! Deterministic, source-agnostic reduction of compiler, test, and tool text.
2//!
3//! The crate performs no I/O and has no async, provider protocol, storage, or
4//! runner dependency. Callers provide already acquired text and an explicit
5//! redaction policy; only normalized and redacted diagnostics are returned.
6
7mod diagnostics;
8mod finalize;
9mod model;
10mod problem_matcher;
11mod redaction;
12mod streaming;
13mod test_failures;
14mod test_log;
15mod text;
16
17pub use diagnostics::{
18    BUILTIN_MATCHER_OWNERS, BuiltinMatcherOverrides, JavaScriptTestDiagnosticParser,
19    JavaTestDiagnosticParser, PythonDiagnosticParser, parse_go_diagnostic,
20};
21pub use logcompact_core::{
22    Emitter, GenericRanker, OutputPolicy, Parser, ParserPlan, ParserPlanError, RankKey, Ranker,
23    ReductionSession, ScopeBoundary,
24};
25pub use model::{
26    Budget, Diagnostic, DiagnosticClass, EndReason, EvidenceQuality, FallbackPolicy, Limits,
27    Location, LogLine, Provenance, Reduction, ReductionOptions, ReductionStats, Scope, ScopeKind,
28    SessionOptions, Severity, Stream, TestFailure, TextInput,
29};
30pub use problem_matcher::{
31    ProblemMatcherError, ProblemMatcherLimits, ProblemMatcherParser, ProblemMatcherRegistry,
32};
33pub use redaction::{NoPathMapping, NoRedaction, PathMapper, Redactor};
34pub use streaming::{BuiltinDiagnosticParser, BuiltinParserOptions, builtin_parser_plan};
35pub use test_failures::{TestFailureAccumulator, TestFailureEvidence};
36pub use test_log::{TestLogReducer, TestLogReduction};
37pub use text::{deduplicate_lines, normalize_terminal_text};
38
39/// Builds the default parser plan while replacing built-ins whose stable owner
40/// is also registered by a custom problem matcher.
41#[must_use]
42pub fn builtin_parser_plan_with_problem_matchers(
43    mut options: BuiltinParserOptions,
44    registry: ProblemMatcherRegistry,
45) -> ParserPlan {
46    options.overrides = options.overrides.union(registry.builtin_overrides());
47    let mut plan = builtin_parser_plan(options);
48    plan.push(registry.into_parser())
49        .expect("the problem matcher parser identifier is unique in the built-in plan");
50    plan
51}
52
53/// Reduces one or more text inputs with deterministic parser and input order.
54#[must_use]
55pub fn reduce(
56    inputs: &[TextInput<'_>],
57    options: &ReductionOptions,
58    redactor: &dyn Redactor,
59) -> Reduction {
60    reduce_with_policy(
61        inputs,
62        options,
63        OutputPolicy::new(redactor, &NoPathMapping, &GenericRanker),
64    )
65}
66
67/// Reduces batch inputs with caller-owned path mapping, redaction, and ranking.
68#[must_use]
69pub fn reduce_with_policy(
70    inputs: &[TextInput<'_>],
71    options: &ReductionOptions,
72    policy: OutputPolicy<'_>,
73) -> Reduction {
74    let mut diagnostics = Vec::new();
75    for input in inputs {
76        let start = diagnostics.len();
77        diagnostics::add_text_diagnostics(
78            input.text,
79            &mut diagnostics,
80            options.fallback,
81            BuiltinMatcherOverrides::default(),
82        );
83        for diagnostic in &mut diagnostics[start..] {
84            diagnostic.provenance = input.provenance.cloned();
85            if diagnostic.location.is_some() && diagnostic.quality == EvidenceQuality::Structured {
86                diagnostic.quality = EvidenceQuality::Located;
87            }
88        }
89    }
90    finalize::finalize_with_policy(diagnostics, options.budget, policy)
91}
92
93#[cfg(feature = "test-support")]
94#[doc(hidden)]
95pub fn __parse_cpp_diagnostic(input: &str) -> Option<Diagnostic> {
96    diagnostics::parse_cpp_diagnostic(input)
97}
98
99#[cfg(feature = "test-support")]
100#[doc(hidden)]
101pub fn __parse_cpp_linker_diagnostic(input: &str) -> Option<Diagnostic> {
102    diagnostics::parse_cpp_linker_diagnostic(input)
103}
104
105#[cfg(feature = "test-support")]
106#[doc(hidden)]
107pub fn __cpp_path_end(input: &str, delimiter: char) -> Option<usize> {
108    diagnostics::cpp_path_end(input, delimiter)
109}
110
111#[cfg(feature = "test-support")]
112#[doc(hidden)]
113pub fn __parse_swc_diagnostics(input: &str) -> Vec<Diagnostic> {
114    diagnostics::parse_swc_diagnostics(input)
115}
116
117#[cfg(feature = "test-support")]
118#[doc(hidden)]
119pub fn __parse_protobuf_diagnostic(input: &str) -> Option<Diagnostic> {
120    diagnostics::parse_protobuf_diagnostic(input)
121}
122
123#[cfg(feature = "test-support")]
124#[doc(hidden)]
125pub fn __parse_typescript_diagnostic(input: &str) -> Option<Diagnostic> {
126    diagnostics::parse_typescript_diagnostic(input)
127}
128
129#[cfg(test)]
130mod tests {
131    use super::*;
132
133    #[test]
134    fn reduces_compiler_and_test_logs_without_provider_objects() {
135        let compiler = Provenance::new("compile").with_label("rust");
136        let test = Provenance::new("test").with_label("node");
137        let inputs = [
138            TextInput::new(
139                b"error[E0308]: mismatched types\n --> src/lib.rs:7:5\n  |\n7 | value\n  | ^ expected u32, found &str",
140            )
141            .with_provenance(&compiler),
142            TextInput::new(b"TypeError: total is not a function\n    at src/invoice.test.js:8:3")
143                .with_provenance(&test),
144        ];
145        let reduction = reduce(
146            &inputs,
147            &ReductionOptions {
148                budget: Budget::unbounded(),
149                ..ReductionOptions::default()
150            },
151            &NoRedaction,
152        );
153
154        assert_eq!(reduction.diagnostics.len(), 2);
155        assert_eq!(reduction.diagnostics[0].class, DiagnosticClass::Compiler);
156        assert_eq!(
157            reduction.diagnostics[0].location.as_ref().unwrap().path,
158            "src/lib.rs"
159        );
160        assert_eq!(reduction.diagnostics[1].class, DiagnosticClass::Test);
161        assert_eq!(
162            reduction.diagnostics[1].provenance.as_ref().unwrap().source,
163            "test"
164        );
165    }
166
167    #[test]
168    fn provenance_keeps_otherwise_identical_inputs_distinct() {
169        let first = Provenance::new("stderr");
170        let second = Provenance::new("stdout");
171        let inputs = [
172            TextInput::new(b"src/main.go:4:2: undefined: value").with_provenance(&first),
173            TextInput::new(b"src/main.go:4:2: undefined: value").with_provenance(&second),
174        ];
175        let reduction = reduce(
176            &inputs,
177            &ReductionOptions {
178                budget: Budget::unbounded(),
179                ..ReductionOptions::default()
180            },
181            &NoRedaction,
182        );
183        assert_eq!(reduction.diagnostics.len(), 2);
184    }
185
186    #[test]
187    fn redacts_messages_locations_and_provenance_before_return() {
188        let provenance = Provenance::new("job-token=source").with_label("token=label");
189        let inputs = [
190            TextInput::new(b"token=path/main.go:4:2: token=message").with_provenance(&provenance)
191        ];
192        let redact = |value: &str| value.replace("token=", "[REDACTED]-");
193        let reduction = reduce(
194            &inputs,
195            &ReductionOptions {
196                budget: Budget::unbounded(),
197                ..ReductionOptions::default()
198            },
199            &redact,
200        );
201        let encoded = serde_json::to_string(&reduction).unwrap();
202        assert!(!encoded.contains("token="));
203        assert!(encoded.contains("[REDACTED]-path/main.go"));
204        assert!(encoded.contains("[REDACTED]-message"));
205        assert!(encoded.contains("job-[REDACTED]-source"));
206    }
207
208    #[test]
209    fn fallback_can_be_disabled_without_disabling_structured_parsers() {
210        let inputs = [TextInput::new(
211            b"generic fatal: wrapper\nsrc/main.go:4:2: undefined: value",
212        )];
213        let reduction = reduce(
214            &inputs,
215            &ReductionOptions {
216                budget: Budget::unbounded(),
217                fallback: FallbackPolicy::Disabled,
218            },
219            &NoRedaction,
220        );
221        assert_eq!(reduction.diagnostics.len(), 1);
222        assert_eq!(reduction.diagnostics[0].message, "undefined: value");
223    }
224}