Skip to main content

mur_common/skill/
store.rs

1use crate::skill::manifest::SkillManifest;
2use crate::skill::parser::{
3    ParseError, parse_canonical, parse_legacy_markdown, parse_markdown, serialize_canonical,
4};
5use std::fs;
6use std::io::{self, Write};
7use std::path::{Path, PathBuf};
8
9#[derive(Debug)]
10pub enum StoreError {
11    Io(io::Error),
12    Parse(ParseError),
13    NotFound(PathBuf),
14}
15
16impl std::fmt::Display for StoreError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            StoreError::Io(e) => write!(f, "io: {e}"),
20            StoreError::Parse(e) => write!(f, "parse: {e}"),
21            StoreError::NotFound(p) => write!(f, "skill not found: {}", p.display()),
22        }
23    }
24}
25
26impl std::error::Error for StoreError {}
27
28impl From<io::Error> for StoreError {
29    fn from(e: io::Error) -> Self {
30        StoreError::Io(e)
31    }
32}
33
34impl From<ParseError> for StoreError {
35    fn from(e: ParseError) -> Self {
36        StoreError::Parse(e)
37    }
38}
39
40/// The shared skill store, `<mur_home>/skills`.
41pub fn global_skills_root(mur_home: &Path) -> PathBuf {
42    mur_home.join("skills")
43}
44
45pub fn global_skill_dir(mur_home: &Path, name: &str) -> PathBuf {
46    global_skills_root(mur_home).join(name)
47}
48
49pub fn agent_skill_dir(mur_home: &Path, agent: &str) -> PathBuf {
50    mur_home.join("agents").join(agent).join("skills")
51}
52
53pub fn read_from_dir(dir: &Path) -> Result<SkillManifest, StoreError> {
54    let yaml = dir.join("skill.yaml");
55    if yaml.exists() {
56        let text = fs::read_to_string(&yaml)?;
57        return Ok(parse_canonical(&text)?);
58    }
59    let md = dir.join("skill.md");
60    if md.exists() {
61        let text = fs::read_to_string(&md)?;
62        return Ok(parse_markdown(&text)?);
63    }
64    let legacy = dir.with_extension("md");
65    if legacy.exists() {
66        let text = fs::read_to_string(&legacy)?;
67        return Ok(parse_legacy_markdown(&text)?);
68    }
69    Err(StoreError::NotFound(dir.to_path_buf()))
70}
71
72pub fn write_to_dir(dir: &Path, m: &SkillManifest) -> Result<PathBuf, StoreError> {
73    fs::create_dir_all(dir)?;
74    let final_path = dir.join("skill.yaml");
75    let tmp_path = dir.join(".skill.yaml.tmp");
76    let yaml = serialize_canonical(m)?;
77
78    {
79        let mut f = fs::File::create(&tmp_path)?;
80        f.write_all(yaml.as_bytes())?;
81        f.sync_all()?;
82    }
83
84    #[cfg(unix)]
85    {
86        use std::os::unix::fs::PermissionsExt;
87        fs::set_permissions(&tmp_path, fs::Permissions::from_mode(0o600))?;
88    }
89
90    fs::rename(&tmp_path, &final_path)?;
91    Ok(final_path)
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97    use tempfile::tempdir;
98
99    fn sample() -> SkillManifest {
100        let yaml = r#"
101name: stored
102version: 1.0.0
103publisher: human:t
104description: d
105category: context
106content:
107  abstract: a
108  context: b
109"#;
110        parse_canonical(yaml).unwrap()
111    }
112
113    #[test]
114    fn write_then_read_roundtrips() {
115        let dir = tempdir().unwrap();
116        let path = dir.path().join("stored");
117        write_to_dir(&path, &sample()).unwrap();
118        let read = read_from_dir(&path).unwrap();
119        assert_eq!(read.name, "stored");
120    }
121
122    #[test]
123    fn reads_markdown_when_yaml_missing() {
124        let dir = tempdir().unwrap();
125        let path = dir.path().join("md-skill");
126        fs::create_dir_all(&path).unwrap();
127        let md = "---\nname: md-skill\nversion: 1.0.0\npublisher: human:t\ndescription: d\ncategory: context\n---\n\nBody content\n";
128        fs::write(path.join("skill.md"), md).unwrap();
129        let read = read_from_dir(&path).unwrap();
130        assert_eq!(read.name, "md-skill");
131    }
132
133    #[test]
134    fn legacy_dot_md_path_resolves() {
135        let dir = tempdir().unwrap();
136        let path = dir.path().join("mur-context");
137        let legacy = "---\nname: mur-context\ndescription: d\n---\n\nbody\n";
138        fs::write(dir.path().join("mur-context.md"), legacy).unwrap();
139        let read = read_from_dir(&path).unwrap();
140        assert_eq!(read.name, "mur-context");
141    }
142
143    #[cfg(unix)]
144    #[test]
145    fn written_file_is_0600() {
146        use std::os::unix::fs::PermissionsExt;
147        let dir = tempdir().unwrap();
148        let path = dir.path().join("perm-test");
149        let written = write_to_dir(&path, &sample()).unwrap();
150        let mode = fs::metadata(&written).unwrap().permissions().mode() & 0o777;
151        assert_eq!(mode, 0o600);
152    }
153
154    #[test]
155    fn missing_skill_returns_not_found() {
156        let dir = tempdir().unwrap();
157        let r = read_from_dir(&dir.path().join("missing"));
158        assert!(matches!(r, Err(StoreError::NotFound(_))));
159    }
160}