//! Parse YAML frontmatter

use miette::SourceOffset;

use crate::{Metadata, parse_simple};

/// YAML parse error
#[derive(Debug, miette::Diagnostic, thiserror::Error)]
#[error("failed to deserialize yaml frontmatter")]
pub struct Error {
    /// The 'source' of the frontmatter.
    #[source_code]
    src: String,
    /// The location where [`serde_yaml`] failed to parse.
    #[label("{err}")]
    location: Option<SourceOffset>,

    /// The error emitted.
    #[source]
    err: serde_yaml::Error,
}

/// Locates start and end `---` and attempts to parse the contents.
///
/// # Errors
///
/// This will fail if the contents is not valid YAML.
pub fn parse(data: &str) -> Result<(Option<Metadata>, &str), Error> {
    let (frontmatter, contents) = match parse_simple(data, "---") {
        (Some(frontmatter), contents) => (frontmatter, contents),
        (None, contents) => return Ok((None, contents)),
    };

    let parsed = match serde_yaml::from_str(frontmatter) {
        Ok(parsed) => parsed,
        Err(err) => {
            return Err(Error {
                src: frontmatter.to_string(),
                location: err
                    .location()
                    .map(|loc| SourceOffset::from_location(frontmatter, loc.line(), loc.column())),
                err,
            });
        }
    };

    Ok((Some(parsed), contents))
}
