use crate::{
node::{
Node,
Content,
}
};
use std::{
fmt::Debug,
ops::Deref
};
#[derive(Debug, Clone)]
pub struct NodeList<T: Debug + Clone>(pub Option<Node<T>>);
impl<'a, T: Debug + Clone> Deref for NodeList<T> {
type Target = Node<T>;
fn deref(&self) -> &Node<T> {
if let Some(ref node) = self.0 {
return node;
} panic!("Can't deref NodeList(None) to Node<T>. Tried dereferencing to T on Option<T>::None.");
}
}
impl<T: Debug + Clone> NodeList<T> {
pub fn new(node: Node<T>) -> Self {
let content = Box::new(node.get().content.clone());
node.get_mut().content = Content::List(content);
Self(Some(node))
}
}
#[macro_export]
macro_rules! list {
($($node: expr),*) => {
{
let mut children: Vec<hedel_rs::Node<_>> = Vec::new();
let mut lists: Vec<usize> = Vec::new();
let mut c = 0;
$(
let n: hedel_rs::Node::<_> = $node.into();
if let hedel_rs::Content::List(_) = n.get().content {
lists.push(c as usize);
}
children.push(n);
c += 1;
)*
if children.len() > 0 {
c = 0;
for ref child in &children {
let mut borrow = child.get_mut();
if c != children.len() - 1 {
borrow.next = Some(children[c + 1].clone());
}
if c != 0 {
borrow.prev = Some(children[c - 1].downgrade());
}
borrow.parent = None;
c += 1;
}
}
for idx in lists.into_iter() {
let first = children[idx].clone();
if idx > 0 {
if let Some(prev) = children.get(idx - 1) {
prev.get_mut().next = Some(first.clone());
first.get_mut().prev = Some(prev.downgrade());
}
}
if let Some(last) = first.get_last_sibling() {
if let Some(next) = children.get(idx + 1) {
next.get_mut().prev = Some(last.downgrade());
last.get_mut().next = Some(next.clone());
}
}
}
hedel_rs::NodeList::new(children[0].clone())
}
}
}