use core::fmt::Debug;
use core::marker::PhantomData;
use crate::ctor::Ctor;
use crate::ctor::Handler;
use crate::ctx::CtxGuard;
use crate::ctx::Match;
use crate::debug_ctor_beg;
use crate::debug_ctor_reval;
use crate::debug_ctor_stage;
use crate::debug_regex_beg;
use crate::debug_regex_reval;
use crate::debug_regex_stage;
use crate::err::Error;
use crate::regex::Regex;
use crate::regex::impl_not_for_regex;
use crate::span::Span;
pub struct Suffix<C, P, T> {
pat: P,
suffix: T,
marker: PhantomData<C>,
}
impl_not_for_regex!(Suffix<C, P, T>);
impl<C, P, T> Debug for Suffix<C, P, T>
where
P: Debug,
T: Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("PadUnit")
.field("pat", &self.pat)
.field("suffix", &self.suffix)
.finish()
}
}
impl<C, P, T> Default for Suffix<C, P, T>
where
P: Default,
T: Default,
{
fn default() -> Self {
Self {
pat: Default::default(),
suffix: Default::default(),
marker: Default::default(),
}
}
}
impl<C, P, T> Clone for Suffix<C, P, T>
where
P: Clone,
T: Clone,
{
fn clone(&self) -> Self {
Self {
pat: self.pat.clone(),
suffix: self.suffix.clone(),
marker: self.marker,
}
}
}
impl<C, P, T> Copy for Suffix<C, P, T>
where
P: Copy,
T: Copy,
{
}
impl<C, P, T> Suffix<C, P, T> {
pub const fn new(pat: P, suffix: T) -> Self {
Self {
pat,
suffix,
marker: PhantomData,
}
}
pub const fn pat(&self) -> &P {
&self.pat
}
pub const fn pat_mut(&mut self) -> &mut P {
&mut self.pat
}
pub const fn suffix(&self) -> &T {
&self.suffix
}
pub const fn suffix_mut(&mut self) -> &mut T {
&mut self.suffix
}
pub fn set_pat(&mut self, pat: P) -> &mut Self {
self.pat = pat;
self
}
pub fn set_suffix(&mut self, suffix: T) -> &mut Self {
self.suffix = suffix;
self
}
}
impl<'a, C, P, T, O, H> Ctor<C, O, H> for Suffix<C, P, T>
where
T: Regex<C>,
P: Ctor<C, O, H>,
C: Match<'a>,
H: Handler<C>,
{
#[inline(always)]
fn construct(&self, ctx: &mut C, func: &mut H) -> Result<O, Error> {
let mut ctx = CtxGuard::new(ctx);
debug_ctor_beg!("Suffix", ctx.beg());
let ret = debug_ctor_stage!("Suffix", "pat", self.pat.construct(ctx.ctx(), func));
if ret.is_ok() {
let _ = debug_ctor_stage!("Suffix", "suffix", ctx.try_mat(&self.suffix)?);
}
debug_ctor_reval!("Suffix", ctx.beg(), ctx.end(), ret.is_ok());
ctx.process_ret(ret)
}
}
impl<'a, C, P, T> Regex<C> for Suffix<C, P, T>
where
T: Regex<C>,
P: Regex<C>,
C: Match<'a>,
{
#[inline(always)]
fn try_parse(&self, ctx: &mut C) -> Result<Span, Error> {
let mut ctx = CtxGuard::new(ctx);
debug_regex_beg!("Suffix", ctx.beg());
let mut ret = debug_regex_stage!("Suffix", "pat", ctx.try_mat(&self.pat)?);
ret.add_assign(debug_regex_stage!(
"Suffix",
"suffix",
ctx.try_mat(&self.suffix)?
));
debug_regex_reval!("Suffix", Ok(ret))
}
}
pub struct Prefix<C, P, T> {
pat: P,
prefix: T,
marker: PhantomData<C>,
}
impl_not_for_regex!(Prefix<C, P, T>);
impl<C, P, T> Debug for Prefix<C, P, T>
where
P: Debug,
T: Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("PaddedUnit")
.field("pat", &self.pat)
.field("prefix", &self.prefix)
.finish()
}
}
impl<C, P, T> Default for Prefix<C, P, T>
where
P: Default,
T: Default,
{
fn default() -> Self {
Self {
pat: Default::default(),
prefix: Default::default(),
marker: Default::default(),
}
}
}
impl<C, P, T> Clone for Prefix<C, P, T>
where
P: Clone,
T: Clone,
{
fn clone(&self) -> Self {
Self {
pat: self.pat.clone(),
prefix: self.prefix.clone(),
marker: self.marker,
}
}
}
impl<C, P, T> Copy for Prefix<C, P, T>
where
P: Copy,
T: Copy,
{
}
impl<C, P, T> Prefix<C, P, T> {
pub const fn new(pat: P, prefix: T) -> Self {
Self {
pat,
prefix,
marker: PhantomData,
}
}
pub const fn pat(&self) -> &P {
&self.pat
}
pub const fn pat_mut(&mut self) -> &mut P {
&mut self.pat
}
pub const fn prefix(&self) -> &T {
&self.prefix
}
pub const fn prefix_mut(&mut self) -> &mut T {
&mut self.prefix
}
pub fn set_pat(&mut self, pat: P) -> &mut Self {
self.pat = pat;
self
}
pub fn set_prefix(&mut self, prefix: T) -> &mut Self {
self.prefix = prefix;
self
}
}
impl<'a, C, P, T, O, H> Ctor<C, O, H> for Prefix<C, P, T>
where
T: Regex<C>,
P: Ctor<C, O, H>,
C: Match<'a>,
H: Handler<C>,
{
#[inline(always)]
fn construct(&self, ctx: &mut C, func: &mut H) -> Result<O, Error> {
let mut ctx = CtxGuard::new(ctx);
debug_ctor_beg!("Prefix", ctx.beg());
let _ = debug_ctor_stage!("Prefix", "head", ctx.try_mat(&self.prefix)?);
let r = debug_ctor_stage!("Prefix", "prefix", self.pat.construct(ctx.ctx(), func));
debug_ctor_reval!("Prefix", ctx.beg(), ctx.end(), r.is_ok());
ctx.process_ret(r)
}
}
impl<'a, C, P, T> Regex<C> for Prefix<C, P, T>
where
T: Regex<C>,
P: Regex<C>,
C: Match<'a>,
{
#[inline(always)]
fn try_parse(&self, ctx: &mut C) -> Result<Span, Error> {
let mut ctx = CtxGuard::new(ctx);
debug_regex_beg!("Prefix", ctx.beg());
let mut ret = debug_regex_stage!("Prefix", "head", ctx.try_mat(&self.prefix)?);
ret.add_assign(debug_regex_stage!(
"Prefix",
"prefix",
ctx.try_mat(&self.pat)?
));
debug_regex_reval!("Prefix", Ok(ret))
}
}