refactor: move regex commands into dedicated functions

This commit is contained in:
aria 2025-07-11 21:55:01 +10:00
parent f04a0379aa
commit 1292117e58
Signed by: aria
SSH key fingerprint: SHA256:WqtcVnDMrv1lnUlNah5k31iywFUI/DV+5yHzCTO4Vds

View file

@ -47,10 +47,6 @@ enum OutputFormat {
Txt, Txt,
} }
/// follow up by extracting just the user and the message
static EXTRACT_MSG: Lazy<Regex> = Lazy::new(|| Regex::new(r"<.*> .*").unwrap());
static EXTRACT_TIME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\[.*?\]").unwrap());
fn main() -> Result<()> { fn main() -> Result<()> {
let args = Args::parse(); let args = Args::parse();
@ -71,18 +67,8 @@ fn main() -> Result<()> {
.map(|x| { .map(|x| {
format!( format!(
"{} {}", "{} {}",
EXTRACT_TIME extract_date_time(&x.to_string()).get(0).unwrap(),
.captures(x) extract_message(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()
) )
}) })
.collect(); .collect();
@ -202,33 +188,50 @@ fn save_csv_file(
} }
for msg in selected { for msg in selected {
let time: Vec<&str> = EXTRACT_TIME let mut time: Vec<&str> = extract_date_time(&msg);
.captures(&msg)
.expect("Unable to extract time") // 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
.get(0) if time.len() == 1 {
.unwrap() time.insert(0, "Null");
.as_str() }
.strip_prefix("[")
.expect("Unable to remove time prefix") out_file.write_record([time[0], time[1], &extract_message(&msg)])?;
.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(),
])?;
} }
Ok(()) Ok(())
} }
fn extract_message(msg: &str) -> String {
/// follow up by extracting just the user and the message
static EXTRACT_MSG: Lazy<Regex> = 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<Regex> = 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 { fn is_possible_chat_msg(input: &str) -> bool {
/// first pass to find all possible lines that could have a chat message /// first pass to find all possible lines that could have a chat message
static SERVER_MSG_RE: Lazy<Regex> = Lazy::new(|| { static SERVER_MSG_RE: Lazy<Regex> = 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 // This is here just in case I need to get a chat message from a strange place
static _CLIENT_CATCHALL_MSG_RE: Lazy<Regex> = Lazy::new(|| { static _CLIENT_CATCHALL_MSG_RE: Lazy<Regex> =
Regex::new(r".*?: \[CHAT\] .*") Lazy::new(|| Regex::new(r".*?: \[CHAT\] .*").unwrap());
.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)
} }