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
use crate::event::Event;
use crate::pipeline::EventParser;
use crate::rhai_functions::columns::{
parse_cols_whitespace, parse_cols_with_sep, set_parse_cols_strict,
};
use anyhow::Result;
/// Parser for column-based text input using parse_cols spec
pub struct ColsParser {
spec: String,
separator: Option<String>,
}
impl ColsParser {
pub fn new(spec: String, separator: Option<String>) -> Self {
Self { spec, separator }
}
}
impl EventParser for ColsParser {
fn parse(&self, line: &str) -> Result<Event> {
// Set strict mode to false for resilient parsing
set_parse_cols_strict(false);
let result = if let Some(ref sep) = self.separator {
parse_cols_with_sep(line, &self.spec, sep)
} else {
parse_cols_whitespace(line, &self.spec)
};
match result {
Ok(map) => {
let mut event = Event::default_with_line(line.to_string());
for (key, value) in map {
// Insert Rhai Dynamic values directly into event
event.fields.insert(key.to_string(), value);
}
Ok(event)
}
Err(err) => {
// Return error for the parser to handle according to strict/resilient mode
Err(anyhow::anyhow!("{}", err))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cols_parser_whitespace() {
let parser = ColsParser::new("ts(2) level *msg".to_string(), None);
let result = parser
.parse("2025-09-22 12:33:44 INFO hello world")
.unwrap();
assert_eq!(
result
.fields
.get("ts")
.unwrap()
.clone()
.into_string()
.unwrap(),
"2025-09-22 12:33:44"
);
assert_eq!(
result
.fields
.get("level")
.unwrap()
.clone()
.into_string()
.unwrap(),
"INFO"
);
assert_eq!(
result
.fields
.get("msg")
.unwrap()
.clone()
.into_string()
.unwrap(),
"hello world"
);
}
#[test]
fn test_cols_parser_with_separator() {
let parser = ColsParser::new("ts(2) level *msg".to_string(), Some("|".to_string()));
let result = parser
.parse("2025-09-22|12:33:44|INFO|hello|world")
.unwrap();
assert_eq!(
result
.fields
.get("ts")
.unwrap()
.clone()
.into_string()
.unwrap(),
"2025-09-22|12:33:44"
);
assert_eq!(
result
.fields
.get("level")
.unwrap()
.clone()
.into_string()
.unwrap(),
"INFO"
);
assert_eq!(
result
.fields
.get("msg")
.unwrap()
.clone()
.into_string()
.unwrap(),
"hello|world"
);
}
#[test]
fn test_cols_parser_shortage() {
let parser = ColsParser::new("ts level user action".to_string(), None);
let result = parser.parse("2025-09-22 INFO alice").unwrap();
assert_eq!(
result
.fields
.get("ts")
.unwrap()
.clone()
.into_string()
.unwrap(),
"2025-09-22"
);
assert_eq!(
result
.fields
.get("level")
.unwrap()
.clone()
.into_string()
.unwrap(),
"INFO"
);
assert_eq!(
result
.fields
.get("user")
.unwrap()
.clone()
.into_string()
.unwrap(),
"alice"
);
assert!(result.fields.get("action").unwrap().is_unit()); // Unit type for missing fields
}
}