use regex::Regex;
pub fn contains<T: PartialEq>(items: &[T], value: &T) -> bool {
items.iter().any(|item| item == value)
}
pub fn matches(value: &str, pattern: &str) -> bool {
Regex::new(pattern)
.map(|re| re.is_match(value))
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn contains_finds_element() {
assert!(contains(&[1, 2, 3], &2));
assert!(!contains(&[1, 2, 3], &4));
assert!(!contains::<i32>(&[], &1));
}
#[test]
fn matches_re2() {
assert!(matches("ORD-12345678", r"^ORD-[0-9]{8}$"));
assert!(!matches("order-12345678", r"^ORD-[0-9]{8}$"));
}
#[test]
fn invalid_pattern_is_false() {
assert!(!matches("anything", "[unclosed"));
}
}