1use rust_embed::RustEmbed;
4use std::fs;
5use std::path::PathBuf;
6use tempfile::TempDir;
7use thiserror::Error;
8
9#[derive(Debug, Error)]
11pub enum TemplateError {
12 #[error("Failed to create temporary directory: {0}")]
13 TempDirCreation(#[from] std::io::Error),
14
15 #[error("Failed to extract template file '{path}': {source}")]
16 FileExtraction {
17 path: String,
18 source: std::io::Error,
19 },
20
21 #[error("Template file '{0}' not found in embedded templates")]
22 TemplateNotFound(String),
23}
24
25#[derive(RustEmbed)]
27#[folder = "templates/"]
28pub struct Templates;
29
30pub fn extract_to_temp() -> Result<(TempDir, PathBuf), TemplateError> {
41 let temp_dir = TempDir::new()?;
42 let temp_path = temp_dir.path().to_path_buf();
43
44 for file_path in Templates::iter() {
45 let file_data = Templates::get(&file_path)
46 .ok_or_else(|| TemplateError::TemplateNotFound(file_path.to_string()))?;
47
48 let target_path = temp_path.join(file_path.as_ref());
49
50 if let Some(parent) = target_path.parent() {
52 fs::create_dir_all(parent).map_err(|e| TemplateError::FileExtraction {
53 path: file_path.to_string(),
54 source: e,
55 })?;
56 }
57
58 fs::write(&target_path, file_data.data.as_ref()).map_err(|e| {
60 TemplateError::FileExtraction {
61 path: file_path.to_string(),
62 source: e,
63 }
64 })?;
65 }
66
67 Ok((temp_dir, temp_path))
68}
69
70pub fn managed_files() -> Vec<&'static str> {
75 vec![
76 "AGENTS.md",
77 "CLAUDE.md", "STYLE_GUIDE.md",
79 "TESTING.md",
80 ".claude/agents/coordinator.md",
81 ".claude/agents/coding.md",
82 ".claude/agents/judge.md",
83 ".claude/agents/tidy.md",
84 ".claude/agents/reflection.md",
85 ".config/nextest.toml",
86 "deny.toml",
87 "rustfmt.toml",
88 ".devcontainer/Dockerfile",
89 ".devcontainer/devcontainer.json",
90 ".beads/config.yaml",
91 ]
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_extract_to_temp() {
100 let result = extract_to_temp();
101 assert!(
102 result.is_ok(),
103 "Failed to extract templates: {:?}",
104 result.err()
105 );
106
107 let (_temp_dir, temp_path) = result.unwrap();
108 assert!(temp_path.exists());
109 assert!(temp_path.is_dir());
110 }
111
112 #[test]
113 fn test_managed_files_not_empty() {
114 let files = managed_files();
115 assert!(!files.is_empty());
116 assert_eq!(files.len(), 15);
117 }
118
119 #[test]
120 fn test_managed_files_includes_expected() {
121 let files = managed_files();
122 assert!(files.contains(&"AGENTS.md"));
123 assert!(files.contains(&"STYLE_GUIDE.md"));
124 assert!(files.contains(&".config/nextest.toml"));
125 assert!(files.contains(&".devcontainer/Dockerfile"));
126 }
127}