Trait Render

Source
pub trait Render {
    // Required method
    fn render_to(&self, f: &mut Formatter<'_>) -> Result;

    // Provided method
    fn render(&self) -> Raw<String> { ... }
}
Expand description

Trait for safely rendering HTML content.

Render is similar to Display and Debug but it is for safely generating HTML.

§Example

use std::fmt::{Formatter, Result};
use gen_html::Render;

struct Vector2D {
    x: f32,
    y: f32,
}

impl Render for Vector2D {
    fn render_to(&self, f: &mut Formatter) -> Result {
        let Self { x, y } = self;
        // `f32` cannot contain special characters, so we don't need to worry about escaping
        write!(f, "({x}, {y})")
    }
}

Required Methods§

Source

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Renders HTML to a given formatter.

When implementing this function, you should make sure that the output is valid HTML.

§Errors

This function should return Err if, and only if, the provided Formatter returns Err.

Provided Methods§

Source

fn render(&self) -> Raw<String>

Converts the given value to a Raw<String>.

let content = "<this is escaped>";
assert_eq!(content.render().0, "&lt;this is escaped&gt;");

Implementations on Foreign Types§

Source§

impl Render for f32

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for f64

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for i8

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for i16

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for i32

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for i64

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for i128

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for isize

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for str

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for u8

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for u16

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for u32

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for u64

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for u128

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for usize

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for String

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl Render for Arguments<'_>

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<B> Render for Cow<'_, B>
where B: Render + ToOwned + ?Sized,

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> Render for &T
where T: Render + ?Sized,

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> Render for &mut T
where T: Render + ?Sized,

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> Render for Box<T>
where T: Render + ?Sized,

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> Render for Rc<T>
where T: Render + ?Sized,

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> Render for Arc<T>
where T: Render + ?Sized,

Source§

fn render_to(&self, f: &mut Formatter<'_>) -> Result

Implementors§

Source§

impl<F> Render for RenderFn<F>
where F: Fn(&mut Formatter<'_>) -> Result,

Source§

impl<T: Display> Render for Escaped<T>

Source§

impl<T: Display> Render for Raw<T>