rust-sanitize 0.10.0

Deterministic one-way data sanitization engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! Log context extraction — finds keyword-matching lines and captures
//! surrounding context windows for LLM-friendly log triage.
//!
//! The extractor scans sanitized output line-by-line for any configured
//! keyword (substring match). For each hit it records the matching line,
//! up to N lines of context before and after, and the 1-based line number
//! so engineers can locate the entry in the original file.
//!
//! # Example
//!
//! ```rust
//! use sanitize_engine::log_context::{LogContextConfig, extract_context};
//!
//! let log = "INFO  start\nERROR disk full\nINFO  retrying\nINFO  done";
//!
//! let config = LogContextConfig::new().with_context_lines(1);
//! let result = extract_context(log, &config);
//!
//! assert_eq!(result.match_count, 1);
//! assert_eq!(result.matches[0].line_number, 2);
//! assert_eq!(result.matches[0].keyword, "error");
//! assert_eq!(result.matches[0].before, vec!["INFO  start"]);
//! assert_eq!(result.matches[0].after,  vec!["INFO  retrying"]);
//! ```

use serde::{Deserialize, Serialize};
use std::{collections::VecDeque, io};

// ---------------------------------------------------------------------------
// Defaults
// ---------------------------------------------------------------------------

/// Built-in keywords used when no custom list is provided.
pub const DEFAULT_KEYWORDS: &[&str] = &[
    "error",
    "failure",
    "warning",
    "warn",
    "fatal",
    "exception",
    "critical",
];

/// Default lines of context captured before and after each match.
pub const DEFAULT_CONTEXT_LINES: usize = 10;

/// Default cap on matches returned in a single result.
pub const DEFAULT_MAX_MATCHES: usize = 50;

// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------

/// Configuration for [`extract_context`].
///
/// Built with a fluent API; all setters consume and return `Self`.
///
/// # Example
///
/// ```rust
/// use sanitize_engine::log_context::LogContextConfig;
///
/// let config = LogContextConfig::new()
///     .with_extra_keywords(["timeout", "oomkilled"])
///     .with_context_lines(15)
///     .with_max_matches(100);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogContextConfig {
    /// Keywords to scan for. Each is matched as a substring of the line.
    pub keywords: Vec<String>,

    /// Lines of context captured before and after each match.
    pub context_lines: usize,

    /// Maximum number of matches to return before setting
    /// [`LogContextResult::truncated`].
    pub max_matches: usize,

    /// When `true`, keyword matching is case-sensitive. Default: `false`.
    pub case_sensitive: bool,
}

impl Default for LogContextConfig {
    fn default() -> Self {
        Self {
            keywords: DEFAULT_KEYWORDS.iter().map(|&s| s.to_owned()).collect(),
            context_lines: DEFAULT_CONTEXT_LINES,
            max_matches: DEFAULT_MAX_MATCHES,
            case_sensitive: false,
        }
    }
}

impl LogContextConfig {
    /// Create a config with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Merge additional keywords into the existing list without replacing defaults.
    #[must_use]
    pub fn with_extra_keywords(
        mut self,
        extra: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.keywords.extend(extra.into_iter().map(Into::into));
        self
    }

    /// Replace all keywords with the given list.
    #[must_use]
    pub fn with_keywords(mut self, keywords: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.keywords = keywords.into_iter().map(Into::into).collect();
        self
    }

    /// Set how many lines of context to capture around each match.
    #[must_use]
    pub fn with_context_lines(mut self, n: usize) -> Self {
        self.context_lines = n;
        self
    }

    /// Set the maximum number of matches to return.
    #[must_use]
    pub fn with_max_matches(mut self, n: usize) -> Self {
        self.max_matches = n;
        self
    }

    /// Set case-sensitivity for keyword matching.
    #[must_use]
    pub fn case_sensitive(mut self, sensitive: bool) -> Self {
        self.case_sensitive = sensitive;
        self
    }
}

// ---------------------------------------------------------------------------
// Output types
// ---------------------------------------------------------------------------

/// A single keyword match with surrounding context lines.
#[derive(Debug, Clone, Serialize)]
pub struct LogContextMatch {
    /// 1-based line number of the matching line.
    pub line_number: usize,

