Crates.io | frontmatter-gen |
lib.rs | frontmatter-gen |
version | 0.0.2 |
source | src |
created_at | 2024-10-03 21:47:01.216273 |
updated_at | 2024-10-03 22:33:53.229145 |
description | A Rust library for generating and parsing frontmatter in various formats. |
homepage | https://frontmatter-gen.com/ |
repository | https://github.com/sebastienrousseau/frontmatter-gen |
max_upload_size | |
id | 1395706 |
size | 188,222 |
A robust Rust library for parsing and serializing frontmatter in various formats, including YAML, TOML, and JSON.
• Website • Documentation • Report Bug • Request Feature • Contributing Guidelines
frontmatter-gen
is a flexible Rust library that provides functionality for extracting, parsing, and serializing frontmatter in various formats. It's designed for use in static site generators, content management systems, and any application that needs to handle metadata at the beginning of content files.
Value
enum for type-safe frontmatter data manipulation.Add this to your Cargo.toml
:
[dependencies]
frontmatter-gen = "0.0.2"
Here are some examples of how to use the library:
use frontmatter_gen::extract;
let content = r#"---
title: My Post
date: 2023-05-20
---
Content here"#;
let (frontmatter, remaining_content) = extract(content).unwrap();
assert_eq!(frontmatter.get("title").unwrap().as_str().unwrap(), "My Post");
assert_eq!(remaining_content, "Content here");
use frontmatter_gen::{Frontmatter, Format, Value, to_format};
let mut frontmatter = Frontmatter::new();
frontmatter.insert("title".to_string(), Value::String("My Post".to_string()));
frontmatter.insert("date".to_string(), Value::String("2023-05-20".to_string()));
let yaml = to_format(&frontmatter, Format::Yaml).unwrap();
assert!(yaml.contains("title: My Post"));
assert!(yaml.contains("date: '2023-05-20'"));
use frontmatter_gen::{parser, Format};
let yaml = "title: My Post\ndate: 2023-05-20\n";
let frontmatter = parser::parse(yaml, Format::Yaml).unwrap();
let toml = "title = \"My Post\"\ndate = 2023-05-20\n";
let frontmatter = parser::parse(toml, Format::Toml).unwrap();
let json = r#"{"title": "My Post", "date": "2023-05-20"}"#;
let frontmatter = parser::parse(json, Format::Json).unwrap();
The library provides comprehensive error handling through the FrontmatterError
enum:
use frontmatter_gen::error::FrontmatterError;
fn example_usage() -> Result<(), FrontmatterError> {
let invalid_toml = "invalid toml content";
match toml::from_str::<toml::Value>(invalid_toml) {
Ok(_) => Ok(()),
Err(e) => Err(FrontmatterError::TomlParseError(e)),
}
}
For full API documentation, please visit docs.rs/frontmatter-gen.
To run the examples, clone the repository and use the following command:
cargo run --example example_name
Available examples:
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under either of
at your option.
Special thanks to all contributors who have helped build the frontmatter-gen
library.