| Crates.io | gray_matter |
| lib.rs | gray_matter |
| version | 0.3.2 |
| created_at | 2021-02-07 09:01:02.793168+00 |
| updated_at | 2025-07-10 12:05:45.208741+00 |
| description | Smart front matter parser. An implementation of gray-matter in rust. Parses YAML, JSON, TOML and support for custom parsers. |
| homepage | https://github.com/the-alchemists-of-arland/gray-matter-rs |
| repository | https://github.com/the-alchemists-of-arland/gray-matter-rs |
| max_upload_size | |
| id | 351847 |
| size | 112,730 |
gray_matter is a tool for easily extracting front matter out of a string. It is a fast Rust implementation of gray-matter. It supports the following front matter formats:
It also has an Engine trait interface for implementing your own parsers that work with gray_matter.
gray_matter as a dependencyAppend this crate to the Cargo.toml:
[dependencies]
# other dependencies...
gray_matter = "0.3"
use gray_matter::{Matter, ParsedEntity, Result};
use gray_matter::engine::YAML;
use serde::Deserialize;
const INPUT: &str = r#"---
title: gray-matter-rs
tags:
- gray-matter
- rust
---
Some excerpt
---
Other stuff
"#;
fn main() -> Result<()> {
// Select one parser engine, such as YAML, and parse it
// into gray_matter's custom data type: `Pod`
let matter = Matter::<YAML>::new();
let result: ParsedEntity = matter.parse(INPUT)?;
// You can now inspect the data from gray_matter.
assert_eq!(result.content, "Some excerpt\n---\nOther stuff");
assert_eq!(result.excerpt, Some("Some excerpt".to_owned()));
assert_eq!(result.data.as_ref().unwrap()["title"].as_string(), Ok("gray-matter-rs".to_string()));
assert_eq!(result.data.as_ref().unwrap()["tags"][0].as_string(), Ok("gray-matter".to_string()));
assert_eq!(result.data.as_ref().unwrap()["tags"][1].as_string(), Ok("rust".to_string()));
// The default `Pod` data type can be a bit unwieldy, so
// you can also deserialize it into a custom struct
#[derive(Deserialize, Debug)]
struct FrontMatter {
title: String,
tags: Vec<String>
}
let result_with_struct = matter.parse::<FrontMatter>(INPUT)?;
println!("{:?}", result_with_struct.data);
// FrontMatter { title: "gray-matter-rs", tags: ["gray-matter", "rust"] }
Ok(())
}
The default delimiter is ---, both for front matter and excerpts. You can change this by modifiying the Matter struct.
use gray_matter::{Matter, ParsedEntity, Result};
use gray_matter::engine::YAML;
use serde::Deserialize;
fn main() -> Result<()> {
let mut matter: Matter<YAML> = Matter::new();
matter.delimiter = "~~~".to_owned();
matter.excerpt_delimiter = Some("<!-- endexcerpt -->".to_owned());
#[derive(Deserialize, Debug)]
struct FrontMatter {
abc: String,
}
let result: ParsedEntity<FrontMatter> = matter.parse(
"~~~\nabc: xyz\n~~~\nfoo\nbar\nbaz\n<!-- endexcerpt -->\ncontent",
)?;
Ok(())
}
The open and close delimiter are the same by default (---). You can change this by modifiying close_delimiter property of Matter struct
use gray_matter::{Matter, ParsedEntity, Result};
use gray_matter::engine::YAML;
use serde::Deserialize;
fn main() -> Result<()> {
let mut matter: Matter<YAML> = Matter::new();
matter.delimiter = "<!--".to_owned();
matter.close_delimiter = Some("-->".to_owned());
matter.excerpt_delimiter = Some("<!-- endexcerpt -->".to_owned());
#[derive(Deserialize, Debug)]
struct FrontMatter {
abc: String,
}
let result: ParsedEntity<FrontMatter> = matter.parse(
"<!--\nabc: xyz\n-->\nfoo\nbar\nbaz\n<!-- endexcerpt -->\ncontent",
)?;
Ok(())
}
If you need more parser engines, feel free to create a PR to help me complete this crate.