    /// The keyword that triggered this match (preserves original casing
    /// from the config, not the casing found in the log line).
    pub keyword: String,

    /// The matching line as-is from the (sanitized) content.
    pub line: String,

    /// Up to [`LogContextConfig::context_lines`] lines immediately before
    /// the match, in document order.
    pub before: Vec<String>,

    /// Up to [`LogContextConfig::context_lines`] lines immediately after
    /// the match, in document order.
    pub after: Vec<String>,
}

/// Output of [`extract_context`].
#[derive(Debug, Clone, Serialize)]
pub struct LogContextResult {
    /// Total number of lines in the input.
    pub total_lines: usize,

    /// Number of matches present in [`Self::matches`].
    /// When [`Self::truncated`] is `true` this equals `max_matches`
    /// and additional matches exist beyond what was returned.
    pub match_count: usize,

    /// `true` when scanning stopped early because `max_matches` was reached.
    /// The caller should increase `max_matches` or narrow the keyword list
    /// if full coverage is required.
    pub truncated: bool,

    /// The matched lines and their context windows, in document order.
    pub matches: Vec<LogContextMatch>,
}

// ---------------------------------------------------------------------------
// Core function
// ---------------------------------------------------------------------------

/// Scan `content` for keyword matches and return surrounding context windows.
///
/// Each line is checked for any configured keyword as a substring match.
/// When multiple keywords appear on the same line the first keyword in
/// [`LogContextConfig::keywords`] wins. Line numbers in the output are
/// 1-based to match standard editor and log viewer conventions.
///
/// This function is allocation-efficient: lines are collected once into a
/// `Vec<&str>` and context slices reference that vec without additional copies
/// until the final owned `String`s are built for the result.
#[must_use]
pub fn extract_context(content: &str, config: &LogContextConfig) -> LogContextResult {
    let lines: Vec<&str> = content.lines().collect();
    let total_lines = lines.len();

    // Pre-normalise keywords once. Each pair is (normalised_for_comparison, original_index).
    // We store the index so we can retrieve the original keyword string for output.
    let normalised: Vec<String> = config
        .keywords
        .iter()
        .map(|kw| {
            if config.case_sensitive {
                kw.clone()
            } else {
                kw.to_lowercase()
            }
        })
        .collect();

    let mut matches: Vec<LogContextMatch> = Vec::new();
    let mut truncated = false;

    for (i, &line) in lines.iter().enumerate() {
        if matches.len() >= config.max_matches {
            truncated = true;
            break;
        }

        // Find the index of the first matching keyword.
        let hit_idx = if config.case_sensitive {
            normalised
                .iter()
                .position(|norm| line.contains(norm.as_str()))
        } else {
            let lower = line.to_lowercase();
            normalised
                .iter()
                .position(|norm| lower.contains(norm.as_str()))
        };

        if let Some(idx) = hit_idx {
            let before_start = i.saturating_sub(config.context_lines);
            let after_end = (i + config.context_lines + 1).min(total_lines);

            matches.push(LogContextMatch {
                line_number: i + 1,
                keyword: config.keywords[idx].clone(),
                line: line.to_owned(),
                before: lines[before_start..i]
                    .iter()
                    .map(|&s| s.to_owned())
                    .collect(),
                after: lines[i + 1..after_end]
                    .iter()
                    .map(|&s| s.to_owned())
                    .collect(),
            });
        }
    }

    let match_count = matches.len();
    LogContextResult {
        total_lines,
        match_count,
        truncated,
        matches,
    }
}

