use regex::{Regex, Error};
use crate::enums::WordBounds;
pub fn build_regex(pattern: &str, case_insensitive: bool) -> Result<Regex, Error> {
let mut parts: Vec<&str> = vec![];
if case_insensitive && pattern.starts_with("(?") == false {
parts.push("(?i)");
}
parts.push(pattern);
let regex_str = parts. concat();
Regex::new(®ex_str)
}
pub(crate) fn build_word_pattern(word: &str, bounds: WordBounds) -> String {
bounds.to_pattern(word)
}
pub(crate) fn build_whole_word_pattern(word: &str) -> String {
build_word_pattern(word, WordBounds::Both)
}
pub(crate) fn build_optional_whole_word_pattern(words: &[&str]) -> String {
let word_pattern = ["(", &words.join("|"), ")"].concat();
build_word_pattern(&word_pattern, WordBounds::Both)
}
pub(crate) fn strs_to_str_bool_pairs<'a>(strs: &'a [&str], bool_val: bool) -> Vec<(&'a str, bool)> {
strs.into_iter().map(|s| (*s, bool_val)).collect()
}