use core::fmt::Debug;
use core::marker::PhantomData;
use crate::ctor::Ctor;
use crate::ctor::Handler;
use crate::ctx::Match;
use crate::err::Error;
use crate::regex::Regex;
use crate::regex::impl_not_for_regex;
use crate::span::Span;
pub struct Pattern<C, P> {
pat: P,
marker: PhantomData<C>,
}
impl_not_for_regex!(Pattern<C, P>);
impl<C, P> Debug for Pattern<C, P>
where
P: Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Pattern").field("pat", &self.pat).finish()
}
}
impl<C, P> Default for Pattern<C, P>
where
P: Default,
{
fn default() -> Self {
Self {
pat: Default::default(),
marker: Default::default(),
}
}
}
impl<C, P> Clone for Pattern<C, P>
where
P: Clone,
{
fn clone(&self) -> Self {
Self {
pat: self.pat.clone(),
marker: self.marker,
}
}
}
impl<C, P> Copy for Pattern<C, P> where P: Copy {}
impl<C, P> Pattern<C, P> {
pub const fn new(pat: P) -> Self {
Self {
pat,
marker: PhantomData,
}
}
pub const fn pat(&self) -> &P {
&self.pat
}
pub const fn pat_mut(&mut self) -> &mut P {
&mut self.pat
}
pub fn set_pat(&mut self, pat: P) -> &mut Self {
self.pat = pat;
self
}
}
impl<'a, C, O, P, H> Ctor<C, O, H> for Pattern<C, P>
where
P: Regex<C>,
C: Match<'a>,
H: Handler<C, Out = O>,
{
#[inline(always)]
fn construct(&self, ctx: &mut C, func: &mut H) -> Result<O, Error> {
let ret = ctx.try_mat(&self.pat)?;
func.invoke(ctx, &ret).map_err(Into::into)
}
}
impl<'a, C, P> Regex<C> for Pattern<C, P>
where
P: Regex<C>,
C: Match<'a>,
{
#[inline(always)]
fn try_parse(&self, ctx: &mut C) -> Result<Span, Error> {
ctx.try_mat(&self.pat)
}
}