use crate::defs::{NodeIdx, XmlIdx};
use crate::node_type::NodeType;
#[cfg(not(feature = "forward_only"))]
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct NodeInfo {
pub(crate) parent_idx: NodeIdx, prev_sibling: NodeIdx, next_sibling: NodeIdx, first_child: NodeIdx, node_type: NodeType,
}
#[cfg(feature = "forward_only")]
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct NodeInfo {
next_sibling: NodeIdx, first_child: NodeIdx, node_type: NodeType,
}
impl NodeInfo {
#[cfg(not(feature = "forward_only"))]
#[inline]
pub(crate) fn new(node_idx: NodeIdx, parent_idx: NodeIdx, node_type: NodeType) -> Self {
NodeInfo {
parent_idx,
next_sibling: 0,
prev_sibling: node_idx, first_child: 0,
node_type,
}
}
#[cfg(feature = "forward_only")]
#[inline]
pub(crate) fn new(node_type: NodeType) -> Self {
NodeInfo {
next_sibling: 0,
first_child: 0,
node_type,
}
}
#[inline]
#[must_use]
pub fn is_element(&self) -> bool {
matches!(self.node_type, NodeType::Element { .. })
}
#[cfg(not(feature = "forward_only"))]
#[inline]
#[must_use]
pub fn parent_idx(&self) -> Option<NodeIdx> {
if self.parent_idx == 0 {
None } else {
Some(self.parent_idx)
}
}
#[cfg(not(feature = "forward_only"))]
#[inline]
#[must_use]
pub fn prev_sibling_idx(&self) -> NodeIdx {
self.prev_sibling
}
#[inline]
#[must_use]
pub fn next_sibling_idx(&self) -> NodeIdx {
self.next_sibling
}
#[inline]
#[must_use]
pub fn first_child_idx(&self) -> NodeIdx {
self.first_child
}
#[inline]
#[must_use]
pub fn position(&self) -> XmlIdx {
#[cfg(feature = "use_cstr")]
{
match &self.node_type {
NodeType::Element { name, .. } => *name,
NodeType::Text(location) => *location,
NodeType::Head => 0,
}
}
#[cfg(not(feature = "use_cstr"))]
match &self.node_type {
NodeType::Element { name, .. } => name.start,
NodeType::Text(location) => location.start,
NodeType::Head => 0,
}
}
#[inline]
#[must_use]
pub fn node_type(&self) -> &NodeType {
&self.node_type
}
#[inline]
pub fn set_next_sibling_idx(&mut self, idx: NodeIdx) {
self.next_sibling = idx;
}
#[cfg(not(feature = "forward_only"))]
#[inline]
pub fn set_prev_sibling_idx(&mut self, idx: NodeIdx) {
self.prev_sibling = idx;
}
#[inline]
pub fn set_first_child_idx(&mut self, idx: NodeIdx) {
self.first_child = idx;
}
#[cfg(not(feature = "forward_only"))]
#[inline]
pub fn set_parent_idx(&mut self, idx: NodeIdx) {
self.parent_idx = idx;
}
#[inline]
pub fn set_node_type(&mut self, node_type: NodeType) {
self.node_type = node_type;
}
}