Skip to main content

extract_typed

Function extract_typed 

Source
pub fn extract_typed<T: DeserializeOwned + 'static>(
    content: &str,
) -> Result<T, MetadataError>
Expand description

Deserialises the front matter into a typed value.

Where extract_metadata flattens everything to strings, this hands the raw block to the format’s own serde deserialiser, so integers stay integers, sequences stay sequences and nested tables become nested structs. The document body is ignored.

§Errors

MetadataError::ExtractionError when no front matter is found; the format’s parse error (YamlError, TomlError, JsonError) when the block does not deserialise into T.

§Example

use metadata_gen::metadata::extract_typed;

#[derive(serde::Deserialize)]
struct Front { title: String, tags: Vec<String>, draft: bool }

let doc = "---\ntitle: T\ntags: [a, b]\ndraft: false\n---\nBody";
let front: Front = extract_typed(doc).unwrap();
assert_eq!(front.tags, ["a", "b"]);
assert!(!front.draft);