use lazy_static::lazy_static;
use regex::Regex;
pub fn process_remark(remark: &mut String, remarks_list: &Vec<String>, proc_comma: bool) {
*remark = remark.replace('=', "-");
if proc_comma {
if remark.contains(',') {
*remark = format!("\"{}\"", remark);
}
}
let mut temp_remark = remark.clone();
let mut cnt = 2;
while remarks_list.contains(&temp_remark) {
temp_remark = format!("{} {}", remark, cnt);
cnt += 1;
}
*remark = temp_remark;
}
pub fn process_filters(remark: &mut String, remarks_list: &Vec<String>) {
lazy_static! {
static ref SCRIPT_REGEX: Regex = Regex::new(r"\s*\[([^\]]*?)\]$").unwrap();
}
for item in remarks_list {
if item.starts_with("filter ") || item.starts_with("aerr ") {
let left = item.find(' ').map(|pos| pos + 1).unwrap_or(0);
if left >= item.len() {
continue;
}
let arguments = &item[left..];
if arguments.starts_with("script:") {
let _script_arg = &arguments[7..];
if let Some(captures) = SCRIPT_REGEX.captures(remark) {
if let Some(_) = captures.get(1) {
*remark = remark[..remark.len() - captures.get(0).unwrap().as_str().len()]
.trim()
.to_string();
}
}
} else if arguments.starts_with("regex:") {
let regex_arg = &arguments[6..];
if let Ok(re) = Regex::new(regex_arg) {
if re.is_match(remark) {
if item.starts_with("filter") {
*remark = re.replace_all(remark, "").to_string();
} else {
}
}
}
} else {
*remark = remark.replace(arguments, "");
}
}
}
lazy_static! {
static ref MULTI_SPACE_REGEX: Regex = Regex::new(r"\s+").unwrap();
}
*remark = MULTI_SPACE_REGEX.replace_all(remark, " ").to_string();
*remark = remark.trim().to_string();
}