include!(concat!(env!("OUT_DIR"), "/skill_assets.rs"));
pub const SKILL_ENTRY: &str = "SKILL.md";
pub fn skill_file(rel: &str) -> Option<&'static str> {
SKILL_FILES
.iter()
.find(|(name, _)| *name == rel)
.map(|(_, body)| *body)
}
pub fn skill_md_raw() -> &'static str {
skill_file(SKILL_ENTRY).unwrap_or("")
}
pub fn skill_md_body() -> &'static str {
strip_frontmatter(skill_md_raw())
}
pub fn skill_description() -> Option<String> {
frontmatter(skill_md_raw()).and_then(|fm| {
fm.lines()
.find_map(|l| l.trim().strip_prefix("description:"))
.map(|v| unquote(v.trim()))
})
}
fn split_frontmatter(s: &str) -> (Option<&str>, &str) {
let s = s.strip_prefix('\u{feff}').unwrap_or(s);
let rest = if let Some(rest) = s.strip_prefix("---\r\n") {
rest
} else if let Some(rest) = s.strip_prefix("---\n") {
rest
} else {
return (None, s);
};
for (line_start, line) in rest.split_inclusive('\n').scan(0usize, |offset, line| {
let line_start = *offset;
*offset += line.len();
Some((line_start, line))
}) {
if line.trim_end_matches(['\r', '\n']) == "---" {
return (Some(&rest[..line_start]), &rest[line_start + line.len()..]);
}
}
(None, s)
}
fn frontmatter(s: &str) -> Option<&str> {
split_frontmatter(s).0
}
fn strip_frontmatter(s: &str) -> &str {
split_frontmatter(s).1
}
fn unquote(s: &str) -> String {
let bytes = s.as_bytes();
if bytes.len() >= 2 {
let first = bytes[0];
let last = bytes[bytes.len() - 1];
if (first == b'"' && last == b'"') || (first == b'\'' && last == b'\'') {
return s[1..s.len() - 1].to_owned();
}
}
s.to_owned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn split_frontmatter_accepts_lf() {
let (fm, body) = split_frontmatter("---\ndescription: test\n---\n# Body\n");
assert_eq!(fm, Some("description: test\n"));
assert_eq!(body, "# Body\n");
}
#[test]
fn split_frontmatter_accepts_crlf() {
let (fm, body) = split_frontmatter("---\r\ndescription: test\r\n---\r\n# Body\r\n");
assert_eq!(fm, Some("description: test\r\n"));
assert_eq!(body, "# Body\r\n");
}
#[test]
fn split_frontmatter_accepts_utf8_bom() {
let (fm, body) = split_frontmatter("\u{feff}---\r\ndescription: test\r\n---\r\n# Body\r\n");
assert_eq!(fm, Some("description: test\r\n"));
assert_eq!(body, "# Body\r\n");
}
#[test]
fn split_frontmatter_leaves_plain_markdown_unchanged() {
let input = "# Body\n---\nnot frontmatter\n";
assert_eq!(split_frontmatter(input), (None, input));
}
}