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
Boundary Slice
Strip optional UTF-8 BOM (\u{FEFF}) and split (raw_frontmatter, body) as borrowed &str slices in a single forward scan.
Format Classifier
Classify raw header slices as Format::Yaml, Format::Toml, Format::Json, or Format::Unsupported without regex overhead.
Guarded Parser
Deserialize into Frontmatter via noyalib, toml, or serde_json while enforcing max_depth and max_keys budgets.
Meta & SSG Emit
Synthesize entity-escaped HTML <meta> tags, round-trip between formats via to_string, or render Tera templates.
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:
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 asValue::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 canonicalFrontmatterentries. - JSON Engine (
serde_json): Parses RFC 8259 JSON objects with exact integer and floating-point preservation. - DoS Budget Checks:
check_depthandcount_keysvalidate nesting depth and total key counts againstParseOptions.
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). |