use alloc::sync::Arc;
use core::fmt;
use crate::error::ParseError;
use crate::state::{Lang, ParsingState};
use crate::token::{Token, TokenReader};
use super::Invocation;
pub enum GroupChildState<'p, L: Lang> {
Inherit,
Fixed(Arc<ParsingState<L>>),
#[allow(clippy::type_complexity)]
Compute(
&'p dyn Fn(
&Arc<ParsingState<L>>,
&Token<L>,
&dyn TokenReader<'_, L>,
)
-> Result<Arc<ParsingState<L>>, ParseError<L::SourceOrigin>>,
),
}
pub enum InvocationChildState<'p, L: Lang> {
Inherit,
Fixed(Arc<ParsingState<L>>),
#[allow(clippy::type_complexity)]
Compute(
&'p dyn Fn(
&Arc<ParsingState<L>>,
&Invocation<'_, L>,
)
-> Result<Arc<ParsingState<L>>, ParseError<L::SourceOrigin>>,
),
}
pub struct ChildStateSpec<'p, L: Lang> {
pub group: GroupChildState<'p, L>,
pub invocation: InvocationChildState<'p, L>,
}
impl<'p, L: Lang> ChildStateSpec<'p, L> {
pub fn inherit() -> ChildStateSpec<'p, L> {
ChildStateSpec {
group: GroupChildState::Inherit,
invocation: InvocationChildState::Inherit,
}
}
}
impl<L: Lang> Default for ChildStateSpec<'_, L> {
fn default() -> Self {
ChildStateSpec::inherit()
}
}
impl<L: Lang> Clone for GroupChildState<'_, L> {
fn clone(&self) -> Self {
match self {
GroupChildState::Inherit => GroupChildState::Inherit,
GroupChildState::Fixed(state) => GroupChildState::Fixed(Arc::clone(state)),
GroupChildState::Compute(compute) => GroupChildState::Compute(*compute),
}
}
}
impl<L: Lang> Clone for InvocationChildState<'_, L> {
fn clone(&self) -> Self {
match self {
InvocationChildState::Inherit => InvocationChildState::Inherit,
InvocationChildState::Fixed(state) => InvocationChildState::Fixed(Arc::clone(state)),
InvocationChildState::Compute(compute) => InvocationChildState::Compute(*compute),
}
}
}
impl<L: Lang> Clone for ChildStateSpec<'_, L> {
fn clone(&self) -> Self {
ChildStateSpec { group: self.group.clone(), invocation: self.invocation.clone() }
}
}
impl<L: Lang> fmt::Debug for GroupChildState<'_, L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GroupChildState::Inherit => write!(f, "Inherit"),
GroupChildState::Fixed(_) => write!(f, "Fixed(..)"),
GroupChildState::Compute(_) => write!(f, "Compute(..)"),
}
}
}
impl<L: Lang> fmt::Debug for InvocationChildState<'_, L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InvocationChildState::Inherit => write!(f, "Inherit"),
InvocationChildState::Fixed(_) => write!(f, "Fixed(..)"),
InvocationChildState::Compute(_) => write!(f, "Compute(..)"),
}
}
}
impl<L: Lang> fmt::Debug for ChildStateSpec<'_, L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ChildStateSpec")
.field("group", &self.group)
.field("invocation", &self.invocation)
.finish()
}
}