1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! Template context for prompt generation.
//!
//! This module provides the `TemplateContext` struct which holds the
//! template registry and is passed through the application to enable
//! user template overrides.
use super::template_registry::TemplateRegistry;
use std::path::PathBuf;
/// Context for template-based prompt generation.
///
/// Provides access to the template registry for loading user-customizable
/// templates. This context is created from the application config and passed
/// to prompt generation functions.
///
/// # Example
///
/// ```ignore
/// let context = TemplateContext::from_user_templates_dir(Some(PathBuf::from("~/.config/ralph/templates")));
/// let prompt = prompt_developer_iteration_xml_with_context(
/// &context,
/// 1, 5, ContextLevel::Normal, "prompt", "plan"
/// );
/// ```
#[derive(Debug, Clone)]
pub struct TemplateContext {
/// Template registry for loading templates.
pub(crate) registry: TemplateRegistry,
}
impl TemplateContext {
/// Create a new template context with the given registry.
#[must_use]
pub const fn new(registry: TemplateRegistry) -> Self {
Self { registry }
}
/// Create a template context from a config's user templates directory.
///
/// This is the recommended way to create a `TemplateContext` as it
/// respects the user's configured templates directory.
#[must_use]
pub const fn from_user_templates_dir(user_templates_dir: Option<PathBuf>) -> Self {
Self::new(TemplateRegistry::new(user_templates_dir))
}
/// Get a reference to the template registry.
#[must_use]
pub const fn registry(&self) -> &TemplateRegistry {
&self.registry
}
}
impl Default for TemplateContext {
fn default() -> Self {
Self::new(TemplateRegistry::default())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_template_context_creation() {
let context = TemplateContext::new(TemplateRegistry::new(None));
// Should not have user templates since we passed None
assert!(!context
.registry()
.has_user_template("developer_iteration_xml"));
}
#[test]
fn test_template_context_default() {
let context = TemplateContext::default();
// Default should work and access templates
assert!(context
.registry()
.template_exists("developer_iteration_xml"));
}
#[test]
fn test_template_context_from_user_templates_dir() {
let custom_dir = PathBuf::from("/custom/templates");
let context = TemplateContext::from_user_templates_dir(Some(custom_dir));
// Context should be created successfully
assert!(!context
.registry()
.has_user_template("developer_iteration_xml"));
}
#[test]
fn test_template_context_from_user_templates_dir_none() {
let context = TemplateContext::from_user_templates_dir(None);
// Should not have user templates
assert!(!context
.registry()
.has_user_template("developer_iteration_xml"));
}
#[test]
fn test_template_context_registry_access() {
let context = TemplateContext::default();
let _registry = context.registry();
// Should be able to access registry methods
assert!(!TemplateRegistry::all_template_names().is_empty());
}
#[test]
fn test_template_context_clone() {
let context = TemplateContext::default();
let _cloned = context.clone();
// Verify clone compiles and original still works
assert!(context
.registry()
.template_exists("developer_iteration_xml"));
}
#[test]
fn test_template_context_get_template() {
let context = TemplateContext::default();
// Should be able to get templates
let result = context.registry().get_template("developer_iteration_xml");
assert!(result.is_ok());
}
#[test]
fn test_template_context_template_source() {
let context = TemplateContext::default();
// Should report embedded source for templates that don't have user overrides
assert_eq!(
context
.registry()
.template_source("developer_iteration_xml"),
"embedded"
);
}
#[test]
fn test_template_context_all_templates() {
let _context = TemplateContext::default();
// Should be able to list all templates
let names = TemplateRegistry::all_template_names();
assert!(names.len() > 10);
assert!(names.contains(&"developer_iteration_xml".to_string()));
}
#[test]
fn test_template_context_all_templates_not_empty() {
let _context = TemplateContext::default();
let names = TemplateRegistry::all_template_names();
assert!(!names.is_empty());
}
}