use super::branch::{Branch, BranchId, BranchStatus};
pub trait BranchCallback: Send + Sync {
fn on_branch_failed(&self, branch_id: BranchId, parent_id: Option<BranchId>);
fn on_branch_solved(&self, branch_id: BranchId);
}
pub struct RefCountHandler<C: BranchCallback> {
callback: C,
}
impl<C: BranchCallback> RefCountHandler<C> {
pub fn new(callback: C) -> Self {
Self { callback }
}
pub fn on_child_fail(&self, parent: &Branch) {
let new_count = parent.dec_refcount();
if new_count == 0 {
parent.set_status(BranchStatus::Failed);
self.callback
.on_branch_failed(parent.id(), parent.parent_id());
}
}
pub fn on_branch_solved(&self, branch: &Branch) {
branch.set_status(BranchStatus::Solved);
self.callback.on_branch_solved(branch.id());
}
}
#[derive(Default)]
pub struct NoOpCallback;
impl BranchCallback for NoOpCallback {
fn on_branch_failed(&self, _branch_id: BranchId, _parent_id: Option<BranchId>) {}
fn on_branch_solved(&self, _branch_id: BranchId) {}
}