Trait HtmlEnv

Source
pub trait HtmlEnv: Write + Sized {
    // Provided methods
    fn write_html(&mut self, html: impl Html) -> Result<&mut Self, Error> { ... }
    fn doctype(&mut self) { ... }
    fn write_html_text<'s>(&'s mut self) -> HtmlEscaper<'s, Self> { ... }
    fn open_tag<'s, 't>(
        &'s mut self,
        tag: &'t str,
        compactability: Compactability,
    ) -> Result<TagOpening<'s, 't, Self>, Error> { ... }
    fn with_html_writer(
        self,
        f: impl FnOnce(&mut Self) -> Result,
    ) -> Result<Self, Error> { ... }
}
Expand description

Represents an environment that can write HTML.

This trait is implemented for everything that implements Write, for example String.

Provided Methods§

Source

fn write_html(&mut self, html: impl Html) -> Result<&mut Self, Error>

Writes an Html into the environment.

§Example
use write_html::{HtmlEnv, Html, AsHtml};
 
let mut s = String::new();
s.write_html("Hello, world!".as_html()).unwrap();
assert_eq!(s, "Hello, world!");
Source

fn doctype(&mut self)

Writes the HTML5 doctype.

§Example
use write_html::HtmlEnv;
 
let mut s = String::new();
s.doctype();
assert_eq!(s, "<!DOCTYPE html>");
Source

fn write_html_text<'s>(&'s mut self) -> HtmlEscaper<'s, Self>

Lets you write text into the HTML document, escaping it as necessary.

§Example
use write_html::HtmlEnv;
use std::fmt::Write;
 
let mut s = String::new();
s.write_html_text().write_str("Hello, <world>").unwrap();
assert_eq!(s, "Hello, &lt;world&gt;");
Source

fn open_tag<'s, 't>( &'s mut self, tag: &'t str, compactability: Compactability, ) -> Result<TagOpening<'s, 't, Self>, Error>

Returns a tag opening, which lets you write attributes and inner HTML.

§Arguments
  • tag - The tag name.
  • compactability - Whether the tag can be compacted.
§Example
use write_html::{HtmlEnv, Compactability};
use std::fmt::Write;
 
let mut s = String::new();
s.open_tag("h1", Compactability::No).unwrap()
    .with_attr("id", "my-id").unwrap()
    .inner_html().unwrap()
    .write_str("Hello, world!").unwrap();
assert_eq!(s, "<h1 id=\"my-id\">Hello, world!</h1>");
Source

fn with_html_writer( self, f: impl FnOnce(&mut Self) -> Result, ) -> Result<Self, Error>

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§

Source§

impl<W: Write> HtmlEnv for W