use std::{
cell::RefCell,
ops::{Index, IndexMut},
rc::Rc,
};
use crate::tree::XmlNodePtr;
use super::XmlParserCtxt;
#[doc(alias = "xmlParserNodeInfoSeq")]
#[repr(C)]
#[derive(Clone, Default)]
pub struct XmlParserNodeInfoSeq {
buffer: Vec<Rc<RefCell<XmlParserNodeInfo>>>,
}
impl XmlParserNodeInfoSeq {
#[doc(alias = "xmlInitNodeInfoSeq", alias = "xmlClearNodeInfoSeq")]
pub(crate) fn clear(&mut self) {
self.buffer.clear();
}
pub(crate) fn insert(&mut self, index: usize, info: Rc<RefCell<XmlParserNodeInfo>>) {
self.buffer.insert(index, info);
}
#[doc(alias = "xmlParserFindNodeInfoIndex")]
pub(crate) fn binary_search(&self, node: Option<XmlNodePtr>) -> Result<usize, usize> {
self.buffer
.binary_search_by_key(&node, |node| node.borrow().node)
}
}
impl Index<usize> for XmlParserNodeInfoSeq {
type Output = Rc<RefCell<XmlParserNodeInfo>>;
fn index(&self, index: usize) -> &Self::Output {
self.buffer
.get(index)
.expect("Index out of bound for XmlParserNodeInfoSeq.")
}
}
impl IndexMut<usize> for XmlParserNodeInfoSeq {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.buffer
.get_mut(index)
.expect("index out of bound for XmlParserNodeInfoSeq.")
}
}
#[doc(alias = "xmlParserNodeInfo")]
#[repr(C)]
#[derive(Clone, Copy, Default)]
pub struct XmlParserNodeInfo {
pub(crate) node: Option<XmlNodePtr>,
pub(crate) begin_pos: u64,
pub(crate) begin_line: u64,
pub(crate) end_pos: u64,
pub(crate) end_line: u64,
}
impl XmlParserCtxt<'_> {
#[doc(alias = "xmlParserAddNodeInfo")]
pub(crate) fn add_node_info(&mut self, info: Rc<RefCell<XmlParserNodeInfo>>) {
let pos = self.node_seq.binary_search(info.borrow().node);
match pos {
Ok(pos) => {
self.node_seq[pos] = info;
}
Err(pos) => {
self.node_seq.insert(pos, info);
}
}
}
#[doc(alias = "xmlParserFindNodeInfo")]
pub(crate) fn find_node_info(
&self,
node: XmlNodePtr,
) -> Option<Rc<RefCell<XmlParserNodeInfo>>> {
let pos = self.node_seq.binary_search(Some(node)).ok()?;
Some(self.node_seq[pos].clone())
}
}