Skip to content

Commit 2a0a0cb

Browse files
committed
refactor: simplify TemplateRenderer trait and remove StaticContext dependency
- Remove generic Context type from TemplateRenderer trait - Simplify render method to only require output path - Move template validation and rendering to construction time - Remove template_path and validate_context methods from trait - Templates now pre-validate content and render directly - Maintains all existing functionality while reducing API complexity - All tests pass and linting requirements met
1 parent d646a0d commit 2a0a0cb

File tree

4 files changed

+9
-39
lines changed

4 files changed

+9
-39
lines changed

src/bin/e2e_tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use torrust_tracker_deploy::template::file::File;
1212
use torrust_tracker_deploy::template::wrappers::ansible::inventory::{
1313
InventoryContext, InventoryTemplate,
1414
};
15-
use torrust_tracker_deploy::template::{StaticContext, TemplateRenderer};
15+
use torrust_tracker_deploy::template::TemplateRenderer;
1616

1717
#[derive(Parser)]
1818
#[command(name = "e2e-tests")]
@@ -150,9 +150,8 @@ impl TestEnvironment {
150150
InventoryTemplate::new(&inventory_template_file, &inventory_context)
151151
.context("Failed to create InventoryTemplate")?;
152152

153-
let static_context = StaticContext::default();
154153
inventory_template
155-
.render(&static_context, &inventory_output_path)
154+
.render(&inventory_output_path)
156155
.context("Failed to render inventory template")?;
157156

158157
// Copy static ansible files

src/template/renderer.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,13 @@
33
//! Defines the core `TemplateRenderer` trait that all template wrappers must implement.
44
55
use anyhow::Result;
6-
use serde::Serialize;
76
use std::path::Path;
87

98
/// Core trait for template rendering with strong typing and validation
109
pub trait TemplateRenderer {
11-
/// Context type that provides template variables
12-
type Context: Serialize;
13-
14-
/// Returns the path to the template file
15-
fn template_path(&self) -> &Path;
16-
1710
/// Renders the template with the given context to the output path
1811
///
1912
/// # Errors
2013
/// Returns an error if template rendering fails or output file cannot be written
21-
fn render(&self, context: &Self::Context, output_path: &Path) -> Result<()>;
22-
23-
/// Validates that the context contains all required variables
24-
///
25-
/// # Errors
26-
/// Returns an error if any required variables are missing from the context
27-
fn validate_context(&self, context: &Self::Context) -> Result<()>;
14+
fn render(&self, output_path: &Path) -> Result<()>;
2815
}

src/template/wrappers/ansible/inventory/mod.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub mod ansible_host;
66
pub mod ssh_private_key_file;
77

88
use crate::template::file::File;
9-
use crate::template::{StaticContext, TemplateRenderer};
9+
use crate::template::TemplateRenderer;
1010
use anyhow::{Context, Result};
1111
use serde::Serialize;
1212
use std::fs;
@@ -115,15 +115,7 @@ impl InventoryTemplate {
115115
}
116116

117117
impl TemplateRenderer for InventoryTemplate {
118-
type Context = StaticContext; // We don't use external context since we have fields
119-
120-
fn template_path(&self) -> &Path {
121-
// Since we're working with content instead of paths, return a dummy path
122-
// This should be refactored in the trait if this pattern is used more widely
123-
Path::new("inventory.yml")
124-
}
125-
126-
fn render(&self, _context: &Self::Context, output_path: &Path) -> Result<()> {
118+
fn render(&self, output_path: &Path) -> Result<()> {
127119
// Create output directory if it doesn't exist
128120
if let Some(parent) = output_path.parent() {
129121
fs::create_dir_all(parent).with_context(|| {
@@ -138,11 +130,6 @@ impl TemplateRenderer for InventoryTemplate {
138130

139131
Ok(())
140132
}
141-
142-
fn validate_context(&self, _context: &Self::Context) -> Result<()> {
143-
// Validation is built-in since fields are mandatory at construction
144-
Ok(())
145-
}
146133
}
147134

148135
#[cfg(test)]

tests/template_integration.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use torrust_tracker_deploy::template::file::File;
1010
use torrust_tracker_deploy::template::wrappers::ansible::inventory::{
1111
InventoryContext, InventoryTemplate,
1212
};
13-
use torrust_tracker_deploy::template::{StaticContext, TemplateRenderer};
13+
use torrust_tracker_deploy::template::TemplateRenderer;
1414

1515
#[cfg(test)]
1616
mod integration_tests {
@@ -45,8 +45,7 @@ mod integration_tests {
4545
let inventory = InventoryTemplate::new(&template_file, &inventory_context)?;
4646

4747
// Render the template
48-
let context = StaticContext::default();
49-
inventory.render(&context, &output_path)?;
48+
inventory.render(&output_path)?;
5049

5150
// Verify the output file exists and has the right content
5251
assert!(output_path.exists());
@@ -144,8 +143,7 @@ mod integration_tests {
144143
)?;
145144
let inventory = InventoryTemplate::new(&template_file, &inventory_context)?;
146145

147-
let context = StaticContext::default();
148-
inventory.render(&context, &output_path)?;
146+
inventory.render(&output_path)?;
149147
}
150148

151149
// Verify the original template is unchanged
@@ -187,8 +185,7 @@ mod integration_tests {
187185
)?;
188186
let inventory = InventoryTemplate::new(&template_file, &inventory_context)?;
189187

190-
let context = StaticContext::default();
191-
inventory.render(&context, &output_path)?;
188+
inventory.render(&output_path)?;
192189

193190
// Verify output in build directory
194191
assert!(output_path.exists());

0 commit comments

Comments
 (0)