pub trait Render {
// Required method
fn write_into(
&self,
out: &mut String,
options: &RenderOptions,
depth: usize,
);
// Provided methods
fn render(&self) -> String { ... }
fn render_pretty(&self) -> String { ... }
fn render_with(&self, options: &RenderOptions) -> String { ... }
}Expand description
Anything that can be written as HTML.
Implement write_into; the rest comes free.
§Implementing it for your own type
Most types should not: build an Element and render that. Do it
when a type is a piece of markup and callers want to pass it where a node is expected,
by delegating to the tree it builds.
use molt::prelude::*;
struct Badge(&'static str);
impl Render for Badge {
fn write_into(&self, out: &mut String, options: &RenderOptions, depth: usize) {
span().add_class("badge").text(self.0).write_into(out, options, depth);
}
}
assert_eq!(Badge("new").render(), r#"<span class="badge">new</span>"#);Whatever you write must already be escaped: use Element and
text() rather than pushing user text into out yourself.
§Stability
Render is part of the 1.x API. write_into(&self, &mut String, &RenderOptions, usize)
will not change, and no required method will be added; provided methods may be. The
buffer is a String on purpose, so a whole tree renders into one allocation, and
depth is the indentation level the output starts at (0 for a root). Implementations
for &T, Box<T> and Option<T> exist; Document deliberately has none, because its
inherent render() is pretty and this trait’s is compact.
§Depth
The provided implementations walk an explicit work stack rather than recursing, so nesting depth costs heap rather than stack and there is no depth at which rendering aborts. A 100,000-level tree is covered by a test.
Pretty mode still writes one indent string per level on every line, which makes its
output quadratic in depth. That is a size to be aware of when depth comes from
untrusted input — see SECURITY.md — not a limit on what renders.
Required Methods§
Sourcefn write_into(&self, out: &mut String, options: &RenderOptions, depth: usize)
fn write_into(&self, out: &mut String, options: &RenderOptions, depth: usize)
Writes this node and its subtree into an existing buffer.
This is the primitive every other method here is built on. Writing into a shared buffer avoids allocating an intermediate string per node.
Provided Methods§
Sourcefn render(&self) -> String
fn render(&self) -> String
Renders as a single line of markup.
Note the asymmetry with crate::Document::render, which defaults to pretty.
The golden fixtures depend on that difference.
Sourcefn render_pretty(&self) -> String
fn render_pretty(&self) -> String
Renders with indentation and line breaks.
Sourcefn render_with(&self, options: &RenderOptions) -> String
fn render_with(&self, options: &RenderOptions) -> String
Renders with the given options.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<T: Render + ?Sized> Render for &T
impl<T: Render + ?Sized> Render for &T
fn write_into(&self, out: &mut String, options: &RenderOptions, depth: usize)
Source§impl<T: Render + ?Sized> Render for Box<T>
impl<T: Render + ?Sized> Render for Box<T>
fn write_into(&self, out: &mut String, options: &RenderOptions, depth: usize)
Source§impl<T: Render> Render for Option<T>
None writes nothing.
impl<T: Render> Render for Option<T>
None writes nothing.