Skip to main content

Output

Trait Output 

Source
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§

Source

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.

Required Methods§

Source

fn put_str(&mut self, text: &str, meta: &M) -> Result<(), Self::Error>

Writes a UTF-8 text fragment with associated metadata.

Provided Methods§

Source

fn put_char(&mut self, ch: char, meta: &M) -> Result<(), Self::Error>

Writes a single character with associated metadata.

Source

fn put_fmt(&mut self, args: Arguments<'_>, meta: &M) -> Result<(), Self::Error>

Writes formatted text with associated metadata.

Implementations on Foreign Types§

Source§

impl<M> Output<M> for &mut String

Writing to a String ignores metadata.

Source§

type Error = Infallible

Source§

fn put_str(&mut self, text: &str, _meta: &M) -> Result<(), Self::Error>

Source§

fn put_char(&mut self, ch: char, _meta: &M) -> Result<(), Self::Error>

Source§

fn put_fmt(&mut self, args: Arguments<'_>, _meta: &M) -> Result<(), Self::Error>

Implementors§

Source§

impl<W: Write, M> Output<M> for PlainOutput<W>

Available on crate feature std only.