use crate::{compile::Flags, regex::Regex, Result};
pub type RegexBuilder = Builder<String>;
pub type RegexSetBuilder = Builder<Vec<String>>;
pub struct Builder<T> {
expr: T,
flags: Flags,
}
impl Builder<String> {
pub fn new<S: Into<String>>(pattern: S) -> Self {
Builder {
expr: pattern.into(),
flags: Flags::empty(),
}
}
pub fn build(&self) -> Result<Regex> {
Regex::with_flags(&self.expr, self.flags)
}
}
impl<T> Builder<T> {
fn toggle(&mut self, flag: Flags, yes: bool) -> &mut Self {
if yes {
self.flags.insert(flag)
} else {
self.flags.remove(flag)
}
self
}
pub fn case_insensitive(&mut self, yes: bool) -> &mut Self {
self.toggle(Flags::CASELESS, yes)
}
pub fn multi_line(&mut self, yes: bool) -> &mut Self {
self.toggle(Flags::MULTILINE, yes)
}
pub fn dot_matches_new_line(&mut self, yes: bool) -> &mut Self {
self.toggle(Flags::DOTALL, yes)
}
pub fn unicode(&mut self, yes: bool) -> &mut Self {
self.toggle(Flags::UCP, yes)
}
}