use std::{borrow::BorrowMut, collections::VecDeque};
use git_hash::{oid, ObjectId};
use git_object::{tree, TreeRefIter};
use quick_error::quick_error;
use crate::tree::visit::Visit;
quick_error! {
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
NotFound{oid: ObjectId} {
display("The tree {} could not be found", oid)
}
Cancelled {
display("The delegate cancelled the operation")
}
ObjectDecode(err: git_object::decode::Error) {
display("An object could not be decoded")
source(err)
from()
}
}
}
#[derive(Default, Clone)]
pub struct State {
next: VecDeque<(bool, ObjectId)>,
buf: Vec<u8>,
}
impl State {
fn clear(&mut self) {
self.next.clear();
self.buf.clear();
}
}
pub fn traverse<StateMut, Find, V>(
root: TreeRefIter<'_>,
mut state: StateMut,
mut find: Find,
delegate: &mut V,
) -> Result<(), Error>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<TreeRefIter<'a>>,
StateMut: BorrowMut<State>,
V: Visit,
{
let state = state.borrow_mut();
state.clear();
let mut tree = root;
loop {
for entry in tree {
let entry = entry?;
match entry.mode {
tree::EntryMode::Tree => {
use super::visit::Action::*;
delegate.push_path_component(entry.filename);
let action = delegate.visit_tree(&entry);
match action {
Skip => {}
Continue => {
delegate.pop_path_component();
delegate.push_back_tracked_path_component(entry.filename);
state.next.push_back((true, entry.oid.to_owned()))
}
Cancel => {
return Err(Error::Cancelled);
}
}
}
_non_tree => {
delegate.push_path_component(entry.filename);
if delegate.visit_nontree(&entry).cancelled() {
return Err(Error::Cancelled);
}
}
}
delegate.pop_path_component();
}
match state.next.pop_front() {
Some((should_pop_path, oid)) => {
if should_pop_path {
delegate.pop_front_tracked_path_and_set_current();
}
match find(&oid, &mut state.buf) {
Some(tree_iter) => tree = tree_iter,
None => return Err(Error::NotFound { oid: oid.to_owned() }),
}
}
None => break Ok(()),
}
}
}