use crate::latex::token::{SpannedToken, Token};
use std::io;
#[allow(clippy::type_complexity)]
pub trait Formatter<'source>: Iterator<Item = SpannedToken<'source>> {
fn write_formatted<W>(&mut self, source: &'source str, buffer: &mut W) -> io::Result<()>
where
W: io::Write,
{
for (token, span) in self {
match token {
Token::OwnedString(string) => buffer.write_all(string.as_bytes())?,
_ => buffer.write_all(source[span].as_bytes())?,
}
}
Ok(())
}
}
impl<'source, I> Formatter<'source> for I where I: Iterator<Item = SpannedToken<'source>> {}
#[derive(Debug)]
pub struct DummyFormatter<'source, I>
where
I: Iterator<Item = SpannedToken<'source>>,
{
iter: I,
}
impl<'source, I> DummyFormatter<'source, I>
where
I: Iterator<Item = SpannedToken<'source>>,
{
pub fn new(iter: I) -> Self {
Self { iter }
}
}
impl<'source, I> Iterator for DummyFormatter<'source, I>
where
I: Iterator<Item = SpannedToken<'source>>,
{
type Item = SpannedToken<'source>;
fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() {
Some((Token::Comment, _)) => self.next(),
next => next,
}
}
}