Crate facet_html

Crate facet_html 

Source
Expand description

HTML parser and serializer implementing the facet format architecture.

This crate provides:

  • Parsing: WHATWG-compliant HTML tokenization via html5gum
  • Serialization: Configurable HTML output (minified or pretty-printed)

§Attributes

After importing use facet_html as html;, you can use these attributes:

  • #[facet(html::element)] - Marks a field as a single HTML child element
  • #[facet(html::elements)] - Marks a field as collecting multiple HTML child elements
  • #[facet(html::attribute)] - Marks a field as an HTML attribute (on the element tag)
  • #[facet(html::text)] - Marks a field as the text content of the element

§Parsing Example

use facet::Facet;
use facet_format::FormatDeserializer;
use facet_html::HtmlParser;

#[derive(Debug, Facet, PartialEq)]
struct Document {
    #[facet(default)]
    head: Option<Head>,
    #[facet(default)]
    body: Option<Body>,
}

#[derive(Debug, Facet, PartialEq)]
struct Head {
    #[facet(default)]
    title: Option<String>,
}

#[derive(Debug, Facet, PartialEq)]
struct Body {
    #[facet(default)]
    text: String,
}

§Serialization Example

use facet::Facet;
use facet_xml as xml;
use facet_html::{to_string, to_string_pretty};

#[derive(Debug, Facet)]
#[facet(rename = "div")]
struct MyDiv {
    #[facet(xml::attribute, default)]
    class: Option<String>,
    #[facet(xml::text, default)]
    content: String,
}

let div = MyDiv {
    class: Some("container".into()),
    content: "Hello!".into(),
};

// Minified output (default)
let html = to_string(&div).unwrap();

// Pretty-printed output
let html_pretty = to_string_pretty(&div).unwrap();

§Pre-defined HTML Element Types

This crate provides typed definitions for all standard HTML5 elements in the elements module. You can use these to deserialize HTML into strongly-typed Rust structures:

use facet_html::elements::{Html, Div, P, A};

Modules§

elements
Typed HTML element definitions.

Structs§

HtmlParser
HTML parser implementing the FormatParser trait.
HtmlSerializeError
Error type for HTML serialization.
HtmlSerializer
HTML serializer with configurable output options.
SerializeOptions
Options for HTML serialization.

Enums§

Attr
HTML attribute types for field and container configuration.
HtmlError
Error type for HTML parsing.

Functions§

from_slice
Deserialize an HTML document from bytes.
from_str
Deserialize an HTML document from a string.
to_string
Serialize a value to an HTML string with default options (minified).
to_string_pretty
Serialize a value to a pretty-printed HTML string.
to_string_with_options
Serialize a value to an HTML string with custom options.
to_vec
Serialize a value to HTML bytes with default options.
to_vec_with_options
Serialize a value to HTML bytes with custom options.