tarpit_log_parser/
lib.rs

1use chrono::NaiveDateTime;
2use nom::character::complete::line_ending;
3use nom::multi::separated_list0;
4use nom::Finish;
5use parsing::parse_log_line;
6use std::net::SocketAddrV4;
7
8mod parsing;
9#[cfg(test)]
10mod tests;
11
12#[derive(Debug)]
13pub struct TarpitLog {
14    pub lines: Vec<TarpitLogEntry>,
15}
16
17#[derive(Debug, PartialEq)]
18pub enum TarpitLogEntry {
19    Message {
20        timestamp: NaiveDateTime,
21        log_level: LogLevel,
22        issuer: String,
23        message: String,
24    },
25    Event {
26        timestamp: NaiveDateTime,
27        ip: SocketAddrV4,
28        action: Action,
29        log_level: LogLevel,
30    },
31}
32
33#[derive(Debug, PartialEq)]
34pub enum Action {
35    Connect,
36    Disconnect,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq)]
40pub enum LogLevel {
41    Trace,
42    Debug,
43    Info,
44    Warn,
45    Error,
46}
47
48// TODO: error handling
49pub fn parse_tarpit_log(input: &str) -> Result<TarpitLog, String> {
50    separated_list0(line_ending, parse_log_line)(input)
51        .finish()
52        .map(|result| TarpitLog { lines: result.1 })
53        .map_err(|e: nom::error::Error<&str>| e.to_string())
54}