// `SKILL.md` 解析。只解析简单 YAML frontmatter(key: value)与正文;
// 不实现完整 YAML,遇到嵌套结构时保留原文并在报告中标注未知。
///|
pub(all) struct SkillSpec {
name : String
description : String
allowed_tools : Array[String]
body : String
}
///|
fn parse_tool_list(value : String) -> Array[String] {
let items : Array[String] = []
let inner = match value.split_once("[") {
Some((_, rest)) =>
match rest.split_once("]") {
Some((body, _)) => body.to_owned()
None => value
}
None => value
}
for part in inner.split(",") {
let item = part.trim().to_owned()
if item != "" {
items.push(item)
}
}
items
}
///|
pub fn parse_skill_md(text : String) -> SkillSpec {
let lines = text.split("\n").to_array()
let frontmatter : Array[String] = []
let body_lines : Array[String] = []
let mut in_front = false
let mut front_done = false
let mut first = true
for line_view in lines {
let line = line_view.to_owned()
let trimmed = line.trim()
if first {
first = false
if trimmed == "---" {
in_front = true
continue
}
}
if in_front && !front_done {
if trimmed == "---" {
in_front = false
front_done = true
continue
}
frontmatter.push(trimmed.to_owned())
continue
}
body_lines.push(line)
}
let mut name = ""
let mut description = ""
let mut allowed_tools : Array[String] = []
for line in frontmatter {
match line.split_once(":") {
Some((key, value)) => {
let key_text = key.trim().to_owned()
let value_text = value.trim().to_owned()
if key_text == "name" {
name = value_text
} else if key_text == "description" {
description = value_text
} else if key_text == "allowed-tools" || key_text == "allowed_tools" {
allowed_tools = parse_tool_list(value_text)
}
}
None => ()
}
}
if name == "" {
name = "unnamed-skill"
}
let buffer = StringBuilder()
for line in body_lines {
buffer.write_stringview(line[:])
buffer.write_stringview("\n"[:])
}
return { name, description, allowed_tools, body: buffer.to_string(), }
}