buildlog-consultant 0.1.3

buildlog parser and analyser
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
//! Module providing pattern matching functionality for log analysis.
//!
//! This module contains tools for matching patterns in logs and extracting problems.
//! It includes regex-based matchers and a matcher group for combining multiple matchers.

use crate::SingleLineMatch;
use crate::{Match, Origin, Problem};
use regex::{Captures, Regex};
use std::fmt::Display;

/// Type alias for the result of extracting a match and optional problem
pub type MatchResult = Result<Option<(Box<dyn Match>, Option<Box<dyn Problem>>)>, Error>;

/// Type alias for a callback function that processes regex captures
pub type RegexCallback =
    Box<dyn Fn(&Captures) -> Result<Option<Box<dyn Problem>>, Error> + Send + Sync>;

/// Error type for matchers.
///
/// Used when pattern matching or problem extraction fails.
#[derive(Debug)]
pub struct Error {
    /// Error message describing what went wrong.
    pub message: String,
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.message.fmt(f)
    }
}

impl std::error::Error for Error {}

/// A matcher that uses regular expressions to find patterns in single lines.
///
/// This matcher applies a regex to individual lines and can extract problem information
/// through a callback function when a match is found.
pub struct RegexLineMatcher {
    /// The regular expression to match against each line.
    regex: Regex,
    /// Callback function that extracts problem information from regex captures.
    callback: RegexCallback,
}

/// Trait for pattern matchers that can extract matches and problems from logs.
///
/// Implementors of this trait can search through log lines to find patterns and
/// extract problem information.
pub trait Matcher: Sync {
    /// Extracts a match and optional problem from a specific line in a log.
    ///
    /// # Arguments
    /// * `lines` - The collection of log lines
    /// * `offset` - The line offset to analyze
    ///
    /// # Returns
    /// * `Ok(Some((match, problem)))` - A match was found along with an optional problem
    /// * `Ok(None)` - No match was found
    /// * `Err(error)` - An error occurred during matching
    fn extract_from_lines(&self, lines: &[&str], offset: usize) -> MatchResult;
}

impl RegexLineMatcher {
    /// Creates a new `RegexLineMatcher` with the given regex and callback.
    ///
    /// # Arguments
    /// * `regex` - The regex pattern to match against lines
    /// * `callback` - Function that processes regex captures and returns an optional problem
    ///
    /// # Returns
    /// A new `RegexLineMatcher` instance
    pub fn new(regex: Regex, callback: RegexCallback) -> Self {
        Self { regex, callback }
    }

    /// Checks if a line matches the regex pattern.
    ///
    /// # Arguments
    /// * `line` - The line to check
    ///
    /// # Returns
    /// `true` if the line matches the pattern, `false` otherwise
    pub fn matches_line(&self, line: &str) -> bool {
        self.regex.is_match(line)
    }

    /// Attempts to extract problem information from a line.
    ///
    /// # Arguments
    /// * `line` - The line to analyze
    ///
    /// # Returns
    /// * `Ok(Some(Some(problem)))` - A match was found and a problem was extracted
    /// * `Ok(Some(None))` - A match was found but no problem was extracted
    /// * `Ok(None)` - No match was found
    /// * `Err(error)` - An error occurred during matching or problem extraction
    pub fn extract_from_line(&self, line: &str) -> Result<Option<Option<Box<dyn Problem>>>, Error> {
        let c = self.regex.captures(line);
        if let Some(c) = c {
            return Ok(Some((self.callback)(&c)?));
        }
        Ok(None)
    }

    /// Creates an origin identifier for matches from this matcher.
    ///
    /// # Returns
    /// An `Origin` identifying the regex pattern used for matching
    fn origin(&self) -> Origin {
        Origin(format!("direct regex ({})", self.regex.as_str()))
    }
}

impl Matcher for RegexLineMatcher {
    fn extract_from_lines(&self, lines: &[&str], offset: usize) -> MatchResult {
        let line = lines[offset];
        if let Some(problem) = self.extract_from_line(line)? {
            let m = SingleLineMatch {
                offset,
                line: line.to_string(),
                origin: self.origin(),
            };
            return Ok(Some((Box::new(m), problem)));
        }
        Ok(None)
    }
}

/// Macro for creating regex-based line matchers.
///
/// This macro simplifies the creation of `RegexLineMatcher` instances by automatically
/// handling regex compilation and callback boxing.
///
/// # Examples
///
/// ```
/// # use buildlog_consultant::regex_line_matcher;
/// # use buildlog_consultant::r#match::RegexLineMatcher;
/// // With callback
/// let matcher = regex_line_matcher!(r"error: (.*)", |captures| {
///     let message = captures.get(1).unwrap().as_str();
///     // Process the error message
///     Ok(None)
/// });
///
/// // Without callback (just matches the pattern)
/// let simple_matcher = regex_line_matcher!(r"warning");
/// ```
#[macro_export]
macro_rules! regex_line_matcher {
    ($regex:expr, $callback:expr) => {
        Box::new(RegexLineMatcher::new(
            regex::Regex::new($regex).unwrap(),
            Box::new($callback),
        ))
    };
    ($regex: expr) => {
        Box::new(RegexLineMatcher::new(
            regex::Regex::new($regex).unwrap(),
            Box::new(|_| Ok(None)),
        ))
    };
}

