Skip to main content

Crate damask

Crate damask 

Source
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

  • Renderer is 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.
  • Component is implemented by the macro. Component::render renders to a String using 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 Component derive generates.
renderers
Built-in Renderer implementations, one per host language, plus StringRenderer — the shared String-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§

tag
Build one element as Trusted markup, in Rust.

Structs§

AsyncFragment
Wraps a closure returning a boxed future as AsyncRender — the async counterpart of Fragment, for a {#snippet} whose enclosing template awaits something. Build one with fragment_async.
Fragment
Wraps a Fn(&mut dyn Renderer) closure as Render.
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 no name="…", filled by a caller’s content that carries no slot="…".

Traits§

AsyncComponent
The async counterpart of Component, for a component whose template genuinely .awaits something — see AsyncRender for why that means a separate trait rather than one method that is sometimes async.
AsyncRender
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 Display for Renderer::write_escaped and Renderer::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 of fragment.

Type Aliases§

RenderFuture
A future returned by an AsyncRender method.

Derive Macros§

Component
Derive macro that generates a Component impl from a struct’s paired .dmk template. Shares its name with the trait (like serde::Serialize), so use damask::Component; brings both into scope. Derive damask::Component for a struct, generating its render_into from the paired .dmk template.