Skip to main content

ralph/template/
builtin.rs

1//! Built-in task templates for common patterns.
2//!
3//! Responsibilities:
4//! - Define embedded JSON templates for standard task types.
5//! - Provide lookup functions for built-in templates by name.
6//!
7//! Not handled here:
8//! - Custom template loading from filesystem (see `loader.rs`).
9//! - Template merging with user options (see `merge.rs`).
10//!
11//! Invariants/assumptions:
12//! - Template JSON is valid and parses to Task structs.
13//! - Template names are lowercase ASCII without spaces.
14
15/// Built-in bug fix template
16pub const BUG_TEMPLATE: &str = r#"{
17  "id": "",
18  "title": "",
19  "status": "todo",
20  "priority": "high",
21  "tags": ["bug", "fix"],
22  "plan": [
23    "Reproduce the issue",
24    "Identify root cause",
25    "Implement fix",
26    "Add regression test",
27    "Verify fix with make ci"
28  ],
29  "evidence": []
30}"#;
31
32/// Built-in feature template
33pub const FEATURE_TEMPLATE: &str = r#"{
34  "id": "",
35  "title": "",
36  "status": "draft",
37  "priority": "medium",
38  "tags": ["feature", "enhancement"],
39  "plan": [
40    "Design the feature interface",
41    "Implement core functionality",
42    "Add tests",
43    "Update documentation",
44    "Run make ci"
45  ],
46  "evidence": []
47}"#;
48
49/// Built-in refactor template
50pub const REFACTOR_TEMPLATE: &str = r#"{
51  "id": "",
52  "title": "",
53  "status": "todo",
54  "priority": "medium",
55  "tags": ["refactor", "cleanup"],
56  "plan": [
57    "Analyze current implementation",
58    "Identify improvement opportunities",
59    "Refactor with tests passing",
60    "Verify no behavior changes",
61    "Run make ci"
62  ],
63  "evidence": []
64}"#;
65
66/// Built-in test template
67pub const TEST_TEMPLATE: &str = r#"{
68  "id": "",
69  "title": "",
70  "status": "todo",
71  "priority": "high",
72  "tags": ["test", "coverage"],
73  "plan": [
74    "Identify untested scenarios",
75    "Write test cases",
76    "Ensure tests fail before fix",
77    "Implement/fix as needed",
78    "Verify coverage with make ci"
79  ],
80  "evidence": []
81}"#;
82
83/// Built-in documentation template
84pub const DOCS_TEMPLATE: &str = r#"{
85  "id": "",
86  "title": "",
87  "status": "todo",
88  "priority": "low",
89  "tags": ["docs", "documentation"],
90  "plan": [
91    "Identify documentation gaps",
92    "Write clear explanations",
93    "Add code examples",
94    "Review for accuracy",
95    "Update related docs"
96  ],
97  "evidence": []
98}"#;
99
100/// Built-in add-tests template - for adding tests to existing code
101pub const ADD_TESTS_TEMPLATE: &str = r#"{
102  "id": "",
103  "title": "Add tests for {{target}}",
104  "status": "todo",
105  "priority": "high",
106  "tags": ["test", "coverage", "quality"],
107  "scope": ["{{target}}"],
108  "plan": [
109    "Analyze {{target}} to understand functionality",
110    "Identify test scenarios and edge cases",
111    "Write unit tests for {{module}}",
112    "Write integration tests if applicable",
113    "Verify test coverage with cargo tarpaulin or similar",
114    "Run make ci to ensure all tests pass"
115  ],
116  "evidence": [
117    "Current test coverage gaps in {{target}}",
118    "Functionality that needs testing"
119  ]
120}"#;
121
122/// Built-in refactor-performance template - for performance optimization
123pub const REFACTOR_PERFORMANCE_TEMPLATE: &str = r#"{
124  "id": "",
125  "title": "Optimize performance of {{target}}",
126  "status": "todo",
127  "priority": "medium",
128  "tags": ["refactor", "performance", "optimization"],
129  "scope": ["{{target}}"],
130  "plan": [
131    "Profile current performance of {{target}}",
132    "Identify bottlenecks and hot paths",
133    "Implement targeted optimizations",
134    "Benchmark before/after performance",
135    "Verify correctness is preserved",
136    "Run make ci to validate changes"
137  ],
138  "evidence": [
139    "Performance measurements showing bottleneck",
140    "Profiling data from {{target}}"
141  ]
142}"#;
143
144/// Built-in fix-error-handling template - for improving error handling
145pub const FIX_ERROR_HANDLING_TEMPLATE: &str = r#"{
146  "id": "",
147  "title": "Fix error handling in {{target}}",
148  "status": "todo",
149  "priority": "high",
150  "tags": ["bug", "error-handling", "reliability"],
151  "scope": ["{{target}}"],
152  "plan": [
153    "Audit current error handling in {{target}}",
154    "Identify gaps and anti-patterns",
155    "Implement proper error types with thiserror/anyhow",
156    "Add error context and logging where needed",
157    "Test error paths and edge cases",
158    "Run make ci to validate all error scenarios"
159  ],
160  "evidence": [
161    "Error handling gaps in {{target}}",
162    "Panics or unwraps that should be proper errors"
163  ]
164}"#;
165
166/// Built-in add-docs template - for documentation improvements
167pub const ADD_DOCS_TEMPLATE: &str = r#"{
168  "id": "",
169  "title": "Add documentation for {{target}}",
170  "status": "todo",
171  "priority": "low",
172  "tags": ["docs", "documentation"],
173  "scope": ["{{target}}"],
174  "plan": [
175    "Identify undocumented public APIs in {{target}}",
176    "Add module-level documentation (//!)",
177    "Add function/struct documentation (///)",
178    "Include code examples in doc comments",
179    "Review for accuracy and completeness",
180    "Run make ci to check doc tests"
181  ],
182  "evidence": [
183    "Missing documentation in {{target}}",
184    "Public APIs without doc comments"
185  ]
186}"#;
187
188/// Built-in security-audit template - for security improvements
189pub const SECURITY_AUDIT_TEMPLATE: &str = r#"{
190  "id": "",
191  "title": "Security audit of {{target}}",
192  "status": "todo",
193  "priority": "critical",
194  "tags": ["security", "audit", "compliance"],
195  "scope": ["{{target}}"],
196  "plan": [
197    "Review security-sensitive code in {{target}}",
198    "Check for common vulnerabilities (OWASP top 10)",
199    "Audit input validation and sanitization",
200    "Implement security fixes",
201    "Add security-focused tests",
202    "Run make ci and security scans"
203  ],
204  "evidence": [
205    "Security-sensitive code in {{target}}",
206    "Potential vulnerability indicators"
207  ]
208}"#;
209
210/// Get built-in template by name
211pub fn get_builtin_template(name: &str) -> Option<&'static str> {
212    match name {
213        "bug" => Some(BUG_TEMPLATE),
214        "feature" => Some(FEATURE_TEMPLATE),
215        "refactor" => Some(REFACTOR_TEMPLATE),
216        "test" => Some(TEST_TEMPLATE),
217        "docs" => Some(DOCS_TEMPLATE),
218        "add-tests" => Some(ADD_TESTS_TEMPLATE),
219        "refactor-performance" => Some(REFACTOR_PERFORMANCE_TEMPLATE),
220        "fix-error-handling" => Some(FIX_ERROR_HANDLING_TEMPLATE),
221        "add-docs" => Some(ADD_DOCS_TEMPLATE),
222        "security-audit" => Some(SECURITY_AUDIT_TEMPLATE),
223        _ => None,
224    }
225}
226
227/// List all built-in template names
228pub fn list_builtin_templates() -> Vec<&'static str> {
229    vec![
230        "add-docs",
231        "add-tests",
232        "bug",
233        "docs",
234        "feature",
235        "fix-error-handling",
236        "refactor",
237        "refactor-performance",
238        "security-audit",
239        "test",
240    ]
241}
242
243/// Get a human-readable description for a built-in template
244pub fn get_template_description(name: &str) -> &'static str {
245    match name {
246        "add-docs" => "Add documentation for a specific file or module",
247        "add-tests" => "Add tests for existing code with coverage verification",
248        "bug" => "Bug fix with reproduction steps and regression tests",
249        "docs" => "Documentation update or creation",
250        "feature" => "New feature with design, implementation, and documentation",
251        "fix-error-handling" => "Fix error handling with proper types and context",
252        "refactor" => "Code refactoring with behavior preservation",
253        "refactor-performance" => "Optimize performance with profiling and benchmarking",
254        "security-audit" => "Security audit with vulnerability checks",
255        "test" => "Test addition or improvement",
256        _ => "Task template",
257    }
258}
259
260#[cfg(test)]
261mod tests {
262    use super::*;
263
264    #[test]
265    fn test_get_builtin_template() {
266        assert!(get_builtin_template("bug").is_some());
267        assert!(get_builtin_template("feature").is_some());
268        assert!(get_builtin_template("refactor").is_some());
269        assert!(get_builtin_template("test").is_some());
270        assert!(get_builtin_template("docs").is_some());
271        assert!(get_builtin_template("add-tests").is_some());
272        assert!(get_builtin_template("refactor-performance").is_some());
273        assert!(get_builtin_template("fix-error-handling").is_some());
274        assert!(get_builtin_template("add-docs").is_some());
275        assert!(get_builtin_template("security-audit").is_some());
276        assert!(get_builtin_template("unknown").is_none());
277    }
278
279    #[test]
280    fn test_list_builtin_templates() {
281        let templates = list_builtin_templates();
282        assert_eq!(templates.len(), 10);
283        assert!(templates.contains(&"bug"));
284        assert!(templates.contains(&"feature"));
285        assert!(templates.contains(&"refactor"));
286        assert!(templates.contains(&"test"));
287        assert!(templates.contains(&"docs"));
288        assert!(templates.contains(&"add-tests"));
289        assert!(templates.contains(&"refactor-performance"));
290        assert!(templates.contains(&"fix-error-handling"));
291        assert!(templates.contains(&"add-docs"));
292        assert!(templates.contains(&"security-audit"));
293    }
294
295    #[test]
296    fn test_templates_are_valid_json() {
297        for name in list_builtin_templates() {
298            let template_json = get_builtin_template(name).unwrap();
299            let result: Result<crate::contracts::Task, _> = serde_json::from_str(template_json);
300            assert!(result.is_ok(), "Template {} should be valid JSON", name);
301        }
302    }
303
304    #[test]
305    fn test_get_template_description() {
306        assert!(get_template_description("bug").contains("Bug fix"));
307        assert!(get_template_description("feature").contains("feature"));
308        assert!(get_template_description("add-tests").contains("tests"));
309        assert!(get_template_description("refactor-performance").contains("performance"));
310        assert!(get_template_description("fix-error-handling").contains("error"));
311        assert!(get_template_description("add-docs").contains("documentation"));
312        assert!(get_template_description("security-audit").contains("Security"));
313        assert!(get_template_description("unknown").contains("Task template"));
314    }
315}