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§
- Html
Parser - HTML parser implementing the
FormatParsertrait. - Html
Serialize Error - Error type for HTML serialization.
- Html
Serializer - HTML serializer with configurable output options.
- Serialize
Options - Options for HTML serialization.
Enums§
- Attr
- HTML attribute types for field and container configuration.
- Html
Error - 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.