Expand description
§Typed HTML Builder
Type-safe HTML construction with compile-time validation of element nesting following the WHATWG HTML Living Standard.
§Design
The typed builder uses Rust’s type system to enforce valid HTML structure:
- Generic elements:
Element<E>is parameterized by the element type - Compile-time validation: The
CanContaintrait ensures valid parent-child relationships at compile time - Zero runtime overhead: Element types are zero-sized, adding no cost
§Example
use html_builder::typed::{Element, TypedNode};
use html_elements::{Div, Span, P, Ul, Li, A, Text};
// Build a navigation list
let nav = Element::<Ul>::new()
.class("nav")
.child::<Li, _>(|li| {
li.child::<A, _>(|a| {
a.attr("href", "/").text("Home")
})
})
.child::<Li, _>(|li| {
li.child::<A, _>(|a| {
a.attr("href", "/about").text("About")
})
});
let html = nav.render();
assert!(html.contains(r#"<ul class="nav">"#));
assert!(html.contains(r#"<a href="/">Home</a>"#));§Compile-Time Safety
Invalid nesting produces a compilation error:
ⓘ
use html_builder::typed::Element;
use html_elements::{Ul, Div};
// This fails to compile: Ul cannot contain Div
let invalid = Element::<Ul>::new()
.child::<Div, _>(|d| d);Structs§
Enums§
- Typed
Node - A node in the typed HTML tree.