use crate::{error::Result, parse::Instr, reflect::ReflectIntermediate};
pub trait Inspector {
fn inspect<'a>(&mut self, itm: &mut ReflectIntermediate<'a>, instr: &Instr) -> Result<()>;
fn chain<'a, I2: Inspector>(&'a mut self, second: &'a mut I2) -> Chain<Self, I2>
where
Self: Sized,
{
Chain {
first: self,
second: second,
}
}
}
pub(crate) struct FnInspector<F: FnMut(&mut ReflectIntermediate<'_>, &Instr)>(pub F);
impl<F: FnMut(&mut ReflectIntermediate<'_>, &Instr)> Inspector for FnInspector<F> {
fn inspect<'a>(&mut self, itm: &mut ReflectIntermediate<'a>, instr: &Instr) -> Result<()> {
Ok(self.0(itm, instr))
}
}
pub struct Chain<'a, I1: Inspector, I2: Inspector> {
first: &'a mut I1,
second: &'a mut I2,
}
impl<I1: Inspector, I2: Inspector> Inspector for Chain<'_, I1, I2> {
fn inspect<'a>(&mut self, itm: &mut ReflectIntermediate<'a>, instr: &Instr) -> Result<()> {
self.first.inspect(itm, instr)?;
self.second.inspect(itm, instr)
}
}