use std::rc::Rc;
use crate::parser::{
CustomParseError,
tree::{ParseContext, ParseTree},
};
#[derive(Debug, Clone)]
pub enum Child<C, E>
where
C: ParseContext,
E: Into<CustomParseError> + Clone,
{
Contains(ParseTree<C, E>),
ByRef(Rc<ParseTree<C, E>>),
}
impl<C, E> Child<C, E>
where
C: ParseContext,
E: Into<CustomParseError> + Clone,
{
pub fn as_tree(&self) -> &ParseTree<C, E> {
match self {
Self::Contains(tree) => tree,
Self::ByRef(ptr) => ptr,
}
}
pub fn as_tree_mut(&mut self) -> Option<&mut ParseTree<C, E>> {
match self {
Self::Contains(tree) => Some(tree),
Self::ByRef(_) => None,
}
}
pub fn into_owned(self) -> Result<ParseTree<C, E>, Self> {
match self {
Self::Contains(tree) => Ok(tree),
other => Err(other),
}
}
pub(crate) fn into_ref(self) -> Self {
match self {
Child::Contains(tree) => Child::ByRef(Rc::new(tree)),
child => child,
}
}
}
impl<C, E> From<ParseTree<C, E>> for Child<C, E>
where
C: ParseContext,
E: Into<CustomParseError> + Clone,
{
fn from(tree: ParseTree<C, E>) -> Self {
Child::Contains(tree)
}
}
impl<C, E> From<Rc<ParseTree<C, E>>> for Child<C, E>
where
C: ParseContext,
E: Into<CustomParseError> + Clone,
{
fn from(tree: Rc<ParseTree<C, E>>) -> Self {
Child::ByRef(tree)
}
}
#[macro_export]
macro_rules! empty_children {
() => {
::std::iter::empty::<$crate::parser::tree::Child<_, _>>()
};
}
pub use empty_children;