use std::sync::Arc;
use rdf_syntax::{Iri, IriBuf};
#[derive(Clone)]
pub struct ProcessingStack {
head: Option<Arc<StackNode>>,
}
impl ProcessingStack {
pub fn new() -> Self {
Self { head: None }
}
pub fn is_empty(&self) -> bool {
self.head.is_none()
}
pub fn cycle(&self, url: &Iri) -> bool {
match &self.head {
Some(head) => head.contains(url),
None => false,
}
}
pub fn push(&mut self, url: IriBuf) -> bool {
if self.cycle(&url) {
false
} else {
let mut head = None;
std::mem::swap(&mut head, &mut self.head);
self.head = Some(Arc::new(StackNode::new(head, url)));
true
}
}
}
impl Default for ProcessingStack {
fn default() -> Self {
Self::new()
}
}
struct StackNode {
previous: Option<Arc<StackNode>>,
url: IriBuf,
}
impl StackNode {
fn new(previous: Option<Arc<StackNode>>, url: IriBuf) -> StackNode {
StackNode { previous, url }
}
fn contains(&self, url: &Iri) -> bool {
if self.url == *url {
true
} else {
match &self.previous {
Some(prev) => prev.contains(url),
None => false,
}
}
}
}