use crate::*;
use regex::Regex;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Pattern<const PATTERN: &'static str> {
matched_string: String
}
impl<const PATTERN: &'static str> Pattern<PATTERN> {
pub fn pattern() -> &'static str { PATTERN }
pub fn matched_string(&self) -> String { self.matched_string.clone() }
}
impl<const PATTERN: &'static str> Parse for Pattern<PATTERN> {
fn parse(position: &mut Position) -> Result<Self> {
let error = Error::unexpected::<Self>(position.clone());
let regex_maybe = Regex::new(PATTERN);
if regex_maybe.is_err() { return Err(error); }
let regex = regex_maybe.unwrap();
let remainder = &position.source()[position.index()..];
let regex_match_maybe = regex.find(remainder);
if regex_match_maybe.is_none() { return Err(error); }
let regex_match = regex_match_maybe.unwrap();
if regex_match.start() != 0 { return Err(error); }
let matched_string = regex_match.as_str().to_string();
for _ in 0..matched_string.len() {
if position.clone().next().is_none() {
return Err(Error::unexpected_end::<Self>(position.clone()))
}
else { let _ = position.next(); }
}
Ok(Self { matched_string })
}
}