Skip to main content

Crate weaver_lang

Crate weaver_lang 

Source
Expand description

§weaver-lang

A text evaluation language for procedural content generation. Templates contain embedded expressions, control flow, and external calls that are evaluated at runtime into a final string output.

The crate is split into two layers:

  • The language (parsing, AST, evaluation) lives here and has no knowledge of any specific application domain.
  • The host implements EvalContext to provide state (variables, triggers, documents) and populates a Registry with callable processors and commands.

§Quick start

use weaver_lang::{render, SimpleContext, Registry};

let mut ctx = SimpleContext::new();
ctx.set("global", "name", "Alice");

let registry = Registry::new();
let output = render("Hello, {{global:name}}!", &mut ctx, &registry).unwrap();
assert_eq!(output, "Hello, Alice!");

§Compiled templates

For repeated evaluation, parse once with CompiledTemplate::compile and call evaluate against different contexts:

use weaver_lang::{CompiledTemplate, SimpleContext, Registry};

let template = CompiledTemplate::compile("Hello, {{global:name}}!").unwrap();
let registry = Registry::new();

let mut ctx = SimpleContext::new();
ctx.set("global", "name", "Alice");
assert_eq!(template.evaluate(&mut ctx, &registry).unwrap(), "Hello, Alice!");

§Evaluation options

Use EvalOptions to configure resource limits, cancellation, and lenient mode:

use weaver_lang::{render_with_options, EvalOptions, SimpleContext, Registry};

let mut ctx = SimpleContext::new();
let registry = Registry::new();

let opts = EvalOptions::new()
    .max_node_evaluations(10_000)
    .max_iterations(1_000)
    .lenient(true);

let result = render_with_options(
    "Hello, {{global:missing}}!",
    &mut ctx,
    &registry,
    opts,
).unwrap();
assert_eq!(result, "Hello, {{global:missing}}!");

Re-exports§

pub use ast::span::Span;
pub use ast::span::Spanned;
pub use ast::template::Template;
pub use ast::value::Value;
pub use error::EvalError;
pub use error::EvalErrorKind;
pub use error::ParseError;
pub use eval::EvalContext;
pub use eval::EvalOptions;
pub use eval::SimpleContext;
pub use eval::eval_expr_value;
pub use eval::evaluate;
pub use eval::evaluate_with_options;
pub use registry::ClosureCommand;
pub use registry::ClosureProcessor;
pub use registry::Registry;
pub use registry::WeaverCommand;
pub use registry::WeaverProcessor;
pub use macros;

Modules§

ast
Abstract syntax tree types for weaver-lang.
error
Error types for parsing and evaluation.
eval
Template evaluation engine.
registry
Command and processor registration for weaver-lang.

Structs§

CompiledExpr
A parsed expression that can be evaluated multiple times without re-parsing.
CompiledTemplate
A parsed template that can be evaluated multiple times without re-parsing.

Enums§

RenderError
Combined error type returned by render and render_with_options.

Functions§

parse
Parse source text into a Template AST.
parse_expr
Parse a standalone expression from source text.
render
Parse source text and evaluate it in a single step.
render_with_options
Parse source text and evaluate it with custom options.