Function toml_frontmatter::parse

source ·
pub fn parse<'a, D: Deserialize<'a>>(data: &'a str) -> Result<(D, &'a str)>
Expand description

Parse a struct that implements serde::Deserialize from frontmatter and return the remaining contents of the string.

Errors

This function will return an Err when:

  • the data doesn’t have a frontmatter section,
  • the frontmatter isnt’ at the beginning of the data.

Example

#[derive(serde::Deserialize)]
struct Frontmatter {
  date: String,
}

let sample = r#"
---toml
date = "2023-01-01"
---

Some **Markdown**. Or something else!
"#.trim();

let (frontmatter, markdown) = toml_frontmatter::parse::<Frontmatter>(sample).unwrap();