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.
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:
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. |
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. |
Command-Line Interface (fmg) & Error Diagnostics
Compile with --features cli to use the fmg binary for extracting metadata or building static sites:
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). |
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.