1use std::path::PathBuf;
13
14use serde::Serialize;
15
16#[derive(Debug, Clone, Serialize)]
18pub struct Skill {
19 pub name: String,
21 pub description: String,
23 pub allowed_tools: Vec<String>,
25 pub body: String,
27 pub dir: PathBuf,
29}
30
31impl Skill {
32 pub fn parse_md(content: &str, dir: PathBuf, fallback_name: &str) -> Skill {
36 let (front, body) = split_frontmatter(content);
37 let mut name = None;
38 let mut description = String::new();
39 let mut allowed_tools = Vec::new();
40 for line in front.lines() {
41 let Some((k, v)) = line.split_once(':') else {
42 continue;
43 };
44 let val = v.trim();
45 match k.trim() {
46 "name" if !val.is_empty() => name = Some(unquote(val)),
47 "description" => description = unquote(val),
48 "allowed-tools" | "allowed_tools" => allowed_tools = parse_list(val),
49 _ => {}
50 }
51 }
52 Skill {
53 name: name.unwrap_or_else(|| fallback_name.to_string()),
54 description,
55 allowed_tools,
56 body: body.trim().to_string(),
57 dir,
58 }
59 }
60
61 pub fn summary(&self) -> String {
63 format!("{}: {}", self.name, self.description)
64 }
65}
66
67fn split_frontmatter(content: &str) -> (String, String) {
70 let content = content.trim_start_matches('\u{feff}');
71 if content.trim_start().starts_with("---") {
72 let mut started = false;
73 let mut closed = false;
74 let mut front = String::new();
75 let mut body: Vec<&str> = Vec::new();
76 for line in content.lines() {
77 if !started {
78 if line.trim() == "---" {
79 started = true;
80 }
81 continue;
82 }
83 if !closed {
84 if line.trim() == "---" {
85 closed = true;
86 continue;
87 }
88 front.push_str(line);
89 front.push('\n');
90 } else {
91 body.push(line);
92 }
93 }
94 if closed {
95 return (front, body.join("\n"));
96 }
97 }
98 (String::new(), content.to_string())
99}
100
101fn unquote(s: &str) -> String {
103 let s = s.trim();
104 for q in ['"', '\''] {
105 if s.len() >= 2 && s.starts_with(q) && s.ends_with(q) {
106 return s[1..s.len() - 1].to_string();
107 }
108 }
109 s.to_string()
110}
111
112fn parse_list(s: &str) -> Vec<String> {
114 s.trim()
115 .trim_start_matches('[')
116 .trim_end_matches(']')
117 .split(',')
118 .map(|t| unquote(t.trim()))
119 .filter(|t| !t.is_empty())
120 .collect()
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126
127 #[test]
128 fn parses_frontmatter_and_body() {
129 let md = "---\nname: pdf-forms\ndescription: Fill PDF forms\nallowed-tools: [read, write]\n---\n# Steps\n1. open the pdf\n";
130 let s = Skill::parse_md(md, PathBuf::from("/x"), "folder");
131 assert_eq!(s.name, "pdf-forms");
132 assert_eq!(s.description, "Fill PDF forms");
133 assert_eq!(s.allowed_tools, vec!["read", "write"]);
134 assert!(s.body.starts_with("# Steps"));
135 assert_eq!(s.summary(), "pdf-forms: Fill PDF forms");
136 }
137
138 #[test]
139 fn name_falls_back_to_folder() {
140 let s = Skill::parse_md(
141 "just a body, no frontmatter",
142 PathBuf::from("/x"),
143 "myskill",
144 );
145 assert_eq!(s.name, "myskill");
146 assert_eq!(s.body, "just a body, no frontmatter");
147 assert!(s.description.is_empty());
148 }
149}