use crate::node::{HierarchyError, HotNode};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AdoptAs {
FirstChild,
LastChild,
PreviousSibling,
NextSibling,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(clippy::enum_variant_names)]
pub enum InsertAs<T> {
FirstChildOf(T),
LastChildOf(T),
PreviousSiblingOf(T),
NextSiblingOf(T),
}
impl<T> InsertAs<T> {
pub fn map<U, F>(self, f: F) -> InsertAs<U>
where
F: FnOnce(T) -> U,
{
match self {
Self::FirstChildOf(v) => InsertAs::FirstChildOf(f(v)),
Self::LastChildOf(v) => InsertAs::LastChildOf(f(v)),
Self::PreviousSiblingOf(v) => InsertAs::PreviousSiblingOf(f(v)),
Self::NextSiblingOf(v) => InsertAs::NextSiblingOf(f(v)),
}
}
#[inline]
pub(super) fn anchor(&self) -> &T {
match self {
Self::FirstChildOf(v)
| Self::LastChildOf(v)
| Self::PreviousSiblingOf(v)
| Self::NextSiblingOf(v) => v,
}
}
#[inline]
#[must_use]
pub(super) fn to_adopt_as(&self) -> AdoptAs {
match self {
Self::FirstChildOf(_) => AdoptAs::FirstChild,
Self::LastChildOf(_) => AdoptAs::LastChild,
Self::PreviousSiblingOf(_) => AdoptAs::PreviousSibling,
Self::NextSiblingOf(_) => AdoptAs::NextSibling,
}
}
#[inline]
#[must_use]
pub fn as_ref(&self) -> InsertAs<&T> {
match self {
Self::FirstChildOf(v) => InsertAs::FirstChildOf(v),
Self::LastChildOf(v) => InsertAs::LastChildOf(v),
Self::PreviousSiblingOf(v) => InsertAs::PreviousSiblingOf(v),
Self::NextSiblingOf(v) => InsertAs::NextSiblingOf(v),
}
}
}
impl<T> InsertAs<&HotNode<T>> {
#[inline]
pub(super) fn try_create_node(&self, data: T) -> Result<HotNode<T>, HierarchyError> {
self.anchor().try_create_node_as(data, self.to_adopt_as())
}
}