use crate::capture::Capture;
pub trait Matcher: Send + Sync {
fn capture<'a>(&'a self, _segment: &str) -> Option<Capture<'a>> {
None
}
fn is_match(&self, segment: &str) -> bool;
}
impl<F> Matcher for F
where
F: Fn(&str) -> bool + Send + Sync,
{
fn is_match(&self, segment: &str) -> bool {
self(segment)
}
}
pub struct StaticMatcher {
inner: String,
}
impl StaticMatcher {
pub fn new<S: Into<String>>(s: S) -> Self {
Self { inner: s.into() }
}
}
impl Matcher for StaticMatcher {
fn is_match(&self, segment: &str) -> bool {
self.inner == segment
}
}
pub struct DynamicMatcher {
inner: String,
}
impl DynamicMatcher {
pub fn new<S: Into<String>>(s: S) -> Self {
Self { inner: s.into() }
}
}
impl Matcher for DynamicMatcher {
fn capture<'a>(&'a self, segment: &str) -> Option<Capture<'a>> {
Some((&self.inner, (0, segment.len())))
}
fn is_match(&self, _segment: &str) -> bool {
true
}
}