use frontmatter_gen::error::Error;
use frontmatter_gen::{extract, to_format, Format, Frontmatter, Value};
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("\n๐งช FrontMatterGen Library Examples\n");
extract_example()?;
to_format_example()?;
#[cfg(feature = "ssg")]
ssg_examples()?;
println!("\n๐ All library examples completed successfully!");
Ok(())
}
fn extract_example() -> Result<(), Error> {
println!("๐ฆ Frontmatter Extraction Example");
println!("---------------------------------------------");
let content = r#"---
title: My Post
date: 2025-09-09
tags:
- rust
- example
---
Content here"#;
let (frontmatter, remaining_content) = extract(content)?;
println!(" โ
Extracted frontmatter: {:?}", frontmatter);
println!(" Remaining content: {}", remaining_content);
assert_eq!(
frontmatter.get("title").unwrap().as_str().unwrap(),
"My Post"
);
assert_eq!(remaining_content, "Content here");
Ok(())
}
fn to_format_example() -> Result<(), Error> {
println!("\n๐ฆ Frontmatter Conversion Example");
println!("---------------------------------------------");
let mut frontmatter = Frontmatter::new();
let _ = frontmatter.insert("title".to_string(), "My Post".into());
let _ = frontmatter.insert("date".to_string(), "2025-09-09".into());
let tags = vec![Value::from("rust"), Value::from("example")];
let _ = frontmatter.insert("tags".to_string(), Value::Array(tags));
let yaml = to_format(&frontmatter, Format::Yaml)?;
println!(" โ
Converted frontmatter to YAML:\n{}", yaml);
let json = to_format(&frontmatter, Format::Json)?;
println!(" โ
Converted frontmatter to JSON:\n{}", json);
let toml = to_format(&frontmatter, Format::Toml)?;
println!(" โ
Converted frontmatter to TOML:\n{}", toml);
assert!(yaml.contains("title: My Post"));
assert!(yaml.contains("date: '2025-09-09'"));
assert!(yaml.contains("tags:\n- rust\n- example"));
println!(" Debug: JSON output is:\n{}", json);
assert!(json.contains("\"title\":\"My Post\"")); assert!(json.contains("\"date\":\"2025-09-09\""));
assert!(json.contains("\"tags\":[\"rust\",\"example\"]"));
assert!(toml.contains("title = \"My Post\""));
assert!(toml.contains("date = \"2025-09-09\""));
assert!(toml.contains("tags = [\"rust\", \"example\"]"));
Ok(())
}
#[cfg(feature = "ssg")]
fn ssg_examples() -> Result<(), Error> {
println!("\n๐ฆ SSG-Specific Frontmatter Examples");
println!("---------------------------------------------");
let content = r#"---
title: My Blog Post
date: 2025-09-09
template: blog
layout: post
tags:
- rust
- ssg
---
# Blog Content Here"#;
let (frontmatter, remaining_content) = extract(content)?;
println!(" โ
Extracted SSG frontmatter: {:?}", frontmatter);
println!(" Remaining content: {}", remaining_content);
let yaml = to_format(&frontmatter, Format::Yaml)?;
let toml = to_format(&frontmatter, Format::Toml)?;
let json = to_format(&frontmatter, Format::Json)?;
println!("\n Converted Formats:");
println!(" YAML:\n{}", yaml);
println!(" TOML:\n{}", toml);
println!(" JSON:\n{}", json);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_extraction() -> Result<(), Error> {
let content = r#"---
title: Test
---
Content"#;
let (frontmatter, content) = extract(content)?;
assert_eq!(
frontmatter.get("title").unwrap().as_str().unwrap(),
"Test"
);
assert_eq!(content, "Content");
Ok(())
}
#[test]
fn test_format_conversion() -> Result<(), Error> {
let mut frontmatter = Frontmatter::new();
frontmatter.insert("title".to_string(), "Test".into());
let yaml = to_format(&frontmatter, Format::Yaml)?;
assert!(yaml.contains("title: Test"));
let json = to_format(&frontmatter, Format::Json)?;
assert!(json.contains("\"title\": \"Test\""));
let toml = to_format(&frontmatter, Format::Toml)?;
assert!(toml.contains("title = \"Test\""));
Ok(())
}
#[cfg(feature = "ssg")]
#[test]
fn test_ssg_metadata() -> Result<(), Error> {
let content = r#"---
title: Test
template: post
layout: blog
tags:
- rust
- ssg
---
Content"#;
let (frontmatter, _) = extract(content)?;
assert!(frontmatter.get("template").is_some());
assert!(frontmatter.get("layout").is_some());
assert!(frontmatter.get("tags").is_some());
Ok(())
}
}