/// Streaming variant of [`extract_context`] for large inputs.
///
/// Reads `reader` line by line using a sliding ring buffer of
/// `config.context_lines` lines. Memory usage is
/// `O(context_lines × max_line_length)` regardless of total file size,
/// making it safe for multi-gigabyte log files.
///
/// Semantics match [`extract_context`]: case handling, `max_matches`,
/// `truncated`, and first-keyword-wins on a line all behave identically.
/// "Before" and "after" context windows are clipped at file boundaries.
///
/// # Example
///
/// ```rust
/// use sanitize_engine::log_context::{LogContextConfig, extract_context_reader};
/// use std::io::BufReader;
///
/// let data = b"INFO start\nERROR disk full\nINFO retrying\n";
/// let config = LogContextConfig::new().with_context_lines(1);
/// let result = extract_context_reader(BufReader::new(data.as_ref()), &config).unwrap();
///
/// assert_eq!(result.match_count, 1);
/// assert_eq!(result.matches[0].line_number, 2);
/// assert_eq!(result.matches[0].before, vec!["INFO start"]);
/// assert_eq!(result.matches[0].after,  vec!["INFO retrying"]);
/// ```
///
/// # Errors
///
/// Returns an [`io::Error`] if reading from `reader` fails.
#[allow(clippy::too_many_lines)]
pub fn extract_context_reader<R: io::BufRead>(
    reader: R,
    config: &LogContextConfig,
) -> io::Result<LogContextResult> {
    struct Pending {
        line_number: usize,
        keyword: String,
        line: String,
        before: Vec<String>,
        after: Vec<String>,
        remaining: usize,
    }

    let cap = config.context_lines;
    let mut before_buf: VecDeque<String> = VecDeque::with_capacity(cap.saturating_add(1));
    let mut pending: Vec<Pending> = Vec::new();
    let mut matches: Vec<LogContextMatch> = Vec::new();
    let mut truncated = false;
    let mut total_lines: usize = 0;

    // Pre-normalise keywords once (mirrors extract_context).
    let normalised: Vec<String> = config
        .keywords
        .iter()
        .map(|kw| {
            if config.case_sensitive {
                kw.clone()
            } else {
                kw.to_lowercase()
            }
        })
        .collect();

    let mut line_buf = String::new();
    let mut reader = reader;
    loop {
        line_buf.clear();
        let n = reader.read_line(&mut line_buf)?;
        if n == 0 {
            break;
        }
        // Strip trailing newline; preserve the rest of the line as-is.
        let line: &str = line_buf.trim_end_matches(['\n', '\r']);
        total_lines += 1;
        let line_number = total_lines;

        // Step 1: feed this line as "after" context to all pending matches.
        let mut i = 0;
        while i < pending.len() {
            pending[i].after.push(line.to_owned());
            pending[i].remaining -= 1;
            if pending[i].remaining == 0 {
                let p = pending.remove(i);
                matches.push(LogContextMatch {
                    line_number: p.line_number,
                    keyword: p.keyword,
                    line: p.line,
                    before: p.before,
                    after: p.after,
                });
            } else {
                i += 1;
            }
        }

        // Step 2: check if this line starts a new match.
        if !truncated {
            let effective_count = matches.len() + pending.len();
            if effective_count >= config.max_matches {
                // At the cap — check if this line would be a new match so we
                // can set the truncated flag accurately.
                let is_match = if config.case_sensitive {
                    normalised.iter().any(|norm| line.contains(norm.as_str()))
                } else {
                    let lower = line.to_lowercase();
                    normalised.iter().any(|norm| lower.contains(norm.as_str()))
                };
                if is_match {
                    truncated = true;
                }
            } else {
                let hit_idx = if config.case_sensitive {
                    normalised
                        .iter()
                        .position(|norm| line.contains(norm.as_str()))
                } else {
                    let lower = line.to_lowercase();
                    normalised
                        .iter()
                        .position(|norm| lower.contains(norm.as_str()))
                };
                if let Some(idx) = hit_idx {
                    let before: Vec<String> = before_buf.iter().cloned().collect();
                    if cap == 0 {
                        matches.push(LogContextMatch {
                            line_number,
                            keyword: config.keywords[idx].clone(),
                            line: line.to_owned(),
                            before,
                            after: Vec::new(),
                        });
                    } else {
                        pending.push(Pending {
                            line_number,
                            keyword: config.keywords[idx].clone(),
                            line: line.to_owned(),
                            before,
                            after: Vec::new(),
                            remaining: cap,
                        });
                    }
                }
            }
        }

        // Step 3: advance the before-context ring buffer.
        if cap > 0 {
            if before_buf.len() >= cap {
                before_buf.pop_front();
            }
            before_buf.push_back(line.to_owned());
        }
    }

    // Flush pending matches whose "after" windows were not fully filled
    // before EOF (context clipped at end of file).
    for p in pending {
        matches.push(LogContextMatch {
            line_number: p.line_number,
            keyword: p.keyword,
            line: p.line,
            before: p.before,
            after: p.after,
        });
    }

    let match_count = matches.len();
    Ok(LogContextResult {
        total_lines,
        match_count,
        truncated,
        matches,
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn make_log(lines: &[&str]) -> String {
        lines.join("\n")
    }

    // ---- basic matching ----

    #[test]
    fn finds_error_line() {
        let log = make_log(&["INFO start", "ERROR disk full", "INFO done"]);
        let result = extract_context(&log, &LogContextConfig::new().with_context_lines(0));
        assert_eq!(result.match_count, 1);
        assert_eq!(result.matches[0].line_number, 2);
        assert_eq!(result.matches[0].keyword, "error");
        assert_eq!(result.matches[0].line, "ERROR disk full");
    }

    #[test]
    fn case_insensitive_by_default() {
        let log = make_log(&["WARNING high load", "Warning: retry", "warn: slow"]);
        let result = extract_context(&log, &LogContextConfig::new().with_context_lines(0));
        assert_eq!(result.match_count, 3);
    }

    #[test]
    fn case_sensitive_skips_uppercase() {
        let log = make_log(&["ERROR upper", "error lower"]);
        let config = LogContextConfig::new()
            .with_keywords(["error"])
            .case_sensitive(true)
            .with_context_lines(0);
        let result = extract_context(&log, &config);
        assert_eq!(result.match_count, 1);
        assert_eq!(result.matches[0].line, "error lower");
    }

    // ---- context windows ----

    #[test]
    fn before_and_after_lines() {
        let log = make_log(&["a", "b", "ERROR c", "d", "e"]);
        let config = LogContextConfig::new()
            .with_keywords(["error"])
            .with_context_lines(1);
        let result = extract_context(&log, &config);
        assert_eq!(result.matches[0].before, vec!["b"]);
        assert_eq!(result.matches[0].after, vec!["d"]);
    }

    #[test]
    fn context_clipped_at_file_start() {
        let log = make_log(&["ERROR first", "INFO second", "INFO third"]);
        let config = LogContextConfig::new()
            .with_keywords(["error"])
            .with_context_lines(5);
        let result = extract_context(&log, &config);
        assert!(result.matches[0].before.is_empty());
        assert_eq!(result.matches[0].after.len(), 2);
    }

    #[test]
    fn context_clipped_at_file_end() {
        let log = make_log(&["INFO first", "INFO second", "ERROR last"]);
        let config = LogContextConfig::new()
            .with_keywords(["error"])
            .with_context_lines(5);
        let result = extract_context(&log, &config);
        assert_eq!(result.matches[0].before.len(), 2);
        assert!(result.matches[0].after.is_empty());
    }

    #[test]
    fn context_lines_zero() {
        let log = make_log(&["a", "ERROR b", "c"]);
        let config = LogContextConfig::new()
            .with_keywords(["error"])
            .with_context_lines(0);
        let result = extract_context(&log, &config);
        assert!(result.matches[0].before.is_empty());
        assert!(result.matches[0].after.is_empty());
    }

    // ---- multiple matches ----

    #[test]
    fn multiple_matches_in_order() {
        let log = make_log(&["ERROR a", "INFO b", "FATAL c"]);
        let config = LogContextConfig::new()
            .with_keywords(["error", "fatal"])
            .with_context_lines(0);
        let result = extract_context(&log, &config);
        assert_eq!(result.match_count, 2);
        assert_eq!(result.matches[0].line_number, 1);
        assert_eq!(result.matches[0].keyword, "error");
        assert_eq!(result.matches[1].line_number, 3);
        assert_eq!(result.matches[1].keyword, "fatal");
    }

    #[test]
    fn first_keyword_wins_on_same_line() {
        let log = "ERROR and WARNING on same line";
        let config = LogContextConfig::new()
            .with_keywords(["error", "warning"])
            .with_context_lines(0);
        let result = extract_context(log, &config);
        assert_eq!(result.match_count, 1);
        assert_eq!(result.matches[0].keyword, "error");
    }

    // ---- max_matches and truncation ----

    #[test]
    fn truncated_when_max_reached() {
        let lines: Vec<String> = (0..10).map(|i| format!("ERROR line {i}")).collect();
        let log = lines.join("\n");
        let config = LogContextConfig::new()
            .with_keywords(["error"])
            .with_max_matches(3)
            .with_context_lines(0);
        let result = extract_context(&log, &config);
        assert_eq!(result.match_count, 3);
        assert!(result.truncated);
    }

    #[test]
    fn not_truncated_under_limit() {
        let log = make_log(&["ERROR a", "INFO b", "ERROR c"]);
        let config = LogContextConfig::new()
            .with_keywords(["error"])
            .with_max_matches(10)
            .with_context_lines(0);
        let result = extract_context(&log, &config);
        assert_eq!(result.match_count, 2);
        assert!(!result.truncated);
    }

    // ---- extra keywords ----

    #[test]
    fn extra_keywords_merge_with_defaults() {
        let log = make_log(&["ERROR a", "OOMKILLED b"]);
        let config = LogContextConfig::new()
            .with_extra_keywords(["oomkilled"])
            .with_context_lines(0);
        let result = extract_context(&log, &config);
        assert_eq!(result.match_count, 2);
    }

    #[test]
    fn replace_keywords_removes_defaults() {
        let log = make_log(&["ERROR a", "CUSTOM b"]);
        let config = LogContextConfig::new()
            .with_keywords(["custom"])
            .with_context_lines(0);
        let result = extract_context(&log, &config);
        assert_eq!(result.match_count, 1);
        assert_eq!(result.matches[0].keyword, "custom");
    }

    // ---- edge cases ----

    #[test]
    fn empty_content() {
        let result = extract_context("", &LogContextConfig::new());
        assert_eq!(result.total_lines, 0);
        assert_eq!(result.match_count, 0);
        assert!(!result.truncated);
    }

    #[test]
    fn no_matches() {
        let log = make_log(&["INFO all good", "DEBUG trace", "INFO done"]);
        let result = extract_context(&log, &LogContextConfig::new());
        assert_eq!(result.match_count, 0);
        assert!(!result.truncated);
        assert_eq!(result.total_lines, 3);
    }

    #[test]
    fn single_line_match() {
        let result = extract_context("ERROR only line", &LogContextConfig::new());
        assert_eq!(result.total_lines, 1);
        assert_eq!(result.match_count, 1);
        assert!(result.matches[0].before.is_empty());
        assert!(result.matches[0].after.is_empty());
    }

    #[test]
    fn line_numbers_are_one_based() {
        let log = make_log(&["INFO a", "INFO b", "ERROR c"]);
        let config = LogContextConfig::new()
            .with_keywords(["error"])
            .with_context_lines(0);
        let result = extract_context(&log, &config);
        assert_eq!(result.matches[0].line_number, 3);
    }

    #[test]
    fn keyword_original_case_preserved_in_output() {
        let log = "TIMEOUT occurred";
        let config = LogContextConfig::new()
            .with_keywords(["Timeout"])
            .with_context_lines(0);
        let result = extract_context(log, &config);
        assert_eq!(result.match_count, 1);
        assert_eq!(result.matches[0].keyword, "Timeout");
    }

    // ---- serialization ----

    #[test]
    fn result_serializes_to_json() {
        let log = make_log(&["INFO ok", "ERROR fail", "INFO ok"]);
        let config = LogContextConfig::new()
            .with_keywords(["error"])
            .with_context_lines(1);
        let result = extract_context(&log, &config);
        let json = serde_json::to_string_pretty(&result).unwrap();
        assert!(json.contains("\"line_number\": 2"));
        assert!(json.contains("\"keyword\": \"error\""));
        assert!(json.contains("\"total_lines\": 3"));
        assert!(json.contains("\"truncated\": false"));
    }
}