Skip to content

crate

weft_dsl

Weft template DSL.

A deliberately small, indentation-insensitive language for component templates:

column(gap: 8) {
    heading(level: 1) "Count: {count}"
    button(@click: increment, label: "Add one") "+"
    if !done { text "pending" } else { text "done" }
    for item in items { text "{item.name}" }
    Counter(start: 5)        // capitalised names are components
}

Modules

  • decl

    Declarations: a template's own state, derived values and handlers.

  • expr

    Expressions: what attribute values, {…} in strings, if conditions and for … in can compute. Pure: an expression reads its scope and returns a value.

  • stdlib

    The standard functions templates can call, as signatures: what the parser checks calls against and the language server shows. The functions themselves are in weft-core.

Structs

  • Attr

    name: value inside an element's parentheses.

  • DslError

    A lexing or parsing error with the position it occurred at.

  • Element

    An element (or component, when the name is capitalised).

  • EventBinding

    @event: handler inside an element's parentheses.

  • ForBlock

    for binding in iterable { … }

  • FormatOptions

    How to lay a template out.

  • Highlight

    A highlighted byte range of the source.

  • IfBlock

    if [!]cond { … } else { … }

  • Path

    A dotted reference into the component scope, e.g. todo.title.

  • Span

    A 1-based line/column position in template source.

  • Template

    A parsed template: a list of root nodes.

  • TextTemplate

    A string with {path} interpolations.

  • Token

    A token and where it starts.

Enums

Functions

command_parts

pub fn command_parts<'a>(name: &str, args: &'a [Expr]) -> (&'a [Expr], &'a [Expr])

A command's arguments split into the values it computes and the names it refers to (handlers, or the load to reload).

command_shape

pub fn command_shape(name: &str) -> Option<(usize, usize, usize)>

How a command's arguments are used: how many are values (the URL, the body), and how many after them may name handlers (ok, err) — or, for reload, a load. None for anything that isn't a command.

format

pub fn format(src: &str, options: &FormatOptions) -> Option<String>

src formatted, or None if it doesn't parse or can't be formatted safely.

format_spelled

pub fn format_spelled(src: &str, options: &FormatOptions, spell: &dyn Fn(Range<usize>) -> String) -> Option<String>

As format(), with spell giving each token's and comment's text from its byte range in src: a template inside Rust keeps its Rust spelling of strings. The caller checks the result, as only it can parse that spelling.

highlight

pub fn highlight(src: &str) -> Vec<Highlight>

Highlighted ranges in src, in order. Never fails.

is_page_meta

pub fn is_page_meta(e: &Element) -> bool

meta(title: …) or meta(description: …): page metadata, which renders nothing. A meta with name, content, property or itemprop is an HTML <meta> (microdata, for instance) and renders as one.

lex

pub fn lex(src: &str) -> Result<Vec<Token>, DslError>

Split template source into tokens. Always ends with [TokenKind::Eof].

parse

pub fn parse(src: &str) -> Result<Template, DslError>

Parse template source into a Template.

A use stays in the declarations as it is; parse_file reads it.

parse_expr

pub fn parse_expr(src: &str) -> Result<Expr, DslError>

Parse one expression (an interpolation's inside).

parse_file

pub fn parse_file(src: &str, file: &str, read: &dyn Fn(&str) -> Option<String>) -> Result<Template, DslError>

Parse src, the template in file, putting each use "other.weft"'s declarations in its place. read gives a file's text from its path, which is file's folder joined with the path the use names. A file used twice is read once, and a file that uses itself is an error.

segments

pub fn segments(src: &str) -> Vec<(Option<HighlightKind>, String)>

src split into consecutive pieces, each with its highlight kind (None for whitespace and anything unclassified). Concatenating the pieces gives back src.

use_path

pub fn use_path(file: &str, path: &str) -> String

The path a use in file names: file's folder joined with path, with . and .. worked out.

Constants

COMMANDS

pub const COMMANDS: &[&str] = _;

The commands an on handler can run besides setting state.

LOAD_REQUESTS

pub const LOAD_REQUESTS: &[&str] = _;

The requests a load can make.