Developer Reference

Rust API & CLI Reference

Public types, zero-copy extractor functions, budget-guarded parsers, HTML meta tag synthesis, and CLI flags.

Frontmatter Gen exposes a compact, strongly-typed Rust API alongside the fmg command-line binary. Every public function returns explicit Result types (ParseError, MetaError, ConfigError, EngineError) with zero panics on malformed user input. Full generated rustdoc is hosted on docs.rs/frontmatter-gen.

01

Core Library Interface & Value Model

Extract and inspect frontmatter values using the unified Frontmatter map and recursive Value enum across YAML 1.2, TOML 1.1, and JSON:

rust — frontmatter_gen::extract & Value
use frontmatter_gen::{extract, Format, Frontmatter, Value};
use frontmatter_gen::parser::to_string;
 
# Extract Frontmatter map and borrowed Markdown body
let (fm, body): (Frontmatter, &str) = extract(content)?;
 
# Inspect typed scalars, arrays, objects, or YAML 1.2 tags
let title: Option<&str> = fm.get("title").and_then(Value::as_str);
let draft: Option<bool> = fm.get("draft").and_then(Value::as_bool);
let tagged = fm.get("custom").and_then(Value::as_tagged);
 
# Serialize Frontmatter into YAML, TOML, or JSON
let json_out: String = to_string(&fm, Format::Json)?;

Core types and helper methods exported at the crate root:

Type / Method Signature Description
extract fn(&str) -> Result<(Frontmatter, &str), ParseError> Extracts and parses frontmatter in one pass, returning the parsed map and borrowed body slice.
Frontmatter pub struct Frontmatter(pub HashMap<String, Value>) Case-sensitive map implementing Deref and DerefMut to HashMap<String, Value>.
Value Null | Bool | Number | String | Array | Object | Tagged Format-agnostic recursive value enum with Value::Tagged(String, Box<Value>) for YAML 1.2 tags.
Value::as_str / as_bool fn(&self) -> Option<...> Borrows the underlying string, boolean, f64, i64, u64, array, object, or tagged pair.
Format Yaml | Toml | Json | Unsupported Identifies the detected or target serialization format for frontmatter blocks.
02

Zero-Copy Extractor & Budgeted Parser Modules

The extractor, parser, and meta modules give fine-grained control over zero-copy slicing, structural DoS budgets, and HTML meta tag generation:

Module Function Signature Purpose & Description
extractor::extract_raw_frontmatter fn(&str) -> Result<(&str, &str), ParseError> Zero-copy split returning (raw_frontmatter, body) borrowed directly from content.
extractor::detect_format fn(&str) -> Format Classifies raw frontmatter as Format::Json, Format::Toml, Format::Yaml, or Format::Unsupported.
parser::parse fn(&str, Format) -> Result<Frontmatter, ParseError> Parses a raw frontmatter slice using default ParseOptions.
parser::parse_with_options fn(&str, Format, &ParseOptions) -> Result<Frontmatter, ParseError> Parses raw frontmatter while enforcing validate, max_depth, and max_keys limits.
parser::to_string fn(&Frontmatter, Format) -> Result<String, ParseError> Serializes a Frontmatter map into YAML, TOML, or pretty-printed JSON.
meta::generate_metatags fn(&Frontmatter) -> MetaResult<String> Synthesizes entity-escaped HTML <meta>, Open Graph (og:*), and Twitter Card tags.
03

Command-Line Interface (fmg) & Error Diagnostics

Compile with --features cli to use the fmg binary for extracting metadata or building static sites:

bash — fmg subcommands
# Extract frontmatter from an input file in JSON, YAML, or TOML format
$fmg extract <INPUT> [--format json|yaml|toml] [--output <FILE>]
 
# Build a static site from Markdown content and Tera templates (requires --features cli,ssg)
$fmg build --content-dir <DIR> --output-dir <DIR> [--template-dir <DIR>] [--base-url <URL>]

Structured error types returned across the crate:

Error Type Module Description
error::Error frontmatter_gen::error Crate-wide error enum covering extraction, parsing (`yaml_line`, `toml_line`), and I/O errors.
error::ParseError frontmatter_gen::error Returned by extract, extractor, and parser for missing fences, format errors, or budget violations.
meta::MetaError frontmatter_gen::meta Returned when a frontmatter key or value fails HTML meta tag validation.
config::ConfigError frontmatter_gen::config Returned when ConfigBuilder validation fails (empty site name or invalid directory paths).
04

Stability & SemVer Contract

In addition to Rust type and function signature stability, any change to the observable output of extract_metadata or generate_metatags for existing inputs is treated as a breaking change.

Deterministic Rust API and CLI architecture for frontmatter-gen
Predictable API contract: zero-copy borrowing, budget-guarded parsing, and deterministic HTML meta tag emission.