Skip to main content

Render

Trait Render 

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

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

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§

Source

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

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

Provided Methods§

Source

fn into_fragment(self) -> Document

Source

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Render for Document

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

Source§

impl Render for Empty

Source§

impl Render for Node

A node is rendered by adding itself to the document

Source§

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

Source§

impl<B, Block> Render for CurriedIterBlockComponent<B, Block>

Source§

impl<B, Block> Render for CurriedOnceBlockComponent<B, Block>

Source§

impl<F> Render for OnceBlock<F>
where F: FnOnce(Document) -> Document,

Source§

impl<Left, Right> Render for Combine<Left, Right>
where Left: Render, Right: Render,

Source§

impl<T> Render for T
where T: Display,