#![cfg_attr(feature = "doc", doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![deny(clippy::pedantic)]
#![deny(missing_docs)]
#[cfg(feature = "json")]
mod json;
#[cfg(feature = "json")]
#[doc(hidden)]
pub use json::serde_json;
#[cfg(feature = "toml")]
mod toml;
#[cfg(feature = "toml")]
#[doc(hidden)]
pub use toml::serde_toml;
#[cfg(feature = "yaml")]
mod yaml;
#[cfg(feature = "yaml")]
#[doc(hidden)]
pub use yaml::serde_yaml;
#[doc(hidden)]
#[must_use]
pub fn extract_frontmatter<'s>(source: &'s str, tag: &str) -> Option<&'s str> {
let start_pattern = format!("/*{tag}\n");
let end_pattern = "\n*/\n";
let from_comment_start = source.split_once(&start_pattern)?.1;
Some(from_comment_start.split_once(end_pattern)?.0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_json_frontmatter_extraction() {
let source = r"
/*json
{
}
*/
";
assert_eq!(extract_frontmatter(source, "json"), Some("{\n}"));
}
#[test]
fn test_no_frontmatter_found() {
let source = r"
/*
{
}
*/
";
assert_eq!(extract_frontmatter(source, "json"), None);
}
}