use lazy_static::lazy_static;
use regex::Regex;
lazy_static! {
pub static ref QUEUE_ID_REGEX: Regex = Regex::new(r"^([0-9A-Za-z]{10,20}):").unwrap();
pub static ref QUEUE_ID_PATTERN: &'static str = r"([0-9A-Za-z]{10,20})";
}
pub fn extract_queue_id(message: &str) -> Option<String> {
QUEUE_ID_REGEX
.captures(message)
.and_then(|captures| captures.get(1))
.map(|m| m.as_str().to_string())
}
pub fn create_queue_id_pattern(pattern: &str) -> String {
pattern.replace("{QUEUE_ID}", *QUEUE_ID_PATTERN)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_queue_id_new_format() {
let message = "4bG4VR5zTgz6pqsd: client=localhost[127.0.0.1]:60166";
let result = extract_queue_id(message);
assert_eq!(result, Some("4bG4VR5zTgz6pqsd".to_string()));
}
#[test]
fn test_extract_queue_id_various_formats() {
let test_cases = vec![
(
"4bG5rK2jJfz6pr1t: message-id=<test@example.com>",
Some("4bG5rK2jJfz6pr1t".to_string()),
),
(
"4bG6s970Nhz6qsfY: to=<user@example.com>",
Some("4bG6s970Nhz6qsfY".to_string()),
),
(
"4bG6sC3LJbz6qsfc: from=<sender@example.com>",
Some("4bG6sC3LJbz6qsfc".to_string()),
),
("no queue id in this message", None),
("ABC123: too short", None),
("4bG5rK2jJfz6pr1tTooLong: too long", None),
];
for (input, expected) in test_cases {
assert_eq!(
extract_queue_id(input),
expected,
"Failed for input: {}",
input
);
}
}
#[test]
fn test_create_queue_id_pattern() {
let pattern = create_queue_id_pattern(r"^{QUEUE_ID}: to=<([^>]+)>");
assert_eq!(pattern, r"^([0-9A-Za-z]{10,20}): to=<([^>]+)>");
}
}