use crate::public::failures::ErrorCases;
use id_tree::{NodeId, Tree};
use self::NodeType::{Atom, Molecule, MoleculeGroup, ParenthesisWrapper};
use super::{super::atomdict::AtomDict, treebuilder::F};
use crate::public::traits::CheckedType;
pub enum NodeType<T: CheckedType> {
Atom(String, T), ParenthesisWrapper(T), Molecule(T, T), MoleculeGroup, }
pub struct ASTNode<T: CheckedType> {
nodetype: NodeType<T>,
}
impl<T: CheckedType> ASTNode<T> {
pub fn new(nodetype: NodeType<T>) -> Self {
Self { nodetype }
}
pub fn to_atomdict(
&self,
node_id: &NodeId,
tree: &Tree<ASTNode<T>>,
) -> Result<AtomDict<T>, ErrorCases> {
match &self.nodetype {
Atom(s, o) => {
let mut a = AtomDict::<T>::new();
a.insert(s.to_string(), *o);
Ok(a)
}
Molecule(o, c) => {
let mut a = AtomDict::<T>::new();
a.insert("e".to_string(), *c);
Ok(tree.children_ids(node_id).map_err(F)?.fold(a, |b, c| {
b + tree.get(c).unwrap().data().to_atomdict(c, tree).unwrap()
}) * *o)
}
ParenthesisWrapper(o) => {
let a = AtomDict::<T>::new();
Ok(tree.children_ids(node_id).map_err(F)?.fold(a, |b, c| {
b + tree.get(c).unwrap().data().to_atomdict(c, tree).unwrap()
}) * *o)
}
MoleculeGroup => {
let a = AtomDict::<T>::new();
Ok(tree.children_ids(node_id).map_err(F)?.fold(a, |b, c| {
b + tree.get(c).unwrap().data().to_atomdict(c, tree).unwrap()
}))
}
}
}
}