pub trait Output<M> {
type Error;
// Required method
fn put_str(&mut self, text: &str, meta: &M) -> Result<(), Self::Error>;
// Provided methods
fn put_char(&mut self, ch: char, meta: &M) -> Result<(), Self::Error> { ... }
fn put_fmt(
&mut self,
args: Arguments<'_>,
meta: &M,
) -> Result<(), Self::Error> { ... }
}Expand description
Trait that consumes a rendered annotated snippet.
Rendering produces a stream of text fragments , each tagged with some
metadata M that describes how that fragment should be presented (for
example, a color/style).
You can implement this trait to plug in your preferred output backend: plain text, terminal coloring, HTML, etc.
M is an implementor-defined metadata type. You can use () if you do not
need it.
§Example
A simple Output implementation that captures rendered fragments alongside
their metadata:
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Style {
Normal,
Emph,
}
// Note that `Annotations::render()` takes the `Output` implementor by value,
// so we need to wrap the mutable reference.
struct Capture<'a>(pub &'a mut Vec<(String, Style)>);
impl sourceannot::Output<Style> for Capture<'_> {
type Error = std::convert::Infallible;
fn put_str(&mut self, text: &str, meta: &Style) -> Result<(), Self::Error> {
self.0.push((text.to_string(), *meta));
Ok(())
}
}Required Associated Types§
Sourcetype Error
type Error
Error type produced by this output backend.
For example, it can be std::io::Error when writing to an I/O
stream, or std::convert::Infallible when the output cannot fail.