Skip to main content

walrus_core/
utils.rs

1//! Shared utilities for markdown-based configuration parsing.
2
3/// Split YAML frontmatter from the body. Frontmatter is delimited by `---`.
4///
5/// Handles CRLF line endings and trailing whitespace on delimiter lines.
6pub fn split_yaml_frontmatter(content: &str) -> anyhow::Result<(&str, &str)> {
7    let content = content.trim_start();
8    if !content.starts_with("---") {
9        anyhow::bail!("missing YAML frontmatter delimiter (---)");
10    }
11
12    // Skip opening delimiter and its trailing newline.
13    let after_first = content[3..].trim_start_matches(['\n', '\r']);
14
15    // Scan line-by-line for the closing `---` delimiter.
16    let mut pos = 0;
17    for line in after_first.lines() {
18        if line.trim() == "---" {
19            let frontmatter = &after_first[..pos].trim_end();
20            let body_start = pos + line.len();
21            // Skip the newline after `---` if present.
22            let body = after_first[body_start..].trim_start_matches(['\n', '\r']);
23            return Ok((frontmatter, body));
24        }
25        pos += line.len() + 1; // +1 for the newline consumed by lines()
26    }
27
28    anyhow::bail!("missing closing YAML frontmatter delimiter (---)")
29}