1use crate::decode_anomaly::DecodeAnomaly;
2use crate::decode_output::DecodeOutput;
3use crate::decode_result::DecodeResult;
4use crate::incoming_log_level::IncomingLogLevel;
5use crate::log_level::LogLevel;
6use crate::log_line::LogLine;
7
8pub struct LogDecoder {
9 buffered: Option<LogLine>,
10 previous_level: LogLevel,
11}
12
13impl LogDecoder {
14 #[must_use]
15 pub const fn new() -> Self {
16 Self {
17 buffered: None,
18 previous_level: LogLevel::None,
19 }
20 }
21
22 pub fn feed(&mut self, level: IncomingLogLevel, text: &str) -> DecodeResult {
23 match level {
24 IncomingLogLevel::Cont => self.feed_cont(text),
25 IncomingLogLevel::Debug => self.feed_non_cont(LogLevel::Debug, text),
26 IncomingLogLevel::Error => self.feed_non_cont(LogLevel::Error, text),
27 IncomingLogLevel::Info => self.feed_non_cont(LogLevel::Info, text),
28 IncomingLogLevel::None => self.feed_non_cont(LogLevel::None, text),
29 IncomingLogLevel::Unknown(raw) => self.feed_non_cont(LogLevel::Unknown(raw), text),
30 IncomingLogLevel::Warn => self.feed_non_cont(LogLevel::Warn, text),
31 }
32 }
33
34 fn feed_cont(&mut self, text: &str) -> DecodeResult {
35 if let Some(LogLine {
36 level,
37 text: mut buffer,
38 }) = self.buffered.take()
39 {
40 buffer.push_str(text);
41 if let Some(without_newline) = buffer.strip_suffix('\n') {
42 DecodeResult {
43 output: DecodeOutput::Line(LogLine {
44 level,
45 text: without_newline.to_owned(),
46 }),
47 anomaly: None,
48 }
49 } else {
50 self.buffered = Some(LogLine {
51 level,
52 text: buffer,
53 });
54 DecodeResult {
55 output: DecodeOutput::None,
56 anomaly: None,
57 }
58 }
59 } else {
60 self.feed_orphan_cont(text)
61 }
62 }
63
64 fn feed_orphan_cont(&mut self, text: &str) -> DecodeResult {
65 let level = self.previous_level;
66 if let Some(without_newline) = text.strip_suffix('\n') {
67 DecodeResult {
68 output: DecodeOutput::Line(LogLine {
69 level,
70 text: without_newline.to_owned(),
71 }),
72 anomaly: Some(DecodeAnomaly::OrphanCont),
73 }
74 } else {
75 self.buffered = Some(LogLine {
76 level,
77 text: text.to_owned(),
78 });
79 DecodeResult {
80 output: DecodeOutput::None,
81 anomaly: Some(DecodeAnomaly::OrphanCont),
82 }
83 }
84 }
85
86 fn feed_non_cont(&mut self, level: LogLevel, text: &str) -> DecodeResult {
87 self.previous_level = level;
88 let stale = self.buffered.take();
89 match (text.strip_suffix('\n'), stale) {
90 (
91 Some(without_newline),
92 Some(LogLine {
93 level: stale_level,
94 text: stale_text,
95 }),
96 ) => DecodeResult {
97 output: DecodeOutput::TwoLines {
98 earlier: LogLine {
99 level: stale_level,
100 text: stale_text,
101 },
102 current: LogLine {
103 level,
104 text: without_newline.to_owned(),
105 },
106 },
107 anomaly: Some(DecodeAnomaly::StaleBufferAbandoned),
108 },
109 (Some(without_newline), None) => DecodeResult {
110 output: DecodeOutput::Line(LogLine {
111 level,
112 text: without_newline.to_owned(),
113 }),
114 anomaly: None,
115 },
116 (
117 None,
118 Some(LogLine {
119 level: stale_level,
120 text: stale_text,
121 }),
122 ) => {
123 self.buffered = Some(LogLine {
124 level,
125 text: text.to_owned(),
126 });
127 DecodeResult {
128 output: DecodeOutput::Line(LogLine {
129 level: stale_level,
130 text: stale_text,
131 }),
132 anomaly: Some(DecodeAnomaly::StaleBufferAbandoned),
133 }
134 }
135 (None, None) => {
136 self.buffered = Some(LogLine {
137 level,
138 text: text.to_owned(),
139 });
140 DecodeResult {
141 output: DecodeOutput::None,
142 anomaly: None,
143 }
144 }
145 }
146 }
147}
148
149impl Default for LogDecoder {
150 fn default() -> Self {
151 Self::new()
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 use super::LogDecoder;
158 use crate::decode_anomaly::DecodeAnomaly;
159 use crate::decode_output::DecodeOutput;
160 use crate::decode_result::DecodeResult;
161 use crate::incoming_log_level::IncomingLogLevel;
162 use crate::log_level::LogLevel;
163 use crate::log_line::LogLine;
164
165 #[test]
166 fn feed_complete_info_line() {
167 let mut decoder = LogDecoder::new();
168 let result = decoder.feed(IncomingLogLevel::Info, "hello\n");
169
170 assert_eq!(
171 result,
172 DecodeResult {
173 output: DecodeOutput::Line(LogLine {
174 level: LogLevel::Info,
175 text: "hello".to_owned(),
176 }),
177 anomaly: None,
178 }
179 );
180 }
181
182 #[test]
183 fn feed_partial_without_newline() {
184 let mut decoder = LogDecoder::new();
185 let result = decoder.feed(IncomingLogLevel::Info, "hello");
186
187 assert_eq!(
188 result,
189 DecodeResult {
190 output: DecodeOutput::None,
191 anomaly: None,
192 }
193 );
194 }
195
196 #[test]
197 fn feed_cont_completion() {
198 let mut decoder = LogDecoder::new();
199 decoder.feed(IncomingLogLevel::Info, "hello ");
200 let result = decoder.feed(IncomingLogLevel::Cont, "world\n");
201
202 assert_eq!(
203 result,
204 DecodeResult {
205 output: DecodeOutput::Line(LogLine {
206 level: LogLevel::Info,
207 text: "hello world".to_owned(),
208 }),
209 anomaly: None,
210 }
211 );
212 }
213
214 #[test]
215 fn feed_multi_part_cont() {
216 let mut decoder = LogDecoder::new();
217 decoder.feed(IncomingLogLevel::Info, "part1 ");
218 decoder.feed(IncomingLogLevel::Cont, "part2 ");
219 let result = decoder.feed(IncomingLogLevel::Cont, "part3\n");
220
221 assert_eq!(
222 result,
223 DecodeResult {
224 output: DecodeOutput::Line(LogLine {
225 level: LogLevel::Info,
226 text: "part1 part2 part3".to_owned(),
227 }),
228 anomaly: None,
229 }
230 );
231 }
232
233 #[test]
234 fn feed_non_cont_while_buffering() {
235 let mut decoder = LogDecoder::new();
236 decoder.feed(IncomingLogLevel::Info, "stale");
237 let result = decoder.feed(IncomingLogLevel::Warn, "fresh\n");
238
239 assert_eq!(
240 result,
241 DecodeResult {
242 output: DecodeOutput::TwoLines {
243 earlier: LogLine {
244 level: LogLevel::Info,
245 text: "stale".to_owned(),
246 },
247 current: LogLine {
248 level: LogLevel::Warn,
249 text: "fresh".to_owned(),
250 },
251 },
252 anomaly: Some(DecodeAnomaly::StaleBufferAbandoned),
253 }
254 );
255 }
256
257 #[test]
258 fn feed_buffer_replacement() {
259 let mut decoder = LogDecoder::new();
260 decoder.feed(IncomingLogLevel::Info, "first");
261 let result = decoder.feed(IncomingLogLevel::Warn, "second");
262
263 assert_eq!(
264 result,
265 DecodeResult {
266 output: DecodeOutput::Line(LogLine {
267 level: LogLevel::Info,
268 text: "first".to_owned(),
269 }),
270 anomaly: Some(DecodeAnomaly::StaleBufferAbandoned),
271 }
272 );
273
274 let follow_up = decoder.feed(IncomingLogLevel::Cont, "more\n");
275 assert_eq!(
276 follow_up,
277 DecodeResult {
278 output: DecodeOutput::Line(LogLine {
279 level: LogLevel::Warn,
280 text: "secondmore".to_owned(),
281 }),
282 anomaly: None,
283 }
284 );
285 }
286
287 #[test]
288 fn feed_orphan_cont() {
289 let mut decoder = LogDecoder::new();
290 let result = decoder.feed(IncomingLogLevel::Cont, "ghost\n");
291
292 assert_eq!(
293 result,
294 DecodeResult {
295 output: DecodeOutput::Line(LogLine {
296 level: LogLevel::None,
297 text: "ghost".to_owned(),
298 }),
299 anomaly: Some(DecodeAnomaly::OrphanCont),
300 }
301 );
302 }
303
304 #[test]
305 fn feed_orphan_cont_previous_level() {
306 let mut decoder = LogDecoder::new();
307 decoder.feed(IncomingLogLevel::Warn, "complete\n");
308 let result = decoder.feed(IncomingLogLevel::Cont, "ghost\n");
309
310 assert_eq!(
311 result,
312 DecodeResult {
313 output: DecodeOutput::Line(LogLine {
314 level: LogLevel::Warn,
315 text: "ghost".to_owned(),
316 }),
317 anomaly: Some(DecodeAnomaly::OrphanCont),
318 }
319 );
320 }
321
322 #[test]
323 fn feed_none_level() {
324 let mut decoder = LogDecoder::new();
325 let result = decoder.feed(IncomingLogLevel::None, "no-level\n");
326
327 assert_eq!(
328 result,
329 DecodeResult {
330 output: DecodeOutput::Line(LogLine {
331 level: LogLevel::None,
332 text: "no-level".to_owned(),
333 }),
334 anomaly: None,
335 }
336 );
337 }
338
339 #[test]
340 fn feed_unknown_level() {
341 let mut decoder = LogDecoder::new();
342 let result = decoder.feed(IncomingLogLevel::Unknown(9999), "weird\n");
343
344 assert_eq!(
345 result,
346 DecodeResult {
347 output: DecodeOutput::Line(LogLine {
348 level: LogLevel::Unknown(9999),
349 text: "weird".to_owned(),
350 }),
351 anomaly: None,
352 }
353 );
354 }
355
356 #[test]
357 fn feed_debug_level() {
358 let mut decoder = LogDecoder::new();
359 let result = decoder.feed(IncomingLogLevel::Debug, "trace\n");
360
361 assert_eq!(
362 result,
363 DecodeResult {
364 output: DecodeOutput::Line(LogLine {
365 level: LogLevel::Debug,
366 text: "trace".to_owned(),
367 }),
368 anomaly: None,
369 }
370 );
371 }
372
373 #[test]
374 fn feed_error_level() {
375 let mut decoder = LogDecoder::new();
376 let result = decoder.feed(IncomingLogLevel::Error, "boom\n");
377
378 assert_eq!(
379 result,
380 DecodeResult {
381 output: DecodeOutput::Line(LogLine {
382 level: LogLevel::Error,
383 text: "boom".to_owned(),
384 }),
385 anomaly: None,
386 }
387 );
388 }
389
390 #[test]
391 fn feed_orphan_cont_without_newline_buffers_text() {
392 let mut decoder = LogDecoder::new();
393 let result = decoder.feed(IncomingLogLevel::Cont, "fragment");
394
395 assert_eq!(
396 result,
397 DecodeResult {
398 output: DecodeOutput::None,
399 anomaly: Some(DecodeAnomaly::OrphanCont),
400 }
401 );
402
403 let follow_up = decoder.feed(IncomingLogLevel::Cont, " rest\n");
404 assert_eq!(
405 follow_up,
406 DecodeResult {
407 output: DecodeOutput::Line(LogLine {
408 level: LogLevel::None,
409 text: "fragment rest".to_owned(),
410 }),
411 anomaly: None,
412 }
413 );
414 }
415
416 #[test]
417 fn default_construction() {
418 let mut default_decoder = LogDecoder::default();
419 let new_decoder_result = LogDecoder::new().feed(IncomingLogLevel::Info, "compare\n");
420 let default_result = default_decoder.feed(IncomingLogLevel::Info, "compare\n");
421
422 assert_eq!(default_result, new_decoder_result);
423 }
424}