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§
Provided Methods§
fn into_fragment(self) -> Document
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", so this trait is not object safe.
Implementors§
impl Render for Node
A node is rendered by adding itself to the document
impl Render for Document
A Document is rendered by extending its nodes onto the original document.