frontmatter_gen/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// src/lib.rs
#![doc = include_str!("../README.md")]
#![doc(
html_favicon_url = "https://kura.pro/frontmatter-gen/images/favicon.ico",
html_logo_url = "https://kura.pro/frontmatter-gen/images/logos/frontmatter-gen.svg",
html_root_url = "https://docs.rs/frontmatter-gen"
)]
#![crate_name = "frontmatter_gen"]
#![crate_type = "lib"]
/// The `error` module contains error types related to the frontmatter parsing process.
pub mod error;
/// The `extractor` module contains functions for extracting raw frontmatter from content.
pub mod extractor;
/// The `parser` module contains functions for parsing frontmatter into a structured format.
pub mod parser;
/// The `types` module contains types related to the frontmatter parsing process.
pub mod types;
use error::FrontmatterError;
use extractor::{detect_format, extract_raw_frontmatter};
use parser::{parse, to_string};
// Re-export types for external access
pub use types::{Format, Frontmatter, Value}; // Add `Frontmatter` and `Format` to the public interface
/// Extracts frontmatter from a string of content.
///
/// This function attempts to extract frontmatter from the given content string.
/// It supports YAML, TOML, and JSON formats.
///
/// # Arguments
///
/// * `content` - A string slice containing the content to parse.
///
/// # Returns
///
/// * `Ok((Frontmatter, &str))` - A tuple containing the parsed frontmatter and the remaining content.
/// * `Err(FrontmatterError)` - An error if extraction or parsing fails.
///
/// # Examples
///
/// ```
/// use frontmatter_gen::{extract, Frontmatter};
///
/// let yaml_content = r#"---
/// title: My Post
/// date: 2023-05-20
/// ---
/// Content here"#;
///
/// let (frontmatter, remaining_content) = extract(yaml_content).unwrap();
/// assert_eq!(frontmatter.get("title").unwrap().as_str().unwrap(), "My Post");
/// assert_eq!(remaining_content, "Content here");
/// ```
pub fn extract(
content: &str,
) -> Result<(Frontmatter, &str), FrontmatterError> {
let (raw_frontmatter, remaining_content) =
extract_raw_frontmatter(content)?;
let format = detect_format(raw_frontmatter)?;
let frontmatter = parse(raw_frontmatter, format)?;
Ok((frontmatter, remaining_content))
}
/// Converts frontmatter to a specific format.
///
/// # Arguments
///
/// * `frontmatter` - The Frontmatter to convert.
/// * `format` - The target Format to convert to.
///
/// # Returns
///
/// * `Ok(String)` - The frontmatter converted to the specified format.
/// * `Err(FrontmatterError)` - An error if conversion fails.
///
/// # Examples
///
/// ```
/// use frontmatter_gen::{Frontmatter, Format, to_format};
///
/// let mut frontmatter = Frontmatter::new();
/// frontmatter.insert("title".to_string(), "My Post".into());
/// frontmatter.insert("date".to_string(), "2023-05-20".into());
///
/// let yaml = to_format(&frontmatter, Format::Yaml).unwrap();
/// assert!(yaml.contains("title: My Post"));
/// assert!(yaml.contains("date: '2023-05-20'"));
/// ```
pub fn to_format(
frontmatter: &Frontmatter,
format: Format,
) -> Result<String, FrontmatterError> {
to_string(frontmatter, format)
}