Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/communication/handlers/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl ParserHandler {
}

/// Parse a message with regex logic
fn regex_handle(&self, message: &str, index: usize, pattern: Regex) -> Option<String> {
fn regex_handle(&self, message: &str, index: usize, pattern: &Regex) -> Option<String> {
// We add 1 here because the zeroth index of a Capture is the original message
match pattern.captures(message) {
Some(caps) => caps
Expand Down
31 changes: 20 additions & 11 deletions src/extensions/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
cell::OnceCell,
collections::HashMap,
error::Error,
fs::{create_dir_all, read_dir, read_to_string, remove_file, write},
Expand Down Expand Up @@ -41,6 +42,8 @@ pub struct Parser {
pub aggregation_methods: HashMap<String, AggregationMethod>,
#[serde(skip_serializing, skip_deserializing)]
pub aggregator_map: HashMap<String, Box<dyn Aggregator>>,
#[serde(skip_serializing, skip_deserializing)]
cached_regex: OnceCell<Regex>,
}

impl ExtensionMethods for Parser {
Expand Down Expand Up @@ -136,6 +139,7 @@ impl Parser {
order,
aggregation_methods,
aggregator_map: HashMap::new(),
cached_regex: OnceCell::new(),
}
}

Expand Down Expand Up @@ -200,14 +204,19 @@ impl Parser {
}
}

pub fn get_regex(&self) -> Result<Regex, LogriaError> {
if self.pattern_type == PatternType::Regex {
match Regex::new(&self.pattern) {
Ok(pattern) => Ok(pattern),
Err(why) => Err(LogriaError::InvalidRegex(why, self.pattern.clone())),
pub fn get_regex(&self) -> Result<&Regex, LogriaError> {
if self.pattern_type != PatternType::Regex {
return Err(LogriaError::WrongParserType);
}
if let Some(regex) = self.cached_regex.get() {
return Ok(regex);
}
match Regex::new(&self.pattern) {
Ok(pattern) => {
let _ = self.cached_regex.set(pattern);
Ok(self.cached_regex.get().unwrap())
}
} else {
Err(LogriaError::WrongParserType)
Err(why) => Err(LogriaError::InvalidRegex(why, self.pattern.clone())),
}
}

Expand Down Expand Up @@ -466,8 +475,8 @@ mod parse_tests {
parser.save("Common Log Format Test 2").unwrap();

let file_name = format!("{}/{}", patterns(), "Common Log Format Test 2");
let read_parser = Parser::load(&file_name);
let regex = read_parser.unwrap().get_regex();
let read_parser = Parser::load(&file_name).unwrap();
let regex = read_parser.get_regex();
assert!(regex.is_ok());
}

Expand Down Expand Up @@ -496,8 +505,8 @@ mod parse_tests {
parser.save("Hyphen Separated Test 1").unwrap();

let file_name = format!("{}/{}", patterns(), "Hyphen Separated Test 1");
let parser = Parser::load(&file_name);
let regex = parser.unwrap().get_regex();
let parser = Parser::load(&file_name).unwrap();
let regex = parser.get_regex();
assert!(regex.is_err());
}

Expand Down