Skip to main content

rust_bucket/
templates.rs

1// Embedded template management
2
3use rust_embed::RustEmbed;
4use std::fs;
5use std::path::PathBuf;
6use tempfile::TempDir;
7use thiserror::Error;
8
9/// Error type for template operations
10#[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/// Embedded templates from the templates/ directory
26#[derive(RustEmbed)]
27#[folder = "templates/"]
28pub struct Templates;
29
30/// Extracts all embedded templates to a temporary directory.
31///
32/// Returns the path to the temporary directory containing all extracted templates.
33/// The temporary directory will be cleaned up when the returned `TempDir` is dropped.
34///
35/// # Errors
36///
37/// Returns `TemplateError` if:
38/// - The temporary directory cannot be created
39/// - Any template file cannot be extracted or written
40pub 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        // Create parent directories if needed
51        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        // Write the file
59        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
70/// Returns the .gitignore entries that rust-bucket requires in the target repository.
71pub fn required_gitignore_lines() -> Vec<&'static str> {
72    vec![
73        ".beads/.br_history/",
74        ".beads/beads.db",
75        ".beads/beads.db-wal",
76    ]
77}
78
79pub fn managed_files() -> Vec<&'static str> {
80    vec![
81        "AGENTS.md",
82        "CLAUDE.md", // symlink to AGENTS.md, created separately
83        "RUST_STYLE_GUIDE.md",
84        "TESTING.md",
85        ".claude/agents/coordinator.md",
86        ".claude/agents/coding.md",
87        ".claude/agents/judge.md",
88        ".claude/agents/tidy.md",
89        ".claude/agents/reflection.md",
90        ".config/nextest.toml",
91        "deny.toml",
92        "rustfmt.toml",
93        ".devcontainer/Dockerfile",
94        ".devcontainer/devcontainer.json",
95        ".beads/config.yaml",
96        "justfile-rustbucket",
97    ]
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn test_extract_to_temp() {
106        let result = extract_to_temp();
107        assert!(
108            result.is_ok(),
109            "Failed to extract templates: {:?}",
110            result.err()
111        );
112
113        let (_temp_dir, temp_path) = result.unwrap();
114        assert!(temp_path.exists());
115        assert!(temp_path.is_dir());
116    }
117
118    #[test]
119    fn test_managed_files_not_empty() {
120        let files = managed_files();
121        assert!(!files.is_empty());
122        assert_eq!(files.len(), 16);
123    }
124
125    #[test]
126    fn test_managed_files_includes_expected() {
127        let files = managed_files();
128        assert!(files.contains(&"AGENTS.md"));
129        assert!(files.contains(&"RUST_STYLE_GUIDE.md"));
130        assert!(files.contains(&".config/nextest.toml"));
131        assert!(files.contains(&".devcontainer/Dockerfile"));
132    }
133}