Skip to main content

renamer_rs/processor/
selector.rs

1use regex::Regex;
2
3/// Represents a [`Regex`]  that is used to find a single matching segment
4#[derive(Debug, Clone)]
5pub struct Selector {
6    #[allow(unused)]
7    name: Option<String>, // todo: use name/id in format string
8    pattern: Regex,
9}
10
11impl Selector {
12    /// Create a new [`Selector`]
13    pub fn new(name: Option<String>, pattern: Regex) -> Self {
14        Self { name, pattern }
15    }
16
17    /// Returns true when a segment matches the provided pattern
18    pub fn is_match<S: AsRef<str>>(&self, segment: S) -> bool {
19        self.pattern.is_match(segment.as_ref())
20    }
21
22    /// Returns the first segment that matches the provided pattern
23    pub fn match_segment<S: AsRef<str>>(&self, segments: &[S]) -> Option<String> {
24        segments
25            .iter()
26            .position(|s| self.is_match(s))
27            .map(|i| segments[i].as_ref().into())
28    }
29}