#[cfg(feature = "html")]
mod html;
#[cfg(all(
feature = "html", feature = "theme")
)]
mod html_themed;
#[cfg(all(
feature = "terminal", feature = "theme")
)]
mod terminal;
use tree_sitter_highlight::HighlightEvent;
use crate::error::InkjetResult as Result;
#[cfg(feature = "html")]
pub use html::*;
#[cfg(all(
feature = "html", feature = "theme")
)]
pub use html_themed::*;
#[cfg(all(
feature = "terminal", feature = "theme")
)]
pub use terminal::*;
#[allow(unused_variables)]
pub trait Formatter {
fn write<W>(&self, source: &str, writer: &mut W, event: HighlightEvent) -> Result<()>
where
W: std::fmt::Write;
#[inline]
fn write_io<W>(&self, source: &str, writer: &mut W, event: HighlightEvent) -> Result<()>
where
W: std::io::Write,
{
self.write(source, &mut IoWrapper(writer), event)?;
Ok(())
}
#[inline]
fn start<W>(&self, source: &str, writer: &mut W) -> Result<()>
where
W: std::fmt::Write,
{
Ok(())
}
#[inline]
fn finish<W>(&self, source: &str, writer: &mut W) -> Result<()>
where
W: std::fmt::Write,
{
Ok(())
}
#[inline]
fn start_io<W>(&self, source: &str, writer: &mut W) -> Result<()>
where
W: std::io::Write,
{
self.start(source, &mut IoWrapper(writer))
}
#[inline]
fn finish_io<W>(&self, source: &str, writer: &mut W) -> Result<()>
where
W: std::io::Write,
{
self.finish(source, &mut IoWrapper(writer))
}
}
pub struct IoWrapper<'a, W>(pub &'a mut W)
where
W: std::io::Write + ?Sized;
impl<'a, W> IoWrapper<'a, W>
where
W: std::io::Write + ?Sized,
{
pub fn new(writer: &'a mut W) -> Self {
Self(writer)
}
}
impl<'a, W> std::fmt::Write for IoWrapper<'a, W>
where
W: std::io::Write + ?Sized,
{
fn write_str(&mut self, s: &str) -> std::fmt::Result {
self.0.write_all(s.as_bytes()).map_err(|_| std::fmt::Error)
}
}