/// Macro for creating regex-based paragraph matchers.
///
/// This macro is similar to `regex_line_matcher`, but creates matchers that can match
/// across multiple lines by automatically enabling the "dot matches newline" regex flag (?s).
///
/// # Examples
///
/// ```
/// # use buildlog_consultant::regex_para_matcher;
/// # use buildlog_consultant::r#match::RegexLineMatcher;
/// // With callback
/// let matcher = regex_para_matcher!(r"BEGIN(.*?)END", |captures| {
///     let content = captures.get(1).unwrap().as_str();
///     // Process the content between BEGIN and END
///     Ok(None)
/// });
///
/// // Without callback
/// let simple_matcher = regex_para_matcher!(r"function\s*\{.*?\}");
/// ```
#[macro_export]
macro_rules! regex_para_matcher {
    ($regex:expr, $callback:expr) => {{
        Box::new(RegexLineMatcher::new(
            regex::Regex::new(concat!("(?s)", $regex)).unwrap(),
            Box::new($callback),
        ))
    }};
    ($regex: expr) => {{
        Box::new(RegexLineMatcher::new(
            regex::Regex::new(concat!("(?s)", $regex)).unwrap(),
            Box::new(|_| Ok(None)),
        ))
    }};
}

/// A group of matchers that can be used to match multiple patterns.
///
/// This struct allows combining multiple matchers and trying them in sequence
/// until a match is found.
pub struct MatcherGroup(Vec<Box<dyn Matcher>>);

impl MatcherGroup {
    /// Creates a new `MatcherGroup` with the given matchers.
    ///
    /// # Arguments
    /// * `matchers` - Vector of boxed matchers
    ///
    /// # Returns
    /// A new `MatcherGroup` instance
    pub fn new(matchers: Vec<Box<dyn Matcher>>) -> Self {
        Self(matchers)
    }
}

impl Default for MatcherGroup {
    fn default() -> Self {
        Self::new(vec![])
    }
}

impl From<Vec<Box<dyn Matcher>>> for MatcherGroup {
    fn from(matchers: Vec<Box<dyn Matcher>>) -> Self {
        Self::new(matchers)
    }
}

