#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UnclosedFrontmatter;
impl core::fmt::Display for UnclosedFrontmatter {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter.write_str(
"it opens a frontmatter block with a `---` line and no later line closes it, \
so nothing in it can be told apart from the prose",
)
}
}
impl std::error::Error for UnclosedFrontmatter {}
pub fn skill_prose(text: &str) -> Result<&str, UnclosedFrontmatter> {
let mut lines = text.split_inclusive('\n');
let Some(first) = lines.next() else {
return Ok(text);
};
if !is_fence(first) {
return Ok(text);
}
let mut consumed = first.len();
for line in lines {
consumed += line.len();
if is_fence(line) {
return Ok(&text[consumed..]);
}
}
Err(UnclosedFrontmatter)
}
fn is_fence(line: &str) -> bool {
let bare = line
.strip_suffix('\n')
.map_or(line, |rest| rest.strip_suffix('\r').unwrap_or(rest));
bare == "---"
}