wayver's git archive


an obsidian renderer
git clone https://git.wayver.dev/sable

sable-frontmatter/src/yaml.rs@2b84405277e54ab809e328cf0237374d4b4dbd0c

raw
Date Commit Message Author Files + -
2026-02-23 01:55 initial mvp wayverd 139 17808 0
...

1//! Parse YAML frontmatter
2
3use miette::SourceOffset;
4
5use crate::{Metadata, parse_simple};
6
7/// YAML parse error
8#[derive(Debug, miette::Diagnostic, thiserror::Error)]
9#[error("failed to deserialize yaml frontmatter")]
10pub struct Error {
11    /// The 'source' of the frontmatter.
12    #[source_code]
13    src: String,
14    /// The location where [`serde_yaml`] failed to parse.
15    #[label("{err}")]
16    location: Option<SourceOffset>,
17
18    /// The error emitted.
19    #[source]
20    err: serde_yaml::Error,
21}
22
23/// Locates start and end `---` and attempts to parse the contents.
24///
25/// # Errors
26///
27/// This will fail if the contents is not valid YAML.
28pub fn parse(data: &str) -> Result<(Option<Metadata>, &str), Error> {
29    let (frontmatter, contents) = match parse_simple(data, "---") {
30        (Some(frontmatter), contents) => (frontmatter, contents),
31        (None, contents) => return Ok((None, contents)),
32    };
33
34    let parsed = match serde_yaml::from_str(frontmatter) {
35        Ok(parsed) => parsed,
36        Err(err) => {
37            return Err(Error {
38                src: frontmatter.to_string(),
39                location: err
40                    .location()
41                    .map(|loc| SourceOffset::from_location(frontmatter, loc.line(), loc.column())),
42                err,
43            });
44        }
45    };
46
47    Ok((Some(parsed), contents))
48}
49