[][src]Trait render_tree::Render

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

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

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: Render>(self, other: Right) -> Combine<Self, Right>

Loading content...

Implementors

impl Render for Node
[src]

A node is rendered by adding itself to the document

fn into_fragment(self) -> Document
[src]

fn add<Right: Render>(self, other: Right) -> Combine<Self, Right>
[src]

impl Render for Document
[src]

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

fn into_fragment(self) -> Document
[src]

fn add<Right: Render>(self, other: Right) -> Combine<Self, Right>
[src]

impl Render for Empty
[src]

fn into_fragment(self) -> Document
[src]

fn add<Right: Render>(self, other: Right) -> Combine<Self, Right>
[src]

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

fn into_fragment(self) -> Document
[src]

fn add<Right: Render>(self, other: Right) -> Combine<Self, Right>
[src]

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

fn into_fragment(self) -> Document
[src]

fn add<Right: Render>(self, other: Right) -> Combine<Self, Right>
[src]

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

fn into_fragment(self) -> Document
[src]

fn add<Right: Render>(self, other: Right) -> Combine<Self, Right>
[src]

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

fn into_fragment(self) -> Document
[src]

fn add<Right: Render>(self, other: Right) -> Combine<Self, Right>
[src]

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

fn into_fragment(self) -> Document
[src]

fn add<Right: Render>(self, other: Right) -> Combine<Self, Right>
[src]

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

fn into_fragment(self) -> Document
[src]

fn add<Right: Render>(self, other: Right) -> Combine<Self, Right>
[src]

Loading content...