[][src]Trait language_reporting::Render

pub trait Render {
    fn render(self, into: Document) -> Document;

    fn into_fragment(self) -> Document { ... }
fn add<Right>(self, other: Right) -> Combine<Self, Right>
    where
        Right: Render
, { ... } }

The Render trait defines a type that can be added to a Document. It is defined for Node, String, &str, and Document.alloc

It is also defined for Option<T> where T is Render, as well as &T where T is both Render and Clone.

Generally speaking, if you need to make a type Render, and it's not one of your types, you can ergonomically make a newtype wrapper for it.

For example, if you want to render std::time::Duration:

#[macro_use]
extern crate render_tree;
extern crate termcolor;
use render_tree::{Render, Document, Line, RenderComponent};
use std::time::Duration;
use termcolor::StandardStream;

struct RenderDuration(Duration);

impl Render for RenderDuration {
    fn render(self, into: Document) -> Document {
        into.add(format!("{} seconds and {} nanos", self.0.as_secs(), self.0.subsec_nanos()))
    }
}

struct MessageContents {
    code: usize,
    header: String,
    body: String,
    duration: Duration,
}

fn message(args: MessageContents, into: Document) -> Document {
    into.render(tree! {
        <Line as {
            {args.code} ":" {args.header} "for" {RenderDuration(args.duration)}
        }>

        <Line as {
            {args.body}
        }>
    })
}

fn main() -> std::io::Result<()> {
    let contents = MessageContents {
        code: 200,
        header: "Hello world".to_string(),
        body: "This is the body of the message".to_string(),
        duration: Duration::new(100, 1_000_000)
    };

    let document = tree! { <message args={contents}> };

    document.write()
}

Required methods

fn render(self, into: Document) -> Document

Produce a new Document with self added to the into Document.

Loading content...

Provided methods

fn into_fragment(self) -> Document

fn add<Right>(self, other: Right) -> Combine<Self, Right> where
    Right: Render

Loading content...

Implementors

impl Render for Node[src]

A node is rendered by adding itself to the document

impl Render for Document[src]

A Document is rendered by extending its nodes onto the original document.

impl Render for Empty[src]

impl<B, Block> Render for CurriedBlockComponent<B, Block> where
    B: BlockComponent,
    Block: FnOnce(Document) -> Document
[src]

impl<B, Block> Render for CurriedIterBlockComponent<B, Block> where
    B: IterBlockComponent,
    Block: FnMut(<B as IterBlockComponent>::Item, Document) -> Document
[src]

impl<B, Block> Render for CurriedOnceBlockComponent<B, Block> where
    B: OnceBlockComponent,
    Block: FnOnce(<B as OnceBlockComponent>::Item, Document) -> Document
[src]

impl<F> Render for OnceBlock<F> where
    F: FnOnce(Document) -> Document
[src]

impl<Left, Right> Render for Combine<Left, Right> where
    Left: Render,
    Right: Render
[src]

impl<T> Render for T where
    T: Display
[src]

Loading content...