Engine Internals

Architecture & Pipeline Specifications

How frontmatter-gen separates zero-copy slice extraction, format detection, DoS-guarded deserialization, and HTML meta tag synthesis.

Frontmatter Gen is architected around a deterministic, four-stage pipeline that strictly decouples zero-copy boundary slicing from typed deserialization and downstream synthesis. Callers that only need to strip or inspect raw frontmatter pay zero heap allocation costs.

Four-Stage Processing Pipeline

01

Boundary Slice

Strip optional UTF-8 BOM (\u{FEFF}) and split (raw_frontmatter, body) as borrowed &str slices in a single forward scan.

Zero Heap Allocations
02

Format Classifier

Classify raw header slices as Format::Yaml, Format::Toml, Format::Json, or Format::Unsupported without regex overhead.

O(n) Lexical Inspection
03

Guarded Parser

Deserialize into Frontmatter via noyalib, toml, or serde_json while enforcing max_depth and max_keys budgets.

Bounded Memory & Depth
04

Meta & SSG Emit

Synthesize entity-escaped HTML <meta> tags, round-trip between formats via to_string, or render Tera templates.

Deterministic Output

Deterministic Pipeline Execution Trace

The trace below illustrates how an input Markdown document flows through the four modules from raw &str slice to escaped HTML meta tags:

pipeline-trace — frontmatter-gen v0.0.15
Input: &str ("---\ntitle: Hello\ncountry: NO\n---\n# Markdown Body")
[Stage 1: extractor::extract_raw_frontmatter] Strip optional UTF-8 BOM • Locate closing --- fence
[Stage 1: zero-copy output] raw = "title: Hello\ncountry: NO" | body = "\n# Markdown Body" (0 heap allocs)
[Stage 2: extractor::detect_format] Heuristic scan -> Format::Yaml
[Stage 3: parser::parse_with_options] noyalib YAML 1.2 parse • Check max_depth & max_keys invariants
[Stage 4: meta::generate_metatags] Emit escaped <meta name="title"> and <meta name="country" content="NO">
Status: OK • Extractor Latency: ~120 ns • Unsafe Blocks: 0

Stage 1: Zero-Copy Boundary Slicing (src/extractor.rs)

extractor::extract_raw_frontmatter(content: &str) -> Result<(&str, &str), ParseError> operates directly on the caller's UTF-8 string slice:

  • UTF-8 BOM Handling: Automatically strips a leading \u{FEFF} byte-order mark before inspecting opening delimiters.
  • Fence Scanning: Recognizes --- (YAML) and +++ (TOML) across both LF (\n) and CRLF (\r\n) line endings.
  • Balanced JSON Braces: Tracks nesting depth for leading { ... } blocks while ignoring braces inside string literals and escape sequences.

Stage 2 & 3: Format Detection & Budgeted Parsing (src/parser.rs)

Once extracted, detect_format routes the raw slice to one of three pure-Rust parsers while ParseOptions enforces structural invariants:

  • YAML 1.2 Engine (noyalib): Preserves explicit YAML tags as Value::Tagged(tag, Box<Value>) and avoids YAML 1.1 boolean coercion.
  • TOML 1.1 Engine (toml): Converts TOML tables and native ISO 8601 datetimes into canonical Frontmatter entries.
  • JSON Engine (serde_json): Parses RFC 8259 JSON objects with exact integer and floating-point preservation.
  • DoS Budget Checks: check_depth and count_keys validate nesting depth and total key counts against ParseOptions.
Frontmatter Gen pipeline architecture and deterministic execution
Modular compilation: zero-copy slicing, YAML 1.2 / TOML 1.1 / JSON parsing, and opt-in CLI and SSG feature flags.

Stage 4: Meta Synthesis & Feature Isolation

To keep compile times and dependency trees minimal for library consumers, heavy application dependencies are gated behind Cargo features:

Feature Flag Default Included Modules & Dependencies
default = [] Yes Core library (extractor, parser, meta, error, noyalib, toml, serde_json).
cli Opt-in Adds the fmg binary target, clap CLI parser, tokio async runtime, and structured logging.
ssg Opt-in Adds config, engine, fs, markdown, and template modules (tera + comrak).