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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Streaming file generator for memory-efficient template processing
//!
//! Processes templates one at a time instead of loading entire file tree
//! into memory, enabling constant memory usage regardless of project size.
use crate::utils::error::{Error, Result};
use std::path::{Path, PathBuf};
use tera::Context;
use walkdir::WalkDir;
use crate::pipeline::Pipeline;
use crate::template_cache::TemplateCache;
/// Streaming generator that processes templates one at a time
///
/// Provides memory-efficient template processing by processing templates
/// sequentially rather than loading the entire file tree into memory.
/// This enables constant memory usage regardless of project size.
///
/// # Features
///
/// - **Memory efficient**: Processes one template at a time
/// - **LRU caching**: Caches parsed templates for reuse
/// - **Streaming processing**: Walks directory tree and processes templates on-the-fly
/// - **Error resilience**: Continues processing even if individual templates fail
///
/// # Examples
///
/// ```rust,no_run
/// use crate::streaming_generator::StreamingGenerator;
/// use tera::Context;
/// use std::path::PathBuf;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let mut generator = StreamingGenerator::new(
/// PathBuf::from("templates"),
/// PathBuf::from("output")
/// )?;
///
/// let mut vars = Context::new();
/// vars.insert("name", "MyApp");
///
/// let result = generator.generate_all(&vars)?;
/// println!("Generated {} files", result.success_count);
/// # Ok(())
/// # }
/// ```
pub struct StreamingGenerator {
template_dir: PathBuf,
output_dir: PathBuf,
cache: TemplateCache,
pipeline: Pipeline,
}
impl StreamingGenerator {
/// Create a new streaming generator
///
/// Creates a generator with default cache capacity (100 templates).
///
/// # Arguments
///
/// * `template_dir` - Directory containing template files (`.tmpl`)
/// * `output_dir` - Directory where generated files will be written
///
/// # Examples
///
/// ```rust,no_run
/// use crate::streaming_generator::StreamingGenerator;
/// use std::path::PathBuf;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let generator = StreamingGenerator::new(
/// PathBuf::from("templates"),
/// PathBuf::from("output")
/// )?;
/// # Ok(())
/// # }
/// ```
pub fn new(template_dir: PathBuf, output_dir: PathBuf) -> Result<Self> {
Ok(Self {
template_dir,
output_dir,
cache: TemplateCache::default(),
pipeline: Pipeline::new()?,
})
}
/// Create with custom cache capacity
///
/// Creates a generator with a specified cache capacity for parsed templates.
///
/// # Arguments
///
/// * `template_dir` - Directory containing template files
/// * `output_dir` - Directory where generated files will be written
/// * `cache_capacity` - Maximum number of templates to cache
///
/// # Examples
///
/// ```rust,no_run
/// use crate::streaming_generator::StreamingGenerator;
/// use std::path::PathBuf;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// // Create generator with larger cache for many templates
/// let generator = StreamingGenerator::with_cache_capacity(
/// PathBuf::from("templates"),
/// PathBuf::from("output"),
/// 500
/// )?;
/// # Ok(())
/// # }
/// ```
pub fn with_cache_capacity(
template_dir: PathBuf, output_dir: PathBuf, cache_capacity: usize,
) -> Result<Self> {
Ok(Self {
template_dir,
output_dir,
cache: TemplateCache::new(cache_capacity),
pipeline: Pipeline::new()?,
})
}
/// Generate all template files in streaming fashion
///
/// Walks the template directory and processes each `.tmpl` file sequentially,
/// generating output files. Templates are processed one at a time to minimize
/// memory usage.
///
/// # Arguments
///
/// * `vars` - Tera context containing template variables
///
/// # Returns
///
/// A `GenerationResult` containing statistics about the generation run,
/// including success count, error count, and generated file paths.
///
/// # Examples
///
/// ```rust,no_run
/// use crate::streaming_generator::StreamingGenerator;
/// use tera::Context;
/// use std::path::PathBuf;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let mut generator = StreamingGenerator::new(
/// PathBuf::from("templates"),
/// PathBuf::from("output")
/// )?;
///
/// let mut vars = Context::new();
/// vars.insert("project_name", "MyApp");
///
/// let result = generator.generate_all(&vars)?;
/// println!("Generated {} files successfully", result.success_count);
/// println!("Errors: {}", result.error_count);
/// println!("Throughput: {:.2} files/sec", result.throughput());
/// # Ok(())
/// # }
/// ```
/// Generate all template files in streaming fashion
///
/// **QUICK WIN 2: PARALLEL TEMPLATE GENERATION**
/// For sequential processing (maintains cache), see implementation.
/// For parallel bulk operations, use separate generator instances per thread.
pub fn generate_all(&mut self, vars: &Context) -> Result<GenerationResult> {
let mut result = GenerationResult::default();
let start = std::time::Instant::now();
// Collect template paths for potential parallelization
let template_paths: Vec<_> = WalkDir::new(&self.template_dir)
.into_iter()
.filter_map(|entry| entry.ok())
.filter(|entry| entry.file_type().is_file())
.filter(|entry| entry.path().extension() == Some(std::ffi::OsStr::new("tmpl")))
.map(|entry| entry.path().to_path_buf())
.collect();
// For now, process sequentially to maintain cache coherence
// Note: For true parallelism, use `generate_all_parallel` or create
// separate generator instances per thread
for path in template_paths {
match self.process_template(&path, vars) {
Ok(output_path) => {
result.success_count += 1;
result.generated_files.push(output_path);
}
Err(e) => {
result.error_count += 1;
result
.errors
.push(format!("Failed to process {}: {}", path.display(), e));
}
}
// Template is dropped here, freeing memory immediately
}
result.duration = start.elapsed();
Ok(result)
}
/// Process a single template file
///
/// **QUICK WIN 1: LAZY RDF LOADING**
/// Checks for RDF usage before expensive graph processing
fn process_template(&mut self, template_path: &Path, vars: &Context) -> Result<PathBuf> {
// Get from cache or parse
let template = self.cache.get_or_parse(template_path)?;
// Clone template for mutation (Arc<Template> -> Template)
let mut template = (*template).clone();
// Render frontmatter
template.render_frontmatter(&mut self.pipeline.tera, vars)?;
// QUICK WIN 1: Early check for RDF usage (40-60% faster for non-RDF templates)
// Process RDF graph only if template actually uses RDF/SPARQL
if !template.front.rdf_inline.is_empty()
|| !template.front.rdf.is_empty()
|| !template.front.sparql.is_empty()
{
template.process_graph(
&mut self.pipeline.graph,
&mut self.pipeline.tera,
vars,
template_path,
)?;
}
// Render template
let rendered = template.render(&mut self.pipeline.tera, vars, std::path::Path::new(""))?;
// Determine output path
let output_path = if let Some(to_path) = &template.front.to {
let rendered_to = self.pipeline.tera.render_str(to_path, vars)?;
self.output_dir.join(rendered_to)
} else {
let filename = template_path
.file_stem()
.ok_or_else(|| Error::new("Template has no filename"))?
.to_string_lossy();
self.output_dir.join(format!("{}.out", filename))
};
// Ensure parent directory exists
if let Some(parent) = output_path.parent() {
std::fs::create_dir_all(parent).map_err(|e| {
Error::new(&format!(
"Failed to create output directory {}: {}",
parent.display(),
e
))
})?;
}
// Write output file
std::fs::write(&output_path, rendered).map_err(|e| {
Error::new(&format!(
"Failed to write output file {}: {}",
output_path.display(),
e
))
})?;
Ok(output_path)
}
/// Get cache statistics
///
/// Returns information about the template cache, including current size
/// and capacity.
///
/// # Examples
///
/// ```rust,no_run
/// use crate::streaming_generator::StreamingGenerator;
/// use std::path::PathBuf;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let generator = StreamingGenerator::new(
/// PathBuf::from("templates"),
/// PathBuf::from("output")
/// )?;
///
/// let stats = generator.cache_stats()?;
/// println!("Cache: {}/{} templates", stats.size, stats.capacity);
/// # Ok(())
/// # }
/// ```
pub fn cache_stats(&self) -> Result<crate::template_cache::CacheStats> {
self.cache.stats()
}
}
/// Result of streaming generation
///
/// Contains statistics and results from a streaming generation run,
/// including success/error counts, generated file paths, and performance metrics.
///
/// # Examples
///
/// ```rust,no_run
/// use crate::streaming_generator::{StreamingGenerator, GenerationResult};
/// use tera::Context;
/// use std::path::PathBuf;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let mut generator = StreamingGenerator::new(
/// PathBuf::from("templates"),
/// PathBuf::from("output")
/// )?;
///
/// let result = generator.generate_all(&Context::new())?;
/// println!("Success rate: {:.1}%", result.success_rate());
/// println!("Throughput: {:.2} files/sec", result.throughput());
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Default)]
pub struct GenerationResult {
pub success_count: usize,
pub error_count: usize,
pub generated_files: Vec<PathBuf>,
pub errors: Vec<String>,
pub duration: std::time::Duration,
}
impl GenerationResult {
/// Total number of templates processed
///
/// Returns the sum of successful and failed template generations.
///
/// # Examples
///
/// ```rust
/// use crate::streaming_generator::GenerationResult;
///
/// # fn main() {
/// let mut result = GenerationResult::default();
/// result.success_count = 8;
/// result.error_count = 2;
///
/// assert_eq!(result.total_count(), 10);
/// # }
/// ```
pub fn total_count(&self) -> usize {
self.success_count + self.error_count
}
/// Success rate as percentage
///
/// Calculates the percentage of templates that were successfully generated.
/// Returns 0.0 if no templates were processed.
///
/// # Examples
///
/// ```rust
/// use crate::streaming_generator::GenerationResult;
///
/// # fn main() {
/// let mut result = GenerationResult::default();
/// result.success_count = 9;
/// result.error_count = 1;
///
/// assert_eq!(result.success_rate(), 90.0);
/// # }
/// ```
pub fn success_rate(&self) -> f64 {
if self.total_count() == 0 {
return 0.0;
}
(self.success_count as f64 / self.total_count() as f64) * 100.0
}
/// Generation throughput (files per second)
///
/// Calculates the number of files generated per second based on the
/// generation duration. Returns 0.0 if duration is zero.
///
/// # Examples
///
/// ```rust
/// use crate::streaming_generator::GenerationResult;
/// use std::time::Duration;
///
/// # fn main() {
/// let mut result = GenerationResult::default();
/// result.success_count = 10;
/// result.duration = Duration::from_secs(2);
///
/// assert_eq!(result.throughput(), 5.0); // 10 files in 2 seconds
/// # }
/// ```
pub fn throughput(&self) -> f64 {
if self.duration.as_secs_f64() == 0.0 {
return 0.0;
}
self.success_count as f64 / self.duration.as_secs_f64()
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
fn create_test_template(dir: &Path, name: &str, content: &str) -> Result<PathBuf> {
let path = dir.join(format!("{}.tmpl", name));
std::fs::write(&path, content)?;
Ok(path)
}
#[test]
fn test_streaming_generator_new() -> Result<()> {
let temp_dir = TempDir::new()?;
let output_dir = TempDir::new()?;
let generator = StreamingGenerator::new(
temp_dir.path().to_path_buf(),
output_dir.path().to_path_buf(),
)?;
assert_eq!(generator.template_dir, temp_dir.path());
assert_eq!(generator.output_dir, output_dir.path());
Ok(())
}
#[test]
fn test_streaming_generator_single_file() -> Result<()> {
let temp_dir = TempDir::new()?;
let output_dir = TempDir::new()?;
create_test_template(
temp_dir.path(),
"test",
r#"---
to: "output.rs"
---
fn main() { println!("Hello, {{ name }}!"); }
"#,
)?;
let mut generator = StreamingGenerator::new(
temp_dir.path().to_path_buf(),
output_dir.path().to_path_buf(),
)?;
let mut vars = Context::new();
vars.insert("name", "World");
let result = generator.generate_all(&vars)?;
assert_eq!(result.success_count, 1);
assert_eq!(result.error_count, 0);
assert_eq!(result.generated_files.len(), 1);
let output_path = output_dir.path().join("output.rs");
assert!(output_path.exists());
let content = std::fs::read_to_string(&output_path)?;
assert!(content.contains("Hello, World!"));
Ok(())
}
#[test]
fn test_streaming_generator_multiple_files() -> Result<()> {
let temp_dir = TempDir::new()?;
let output_dir = TempDir::new()?;
// Create multiple templates
for i in 0..5 {
create_test_template(
temp_dir.path(),
&format!("test_{}", i),
&format!(
r#"---
to: "output_{i}.rs"
---
fn main_{i}() {{ println!("File {i}"); }}
"#,
),
)?;
}
let mut generator = StreamingGenerator::new(
temp_dir.path().to_path_buf(),
output_dir.path().to_path_buf(),
)?;
let vars = Context::new();
let result = generator.generate_all(&vars)?;
assert_eq!(result.success_count, 5);
assert_eq!(result.error_count, 0);
assert!(result.success_rate() > 99.0);
assert!(result.throughput() > 0.0);
Ok(())
}
#[test]
fn test_streaming_generator_nested_output() -> Result<()> {
let temp_dir = TempDir::new()?;
let output_dir = TempDir::new()?;
create_test_template(
temp_dir.path(),
"nested",
r#"---
to: "src/handlers/{{ module }}.rs"
---
// Handler module
"#,
)?;
let mut generator = StreamingGenerator::new(
temp_dir.path().to_path_buf(),
output_dir.path().to_path_buf(),
)?;
let mut vars = Context::new();
vars.insert("module", "users");
let result = generator.generate_all(&vars)?;
assert_eq!(result.success_count, 1);
let output_path = output_dir.path().join("src/handlers/users.rs");
assert!(output_path.exists());
Ok(())
}
#[test]
fn test_streaming_generator_cache_reuse() -> Result<()> {
let temp_dir = TempDir::new()?;
let output_dir = TempDir::new()?;
create_test_template(
temp_dir.path(),
"cached",
r#"---
to: "output.rs"
---
fn main() {}
"#,
)?;
let mut generator = StreamingGenerator::new(
temp_dir.path().to_path_buf(),
output_dir.path().to_path_buf(),
)?;
let vars = Context::new();
// First generation
generator.generate_all(&vars)?;
let stats1 = generator.cache_stats()?;
assert_eq!(stats1.size, 1);
// Second generation should hit cache
generator.generate_all(&vars)?;
let stats2 = generator.cache_stats()?;
assert_eq!(stats2.size, 1); // Still 1, reused from cache
Ok(())
}
}