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 list of all files that rust-bucket manages in the target repository.
71///
72/// These are the output filenames (not the template names), representing the files
73/// that will be generated or updated by rust-bucket.
74pub fn required_gitignore_lines() -> Vec<&'static str> {
75    vec![
76        ".beads/.br_history/",
77        ".beads/beads.db",
78        ".beads/beads.db-wal",
79    ]
80}
81
82pub fn managed_files() -> Vec<&'static str> {
83    vec![
84        "AGENTS.md",
85        "CLAUDE.md", // symlink to AGENTS.md, created separately
86        "RUST_STYLE_GUIDE.md",
87        "TESTING.md",
88        ".claude/agents/coordinator.md",
89        ".claude/agents/coding.md",
90        ".claude/agents/judge.md",
91        ".claude/agents/tidy.md",
92        ".claude/agents/reflection.md",
93        ".config/nextest.toml",
94        "deny.toml",
95        "rustfmt.toml",
96        ".devcontainer/Dockerfile",
97        ".devcontainer/devcontainer.json",
98        ".beads/config.yaml",
99        "justfile-rustbucket",
100    ]
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn test_extract_to_temp() {
109        let result = extract_to_temp();
110        assert!(
111            result.is_ok(),
112            "Failed to extract templates: {:?}",
113            result.err()
114        );
115
116        let (_temp_dir, temp_path) = result.unwrap();
117        assert!(temp_path.exists());
118        assert!(temp_path.is_dir());
119    }
120
121    #[test]
122    fn test_managed_files_not_empty() {
123        let files = managed_files();
124        assert!(!files.is_empty());
125        assert_eq!(files.len(), 16);
126    }
127
128    #[test]
129    fn test_managed_files_includes_expected() {
130        let files = managed_files();
131        assert!(files.contains(&"AGENTS.md"));
132        assert!(files.contains(&"RUST_STYLE_GUIDE.md"));
133        assert!(files.contains(&".config/nextest.toml"));
134        assert!(files.contains(&".devcontainer/Dockerfile"));
135    }
136}