1#![cfg_attr(feature = "doc", doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#))]
22#![cfg_attr(docsrs, feature(doc_auto_cfg))]
23#![deny(clippy::pedantic)]
24#![deny(missing_docs)]
25
26#[cfg(feature = "json")]
27mod json;
28#[cfg(feature = "json")]
29#[doc(hidden)]
30pub use json::serde_json;
31
32#[cfg(feature = "toml")]
33mod toml;
34#[cfg(feature = "toml")]
35#[doc(hidden)]
36pub use toml::serde_toml;
37
38#[cfg(feature = "yaml")]
39mod yaml;
40#[cfg(feature = "yaml")]
41#[doc(hidden)]
42pub use yaml::serde_yaml;
43
44#[doc(hidden)]
46#[must_use]
47pub fn extract_frontmatter<'s>(source: &'s str, tag: &str) -> Option<&'s str> {
48 let start_pattern = format!("/*{tag}\n");
49 let end_pattern = "\n*/\n";
50 let from_comment_start = source.split_once(&start_pattern)?.1;
51 Some(from_comment_start.split_once(end_pattern)?.0)
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_json_frontmatter_extraction() {
60 let source = r"
61/*json
62{
63}
64*/
65";
66
67 assert_eq!(extract_frontmatter(source, "json"), Some("{\n}"));
68 }
69
70 #[test]
71 fn test_no_frontmatter_found() {
72 let source = r"
73/*
74{
75}
76*/
77";
78
79 assert_eq!(extract_frontmatter(source, "json"), None);
80 }
81}