From 1292117e58e7906f549dd4c1282e0ada23ddfa23 Mon Sep 17 00:00:00 2001 From: aria Date: Fri, 11 Jul 2025 21:55:01 +1000 Subject: [PATCH] refactor: move regex commands into dedicated functions --- src/main.rs | 90 +++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/src/main.rs b/src/main.rs index 16f5e30..0f53f3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,10 +47,6 @@ enum OutputFormat { Txt, } -/// follow up by extracting just the user and the message -static EXTRACT_MSG: Lazy = Lazy::new(|| Regex::new(r"<.*> .*").unwrap()); -static EXTRACT_TIME: Lazy = Lazy::new(|| Regex::new(r"^\[.*?\]").unwrap()); - fn main() -> Result<()> { let args = Args::parse(); @@ -71,18 +67,8 @@ fn main() -> Result<()> { .map(|x| { format!( "{} {}", - EXTRACT_TIME - .captures(x) - .expect("Unable to extract time") - .get(0) - .unwrap() - .as_str(), - EXTRACT_MSG - .captures(x) - .expect("Unable to extract message") - .get(0) - .unwrap() - .as_str() + extract_date_time(&x.to_string()).get(0).unwrap(), + extract_message(x) ) }) .collect(); @@ -202,33 +188,50 @@ fn save_csv_file( } for msg in selected { - let time: Vec<&str> = EXTRACT_TIME - .captures(&msg) - .expect("Unable to extract time") - .get(0) - .unwrap() - .as_str() - .strip_prefix("[") - .expect("Unable to remove time prefix") - .strip_suffix("]") - .expect("Unable to remove time suffix") - .split(' ') - .collect(); - out_file.write_record([ - time[0], - time[1], - EXTRACT_MSG - .captures(&msg) - .expect("Unable to extract time") - .get(0) - .unwrap() - .as_str(), - ])?; + let mut time: Vec<&str> = extract_date_time(&msg); + + // if there is only 1 entry in the vec then it has to be a client time so we can just add a pointless entry for the csv + if time.len() == 1 { + time.insert(0, "Null"); + } + + out_file.write_record([time[0], time[1], &extract_message(&msg)])?; } Ok(()) } +fn extract_message(msg: &str) -> String { + /// follow up by extracting just the user and the message + static EXTRACT_MSG: Lazy = Lazy::new(|| Regex::new(r"<.*> .*").unwrap()); + + EXTRACT_MSG + .captures(&msg) + .expect("Unable to extract time") + .get(0) + .unwrap() + .as_str() + .to_string() +} + +fn extract_date_time(msg: &String) -> Vec<&str> { + static EXTRACT_TIME: Lazy = Lazy::new(|| Regex::new(r"^\[.*?\]").unwrap()); + + let time: Vec<&str> = EXTRACT_TIME + .captures(msg) + .expect("Unable to extract time") + .get(0) + .unwrap() + .as_str() + .strip_prefix("[") + .expect("Unable to remove time prefix") + .strip_suffix("]") + .expect("Unable to remove time suffix") + .split(' ') + .collect(); + time +} + fn is_possible_chat_msg(input: &str) -> bool { /// first pass to find all possible lines that could have a chat message static SERVER_MSG_RE: Lazy = Lazy::new(|| { @@ -247,11 +250,10 @@ fn is_possible_chat_msg(input: &str) -> bool { }); // This is here just in case I need to get a chat message from a strange place - static _CLIENT_CATCHALL_MSG_RE: Lazy = Lazy::new(|| { - Regex::new(r".*?: \[CHAT\] .*") - .unwrap() - }); - + static _CLIENT_CATCHALL_MSG_RE: Lazy = + Lazy::new(|| Regex::new(r".*?: \[CHAT\] .*").unwrap()); - SERVER_MSG_RE.is_match(input) || CLIENT_PRISM_MSG_RE.is_match(input) || CLIENT_LOG_MSG_RE.is_match(input) + SERVER_MSG_RE.is_match(input) +|| CLIENT_PRISM_MSG_RE.is_match(input) +|| CLIENT_LOG_MSG_RE.is_match(input) }