1use std::collections::BTreeMap;
2
3use logcompact_core::{
4 Emitter, EndReason, EvidenceQuality, FallbackPolicy, LogLine, Parser, ParserPlan, Provenance,
5 Scope, ScopeBoundary, Stream,
6};
7
8use crate::diagnostics::{self, BuiltinMatcherOverrides};
9use crate::test_log::TestLogReducer;
10
11#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13pub struct BuiltinParserOptions {
14 pub fallback: FallbackPolicy,
15 pub max_buffered_scope_bytes: usize,
16 pub overrides: BuiltinMatcherOverrides,
17}
18
19impl Default for BuiltinParserOptions {
20 fn default() -> Self {
21 Self {
22 fallback: FallbackPolicy::Generic,
23 max_buffered_scope_bytes: 1024 * 1024,
24 overrides: BuiltinMatcherOverrides::default(),
25 }
26 }
27}
28
29#[must_use]
31pub fn builtin_parser_plan(options: BuiltinParserOptions) -> ParserPlan {
32 let mut plan = ParserPlan::new();
33 plan.push(BuiltinDiagnosticParser::new(options))
34 .expect("the built-in diagnostic parser identifier is unique");
35 plan.push(TestFailureParser::default())
36 .expect("the built-in test parser identifier is unique");
37 plan
38}
39
40#[derive(Clone, Debug)]
41struct BufferedStream {
42 text: String,
43 first_line: u64,
44 last_line: u64,
45 truncated: bool,
46}
47
48pub struct BuiltinDiagnosticParser {
52 options: BuiltinParserOptions,
53 scopes: BTreeMap<String, BTreeMap<Stream, BufferedStream>>,
54}
55
56impl BuiltinDiagnosticParser {
57 #[must_use]
58 pub fn new(options: BuiltinParserOptions) -> Self {
59 Self {
60 options,
61 scopes: BTreeMap::new(),
62 }
63 }
64
65 fn flush_scope(&mut self, scope: &Scope, emitter: &mut Emitter<'_>) {
66 let Some(streams) = self.scopes.remove(&scope.id) else {
67 return;
68 };
69 for (stream, buffer) in streams {
70 let mut parsed = Vec::new();
71 diagnostics::add_normalized_text_diagnostics(
72 &buffer.text,
73 &mut parsed,
74 self.options.fallback,
75 self.options.overrides,
76 );
77 for mut diagnostic in parsed {
78 if diagnostic.location.is_some() {
79 diagnostic.quality = EvidenceQuality::Located;
80 }
81 diagnostic.provenance = Some(
82 Provenance::new(stream_name(stream))
83 .with_scope(scope)
84 .with_span(stream, buffer.first_line, buffer.last_line)
85 .with_parser(self.id()),
86 );
87 emitter.diagnostic(diagnostic);
88 }
89 }
90 }
91}
92
93impl Parser for BuiltinDiagnosticParser {
94 fn id(&self) -> &'static str {
95 "builtin.diagnostics.v1"
96 }
97
98 fn observe(&mut self, line: &LogLine<'_>, _emitter: &mut Emitter<'_>) {
99 if !self.scopes.contains_key(&line.scope.id) {
100 self.scopes.insert(line.scope.id.clone(), BTreeMap::new());
101 }
102 let streams = self
103 .scopes
104 .get_mut(&line.scope.id)
105 .expect("scope buffer was inserted above");
106 let buffer = streams
107 .entry(line.stream)
108 .or_insert_with(|| BufferedStream {
109 text: String::new(),
110 first_line: line.stream_line,
111 last_line: line.stream_line,
112 truncated: false,
113 });
114 buffer.last_line = line.stream_line;
115 let required = line.text.len().saturating_add(1);
116 let available = self
117 .options
118 .max_buffered_scope_bytes
119 .saturating_sub(buffer.text.len());
120 if required <= available {
121 if !buffer.text.is_empty() {
122 buffer.text.push('\n');
123 }
124 buffer.text.push_str(line.text);
125 } else {
126 buffer.truncated = true;
127 let retained = available.min(line.text.len());
128 let mut boundary = retained;
129 while boundary > 0 && !line.text.is_char_boundary(boundary) {
130 boundary -= 1;
131 }
132 if boundary > 0 {
133 if !buffer.text.is_empty() {
134 buffer.text.push('\n');
135 }
136 buffer.text.push_str(&line.text[..boundary]);
137 }
138 }
139 buffer.truncated |= line.truncated;
140 }
141
142 fn boundary(&mut self, boundary: ScopeBoundary<'_>, emitter: &mut Emitter<'_>) {
143 if let ScopeBoundary::End { scope, .. } = boundary {
144 self.flush_scope(scope, emitter);
145 }
146 }
147}
148
149#[derive(Default)]
150struct TestFailureParser {
151 scopes: BTreeMap<String, TestLogReducer>,
152}
153
154impl Parser for TestFailureParser {
155 fn id(&self) -> &'static str {
156 "builtin.test-log.v1"
157 }
158
159 fn observe(&mut self, line: &LogLine<'_>, _emitter: &mut Emitter<'_>) {
160 if self.scopes.is_empty() {
161 if !TestLogReducer::may_start_failure_line(line.text) {
162 return;
163 }
164 } else {
165 let active = self
166 .scopes
167 .get(&line.scope.id)
168 .is_some_and(TestLogReducer::failure_state_active);
169 if !active && !TestLogReducer::may_start_failure_line(line.text) {
170 return;
171 }
172 }
173 let provenance = Provenance::new(stream_name(line.stream))
174 .with_scope(line.scope)
175 .with_span(line.stream, line.stream_line, line.stream_line)
176 .with_parser(self.id());
177 self.scopes
178 .entry(line.scope.id.clone())
179 .or_default()
180 .observe_failure_line(line.text, &provenance);
181 }
182
183 fn boundary(&mut self, boundary: ScopeBoundary<'_>, emitter: &mut Emitter<'_>) {
184 let ScopeBoundary::End { scope, reason, .. } = boundary else {
185 return;
186 };
187 let Some(mut reducer) = self.scopes.remove(&scope.id) else {
188 return;
189 };
190 reducer.finish_log(reason == EndReason::Complete);
191 let result = reducer.finish();
192 for failure in result.failures {
193 emitter.test_failure(failure);
194 }
195 }
196}
197
198fn stream_name(stream: Stream) -> &'static str {
199 match stream {
200 Stream::Stdout => "stdout",
201 Stream::Stderr => "stderr",
202 Stream::Combined => "combined",
203 Stream::Annotation => "annotation",
204 Stream::Structured => "structured",
205 }
206}
207
208#[cfg(test)]
209mod tests {
210 use logcompact_core::{
211 Budget, EndReason, GenericRanker, NoPathMapping, NoRedaction, OutputPolicy,
212 ReductionSession, Scope, SessionOptions,
213 };
214
215 use super::*;
216
217 fn reduce_chunks(chunks: &[&[u8]]) -> logcompact_core::Reduction {
218 let mut session = ReductionSession::new(
219 builtin_parser_plan(BuiltinParserOptions::default()),
220 SessionOptions {
221 budget: Budget::unbounded(),
222 ..SessionOptions::default()
223 },
224 OutputPolicy::new(&NoRedaction, &NoPathMapping, &GenericRanker),
225 );
226 session.begin_scope(Scope::step("tests"));
227 for chunk in chunks {
228 session.push_chunk("tests", Stream::Stderr, chunk);
229 }
230 session.end_scope("tests", EndReason::Complete);
231 session.finish()
232 }
233
234 #[test]
235 fn batch_and_arbitrary_stream_chunks_are_equivalent() {
236 let text = b"error[E0308]: mismatched types\n --> src/lib.rs:7:5\n |\n7 | value\n | ^ expected u32, found &str\n";
237 let whole = reduce_chunks(&[text]);
238 let split = reduce_chunks(&[&text[..9], &text[9..31], &text[31..]]);
239 assert_eq!(whole.diagnostics, split.diagnostics);
240 assert_eq!(
241 whole.diagnostics[0].location.as_ref().unwrap().path,
242 "src/lib.rs"
243 );
244 }
245
246 #[test]
247 fn emits_structured_test_failures_for_each_streaming_framework() {
248 for (input, expected_name) in [
249 (
250 &b"test invoice::fails ... FAILED\n---- invoice::fails stdout ----\nthread 'invoice::fails' panicked at src/lib.rs:7:3:\nassertion `left == right` failed\n"[..],
251 "invoice::fails",
252 ),
253 (
254 &b"[ RUN ] InvoiceTest.Fails\nsrc/invoice_test.cc:12: Failure\nExpected equality of these values\n[ FAILED ] InvoiceTest.Fails (1 ms)\n"[..],
255 "InvoiceTest.Fails",
256 ),
257 (
258 &b"=== RUN TestInvoice\ninvoice_test.go:7: got 2; want 3\n--- FAIL: TestInvoice (0.00s)\n"[..],
259 "TestInvoice",
260 ),
261 ] {
262 let reduction = reduce_chunks(&[input]);
263 assert_eq!(reduction.test_failures.len(), 1, "{expected_name}");
264 assert_eq!(reduction.test_failures[0].name, expected_name);
265 }
266 }
267}