impl MatcherGroup {
    /// Tries each matcher in the group until one finds a match.
    ///
    /// This method attempts to extract a match and problem from a specific line
    /// by trying each matcher in the group in sequence until one succeeds.
    ///
    /// # Arguments
    /// * `lines` - The collection of log lines
    /// * `offset` - The line offset to analyze
    ///
    /// # Returns
    /// * `Ok(Some((match, problem)))` - A match was found by one of the matchers
    /// * `Ok(None)` - No match was found by any matcher
    /// * `Err(error)` - An error occurred during matching
    pub fn extract_from_lines(&self, lines: &[&str], offset: usize) -> MatchResult {
        for matcher in self.0.iter() {
            if let Some(p) = matcher.extract_from_lines(lines, offset)? {
                return Ok(Some(p));
            }
        }
        Ok(None)
    }
}

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

    #[derive(Debug)]
    struct TestProblem {
        description: String,
    }

    impl std::fmt::Display for TestProblem {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{}", self.description)
        }
    }

    impl Problem for TestProblem {
        fn kind(&self) -> Cow<'_, str> {
            Cow::Borrowed("test")
        }

        fn json(&self) -> serde_json::Value {
            serde_json::json!({
                "description": self.description,
            })
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    #[test]
    fn test_error_display() {
        let error = Error {
            message: "test error".to_string(),
        };
        assert_eq!(error.to_string(), "test error");
    }

    #[test]
    fn test_regex_line_matcher_new() {
        let regex = Regex::new(r"test").unwrap();
        let callback = Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> {
            Ok(Some(Box::new(TestProblem {
                description: "test problem".to_string(),
            })))
        });
        let matcher = RegexLineMatcher::new(regex, callback);
        assert!(matcher.matches_line("test line"));
        assert!(!matcher.matches_line("other line"));
    }

    #[test]
    fn test_regex_line_matcher_matches_line() {
        let regex = Regex::new(r"test").unwrap();
        let callback =
            Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> { Ok(None) });
        let matcher = RegexLineMatcher::new(regex, callback);
        assert!(matcher.matches_line("test line"));
        assert!(!matcher.matches_line("other line"));
    }

    #[test]
    fn test_regex_line_matcher_extract_from_line() {
        let regex = Regex::new(r"test").unwrap();
        let callback = Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> {
            Ok(Some(Box::new(TestProblem {
                description: "test problem".to_string(),
            })))
        });
        let matcher = RegexLineMatcher::new(regex, callback);
        let result = matcher.extract_from_line("test line").unwrap();
        assert!(result.is_some());
        let problem = result.unwrap();
        assert!(problem.is_some());
        let problem = problem.unwrap();
        assert_eq!(problem.kind(), "test");
    }

    #[test]
    fn test_regex_line_matcher_extract_from_line_no_match() {
        let regex = Regex::new(r"test").unwrap();
        let callback = Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> {
            Ok(Some(Box::new(TestProblem {
                description: "test problem".to_string(),
            })))
        });
        let matcher = RegexLineMatcher::new(regex, callback);
        let result = matcher.extract_from_line("other line").unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_regex_line_matcher_extract_from_line_no_problem() {
        let regex = Regex::new(r"test").unwrap();
        let callback =
            Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> { Ok(None) });
        let matcher = RegexLineMatcher::new(regex, callback);
        let result = matcher.extract_from_line("test line").unwrap();
        assert!(result.is_some());
        let problem = result.unwrap();
        assert!(problem.is_none());
    }

    #[test]
    fn test_regex_line_matcher_extract_from_lines() {
        let regex = Regex::new(r"test").unwrap();
        let callback = Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> {
            Ok(Some(Box::new(TestProblem {
                description: "test problem".to_string(),
            })))
        });
        let matcher = RegexLineMatcher::new(regex, callback);
        let lines = vec!["line 1", "test line", "line 3"];
        let result = matcher.extract_from_lines(&lines, 1).unwrap();
        assert!(result.is_some());
        let (m, problem) = result.unwrap();
        assert_eq!(m.line(), "test line");
        assert_eq!(m.offset(), 1);
        assert!(problem.is_some());
        let problem = problem.unwrap();
        assert_eq!(problem.kind(), "test");
    }

    #[test]
    fn test_regex_line_matcher_extract_from_lines_no_match() {
        let regex = Regex::new(r"test").unwrap();
        let callback = Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> {
            Ok(Some(Box::new(TestProblem {
                description: "test problem".to_string(),
            })))
        });
        let matcher = RegexLineMatcher::new(regex, callback);
        let lines = vec!["line 1", "line 2", "line 3"];
        let result = matcher.extract_from_lines(&lines, 1).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_matcher_group() {
        let regex1 = Regex::new(r"test1").unwrap();
        let callback1 = Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> {
            Ok(Some(Box::new(TestProblem {
                description: "test problem 1".to_string(),
            })))
        });
        let matcher1 = RegexLineMatcher::new(regex1, callback1);

        let regex2 = Regex::new(r"test2").unwrap();
        let callback2 = Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> {
            Ok(Some(Box::new(TestProblem {
                description: "test problem 2".to_string(),
            })))
        });
        let matcher2 = RegexLineMatcher::new(regex2, callback2);

        let group = MatcherGroup::new(vec![Box::new(matcher1), Box::new(matcher2)]);
        let lines = vec!["line 1", "test2 line", "line 3"];
        let result = group.extract_from_lines(&lines, 1).unwrap();
        assert!(result.is_some());
        let (m, problem) = result.unwrap();
        assert_eq!(m.line(), "test2 line");
        assert_eq!(m.offset(), 1);
        assert!(problem.is_some());
        let problem = problem.unwrap();
        assert_eq!(problem.kind(), "test");
    }

    #[test]
    fn test_matcher_group_no_match() {
        let regex1 = Regex::new(r"test1").unwrap();
        let callback1 = Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> {
            Ok(Some(Box::new(TestProblem {
                description: "test problem 1".to_string(),
            })))
        });
        let matcher1 = RegexLineMatcher::new(regex1, callback1);

        let regex2 = Regex::new(r"test2").unwrap();
        let callback2 = Box::new(|_: &Captures| -> Result<Option<Box<dyn Problem>>, Error> {
            Ok(Some(Box::new(TestProblem {
                description: "test problem 2".to_string(),
            })))
        });
        let matcher2 = RegexLineMatcher::new(regex2, callback2);

        let group = MatcherGroup::new(vec![Box::new(matcher1), Box::new(matcher2)]);
        let lines = vec!["line 1", "line 2", "line 3"];
        let result = group.extract_from_lines(&lines, 1).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_regex_line_matcher_macro() {
        let matcher = regex_line_matcher!(r"test", |_| {
            Ok(Some(Box::new(TestProblem {
                description: "test problem".to_string(),
            })))
        });
        let lines = vec!["line 1", "test line", "line 3"];
        let result = matcher.extract_from_lines(&lines, 1).unwrap();
        assert!(result.is_some());
    }

    #[test]
    fn test_regex_line_matcher_macro_simple() {
        let matcher = regex_line_matcher!(r"test");
        let lines = vec!["line 1", "test line", "line 3"];
        let result = matcher.extract_from_lines(&lines, 1).unwrap();
        assert!(result.is_some());
        let (_m, problem) = result.unwrap();
        assert!(problem.is_none());
    }
}