use crate::matcher::{DynamicMatcher, Matcher, StaticMatcher};
pub trait Parser: Send + Sync {
fn parse(&self, segment: &str) -> Option<Box<dyn Matcher>>;
}
impl<F> Parser for F
where
F: Fn(&str) -> Option<Box<dyn Matcher>> + Send + Sync,
{
fn parse(&self, segment: &str) -> Option<Box<dyn Matcher>> {
self(segment)
}
}
pub struct StaticParser;
impl Parser for StaticParser {
fn parse(&self, segment: &str) -> Option<Box<dyn Matcher>> {
Some(Box::new(StaticMatcher::new(segment)))
}
}
pub struct DynamicParser;
impl Parser for DynamicParser {
fn parse(&self, segment: &str) -> Option<Box<dyn Matcher>> {
if &segment[0..1] != ":" || segment.len() == 1 {
return None;
}
let field = &segment[1..];
let matcher = DynamicMatcher::new(field);
Some(Box::new(matcher))
}
}