Expand description
§Damask — compile-time components for Rust
React-like, compile-time components for Rust. A component is a struct (its
fields are its props) paired with a .dmk template. The Component derive
compiles the template into a Render::render_into method, so rendering
is plain Rust with no runtime template parsing.
use damask::Component;
// greeting.rs — paired with greeting.dmk containing: Hello {self.name}!
#[derive(Component)]
pub struct Greeting {
pub name: String,
}
let out = Greeting { name: "Ada".into() }.render();
assert_eq!(out, "Hello Ada!");§The two traits
Rendereris the extensibility seam: it owns the output buffer and the escaping policy. Implement it to change escaping, target a different sink, or stream. Components render into&mut dyn Renderer, so a component compiled once works with any renderer.Componentis implemented by the macro.Component::renderrenders to aStringusing the default renderer chosen by the template’s host language.
See renderers for the built-ins and StringRenderer
for the easiest custom-renderer starting point.
§Props and slots
A struct’s fields are its props. Its template’s <slot>s are not fields:
they are content the caller supplies, and they travel as a Slots argument
to Render::render_slots. See Slots for what that buys and costs — and
for the methods a template reaches through its slots binding, which is that
same argument under a name templates may write.
A prop must be passed unless its type says what leaving it out means:
Option<_> is None. #[component(default)] on the struct extends that to
every prop, filling the skipped ones from its Default. See props for
how a call site — which cannot see the struct it is building — is held to
that.
Re-exports§
pub use attr::Attr;pub use attr::AttrSet;pub use attr::AttrSpread;pub use attr::AttrValue;pub use attr::Attrs;pub use attr::IntoAttrValue;pub use attr::TokenItem;pub use attr::TokenList;pub use attr::is_attr_name_safe;pub use renderers::HtmlRenderer;pub use renderers::Whitespace;pub use trusted::Content;pub use trusted::Sink;pub use trusted::ToTrusted;pub use trusted::Trusted;pub use trusted::Value;pub use trusted::splice;
Modules§
- attr
- What an attribute value may be, and how the two helpers build one.
- prelude
- Common imports for authoring and using components.
- props
- Typestate and conversions for the prop builder the
Componentderive generates. - renderers
- Built-in
Rendererimplementations, one per host language, plusStringRenderer— the sharedString-backed core they are built on and the easiest starting point for a custom renderer. - trusted
- Markup that is already safe to write, and the seam that recognises it.
Macros§
Structs§
- Async
Fragment - Wraps a closure returning a boxed future as
AsyncRender— the async counterpart ofFragment, for a{#snippet}whose enclosing template awaits something. Build one withfragment_async. - Fragment
- Wraps a
Fn(&mut dyn Renderer)closure asRender. - Slot
- One named piece of caller-supplied content, as passed to
Render::render_slots. - Slots
- The slot content a caller passes to one component render.
Enums§
- Fill
- One slot’s content, and whether producing it suspends.
Constants§
- DEFAULT_
SLOT - The name of the slot
<slot/>marks — the one with noname="…", filled by a caller’s content that carries noslot="…".
Traits§
- Async
Component - The async counterpart of
Component, for a component whose template genuinely.awaits something — seeAsyncRenderfor why that means a separate trait rather than one method that is sometimes async. - Async
Render - The async counterpart of
Render, for content whose template genuinely.awaits something. - Component
- A renderable component.
- Render
- Renderable content: given a renderer, write yourself into it.
- Renderer
- A sink that accumulates rendered output and owns the escaping policy.
Functions§
- as_
display - Widen a reference to
&dyn DisplayforRenderer::write_escapedandRenderer::write_display_raw. - fragment
- Turn a
|r: &mut dyn Renderer| { … }closure into renderable content. - fragment_
async - Turn a
|r: &mut dyn Renderer| { Box::pin(async move { … }) }closure into async-renderable content — the async counterpart offragment.
Type Aliases§
- Render
Future - A future returned by an
AsyncRendermethod.