strings::template
This module provides support for formatting of large or complex strings beyond the scope of fmt::. A template is compiled using compile, then executed with execute to print formatted text to an io::handle.
The template format is a string with variables substituted using "$". Variable names consist of alphanumeric ASCII characters (i.e. for which ascii::isalnum returns true) or underscores ('_'). A literal "$" may be printed by using it twice: "$$". Variables may also be used with braces, i.e. ${variable}, so that they can be placed immediately next to alphanumeric characters; such variables may include non-alphanumeric characters other than '{' and '}'.
const src = "Hello, $user! Your balance is $$$balance.\n";
const template = template::compile(src)!;
defer template::finish(&template);
template::execute(&template, os::stdout,
("user", "ddevault"),
("balance", 1000),
)!; // "Hello, ddevault! Your balance is $1000.
Index
Types
type param = (str, fmt::formattable);
type instruction = (literal | variable);
type literal = str;
type template = []instruction;
type variable = str;
Errors
type invalid = !void;
Functions
fn compile(input: str) (template | invalid);
fn execute(tmpl: *template, out: io::handle, params: param...) (size | io::error);
fn finish(tmpl: *template) void;
fn strerror(err: invalid) str;
Types
type param
type param = (str, fmt::formattable);
Parameters to execute with a template, a tuple of a variable name and a formattable object.
type instruction
Show undocumented member
type instruction = (literal | variable);
type literal
Show undocumented member
type literal = str;
type template
Show undocumented member
type template = []instruction;
type variable
Show undocumented member
type variable = str;
Errors
type invalid
type invalid = !void;
The template string has an invalid format.
Functions
fn compile
fn compile(input: str) (template | invalid);
Compiles a template string. The return value must be freed with finish after use.
fn execute
fn execute(tmpl: *template, out: io::handle, params: param...) (size | io::error);
Executes a template, writing the output to the given io::handle. If the template calls for a parameter which is not provided, an assertion will be fired.
fn finish
fn finish(tmpl: *template) void;
Frees resources associated with a template.
fn strerror
fn strerror(err: invalid) str;
Converts an error into a human-friendly string.