use super::matcher::Matcher;
pub struct Node<T> {
value: Option<T>,
matcher: Box<dyn Matcher>,
children: Vec<Node<T>>,
}
impl<T> Node<T> {
pub(crate) fn new(matcher: Box<dyn Matcher>) -> Self {
Self {
matcher,
value: None,
children: Vec::new(),
}
}
pub(crate) fn add_child(&mut self, child: Node<T>) {
self.children.reserve_exact(1);
self.children.push(child);
}
pub(crate) fn children(&self) -> &[Node<T>] {
&self.children
}
pub(crate) fn children_mut(&mut self) -> &mut [Node<T>] {
&mut self.children
}
pub(crate) fn matcher(&self) -> &dyn Matcher {
&*self.matcher
}
pub(crate) fn update<F>(&mut self, f: F)
where
F: FnOnce(Option<T>) -> T,
{
let t = f(self.value.take());
self.value.replace(t);
}
pub(crate) fn value(&self) -> Option<&T> {
self.value.as_ref()
}
}