use crate::Error;
use crate::linear::proofs::InfId;
use crate::linear::proofs::style::Drawn;
use std::fmt::Write;
#[derive(Clone, Copy)]
pub(crate) enum Step {
Enter(InfId, usize),
#[cfg_attr(
not(any(feature = "typst", feature = "svg")),
expect(
dead_code,
reason = "only the layouts of their own read the depth on the way up"
)
)]
Exit(InfId, usize),
}
pub(crate) fn walk<T: Drawn, E>(
derivation: &T,
mut visit: impl FnMut(Step) -> Result<(), E>,
) -> Result<(), E> {
let mut stack = vec![Step::Enter(derivation.root(), 0)];
while let Some(step) = stack.pop() {
visit(step)?;
if let Step::Enter(id, depth) = step {
stack.push(Step::Exit(id, depth));
for &p in derivation.premises(id).iter().rev() {
stack.push(Step::Enter(p, depth + 1));
}
}
}
Ok(())
}
pub(crate) fn flush(
out: &mut impl Write,
buffer: &mut String,
stop: &mut impl FnMut() -> bool,
) -> Result<(), Error> {
out.write_str(buffer)?;
buffer.clear();
if stop() {
Err(Error::Refused(crate::limits::Refusal::Stopped {
phase: crate::limits::Phase::Write,
}))
} else {
Ok(())
}
}