ggen-domain 5.1.3

Domain logic layer for ggen - pure business logic without CLI dependencies
Documentation
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
574
575
576
//! Template Generator for generating code from pack templates
//!
//! This module provides template generation capabilities using Tera templating engine.
//! It supports variable validation, interactive prompts, and post-generation hooks.

use crate::packs::types::{Pack, PackTemplate};
use ggen_utils::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use tera::{Context, Tera};
use tracing::{debug, info, warn};

/// Template generator for creating code from pack templates
pub struct TemplateGenerator {
    /// Tera template engine
    #[allow(dead_code)]
    tera: Tera,
}

/// Generation report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerationReport {
    /// Files created during generation
    pub files_created: Vec<PathBuf>,
    /// Total size of generated files (bytes)
    pub total_size: u64,
    /// Variables used in generation
    pub variables_used: HashMap<String, String>,
    /// Time taken for generation
    pub duration: Duration,
    /// Post-generation hooks executed
    pub hooks_executed: Vec<String>,
    /// Success status
    pub success: bool,
}

/// Variable definition for templates
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableDefinition {
    /// Variable name
    pub name: String,
    /// Variable type (string, integer, boolean, etc.)
    pub var_type: VariableType,
    /// Description
    pub description: String,
    /// Default value
    pub default: Option<String>,
    /// Required flag
    pub required: bool,
    /// Validation pattern (regex)
    pub pattern: Option<String>,
}

/// Variable type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum VariableType {
    String,
    Integer,
    Boolean,
    Path,
    Email,
    Url,
}

impl TemplateGenerator {
    /// Create new template generator
    pub fn new() -> Result<Self> {
        let tera = Tera::default();
        Ok(Self { tera })
    }

    /// List available templates in a pack
    ///
    /// # Arguments
    /// * `pack` - The pack to query
    ///
    /// # Returns
    /// List of templates in the pack
    pub fn list_templates(&self, pack: &Pack) -> Result<Vec<PackTemplate>> {
        Ok(pack.templates.clone())
    }

    /// Generate code from a template
    ///
    /// # Arguments
    /// * `template` - Template to use
    /// * `variables` - Template variables
    /// * `target_dir` - Output directory
    ///
    /// # Returns
    /// Generation report with created files
    pub fn generate_from_template(
        &mut self, template: &PackTemplate, variables: HashMap<String, String>, target_dir: &Path,
    ) -> Result<GenerationReport> {
        let start = Instant::now();

        info!(
            "Generating from template '{}' to {}",
            template.name,
            target_dir.display()
        );

        // Validate variables
        self.validate_variables(template, &variables)?;

        // Create target directory
        std::fs::create_dir_all(target_dir)?;

        // Build Tera context
        let context = self.build_context(&variables)?;

        // Generate files
        let files_created = self.generate_files(template, &context, target_dir)?;

        // Calculate total size
        let total_size = files_created
            .iter()
            .filter_map(|path| std::fs::metadata(path).ok())
            .map(|meta| meta.len())
            .sum();

        // Execute post-generation hooks
        let hooks_executed = self.execute_post_hooks(template, target_dir, &variables)?;

        let duration = start.elapsed();

        info!(
            "Generated {} files ({} bytes) in {:?}",
            files_created.len(),
            total_size,
            duration
        );

        Ok(GenerationReport {
            files_created,
            total_size,
            variables_used: variables,
            duration,
            hooks_executed,
            success: true,
        })
    }

    /// Validate template variables
    ///
    /// # Arguments
    /// * `template` - Template definition
    /// * `vars` - Variables to validate
    ///
    /// # Returns
    /// Ok if valid, error otherwise
    pub fn validate_variables(
        &self, template: &PackTemplate, vars: &HashMap<String, String>,
    ) -> Result<()> {
        // Get variable definitions from template
        let var_defs = self.extract_variable_definitions(template);

        for var_def in &var_defs {
            // Check required variables
            if var_def.required && !vars.contains_key(&var_def.name) {
                return Err(Error::new(&format!(
                    "Required variable '{}' not provided for template '{}'",
                    var_def.name, template.name
                )));
            }

            // Validate provided variables
            if let Some(value) = vars.get(&var_def.name) {
                self.validate_variable_value(&var_def, value)?;
            }
        }

        Ok(())
    }

    /// Extract variable definitions from template
    fn extract_variable_definitions(&self, template: &PackTemplate) -> Vec<VariableDefinition> {
        // In a real implementation, this would parse the template file
        // For now, we use the variables list from the template metadata

        template
            .variables
            .iter()
            .map(|name| VariableDefinition {
                name: name.clone(),
                var_type: VariableType::String,
                description: format!("Variable: {}", name),
                default: None,
                required: true,
                pattern: None,
            })
            .collect()
    }

    /// Validate a single variable value
    fn validate_variable_value(&self, var_def: &VariableDefinition, value: &str) -> Result<()> {
        // Type validation
        match var_def.var_type {
            VariableType::Integer => {
                value.parse::<i64>().map_err(|_| {
                    Error::new(&format!(
                        "Variable '{}' must be an integer, got '{}'",
                        var_def.name, value
                    ))
                })?;
            }
            VariableType::Boolean => {
                if value != "true" && value != "false" {
                    return Err(Error::new(&format!(
                        "Variable '{}' must be 'true' or 'false', got '{}'",
                        var_def.name, value
                    )));
                }
            }
            VariableType::Email => {
                if !value.contains('@') {
                    return Err(Error::new(&format!(
                        "Variable '{}' must be a valid email, got '{}'",
                        var_def.name, value
                    )));
                }
            }
            VariableType::Url => {
                if !value.starts_with("http://") && !value.starts_with("https://") {
                    return Err(Error::new(&format!(
                        "Variable '{}' must be a valid URL, got '{}'",
                        var_def.name, value
                    )));
                }
            }
            VariableType::Path => {
                // Just check it's not empty
                if value.is_empty() {
                    return Err(Error::new(&format!(
                        "Variable '{}' cannot be empty",
                        var_def.name
                    )));
                }
            }
            VariableType::String => {
                // No specific validation for generic strings
            }
        }

        // Pattern validation
        if let Some(pattern) = &var_def.pattern {
            let re = regex::Regex::new(pattern)
                .map_err(|e| Error::new(&format!("Invalid regex pattern '{}': {}", pattern, e)))?;

            if !re.is_match(value) {
                return Err(Error::new(&format!(
                    "Variable '{}' value '{}' does not match pattern '{}'",
                    var_def.name, value, pattern
                )));
            }
        }

        Ok(())
    }

    /// Build Tera context from variables
    fn build_context(&self, variables: &HashMap<String, String>) -> Result<Context> {
        let mut context = Context::new();

        for (key, value) in variables {
            context.insert(key, value);
        }

        // Add utility functions/filters
        context.insert("timestamp", &chrono::Utc::now().to_rfc3339());
        context.insert("uuid", &uuid::Uuid::new_v4().to_string());

        Ok(context)
    }

    /// Generate files from template
    fn generate_files(
        &mut self, template: &PackTemplate, _context: &Context, target_dir: &Path,
    ) -> Result<Vec<PathBuf>> {
        let mut files_created = Vec::new();

        // In a real implementation, this would:
        // 1. Read template directory
        // 2. Process each template file
        // 3. Render using Tera
        // 4. Write to target directory

        // For now, we'll create a placeholder implementation
        debug!("Generating from template at path: {}", template.path);

        // Example: Create a basic file
        let output_file = target_dir.join(format!("{}.generated", template.name));

        // Render a simple template
        let content = format!(
            "# Generated from template: {}\n# Description: {}\n\n",
            template.name, template.description
        );

        // FMEA tracked file I/O with proper instrumentation
        ggen_utils::fmea_track!(
            "file_io_write_fail",
            &format!("write_{}", output_file.display()),
            {
                std::fs::write(&output_file, &content)
                    .map_err(|e| ggen_utils::error::Error::io_error(&e.to_string()))
            }
        )?;
        files_created.push(output_file);

        info!("Created {} files", files_created.len());

        Ok(files_created)
    }

    /// Execute post-generation hooks
    fn execute_post_hooks(
        &self, template: &PackTemplate, target_dir: &Path, variables: &HashMap<String, String>,
    ) -> Result<Vec<String>> {
        let mut hooks_executed = Vec::new();

        // Check for common post-generation hooks based on template type
        let hooks = self.determine_hooks(template, variables);

        for hook in &hooks {
            match hook.as_str() {
                "npm_install" => {
                    if self.should_run_npm_install(target_dir) {
                        info!("Running npm install in {}", target_dir.display());
                        // In production, actually run: std::process::Command::new("npm").arg("install")...
                        hooks_executed.push("npm_install".to_string());
                    }
                }
                "cargo_check" => {
                    if target_dir.join("Cargo.toml").exists() {
                        info!("Running cargo check in {}", target_dir.display());
                        // In production: std::process::Command::new("cargo").arg("check")...
                        hooks_executed.push("cargo_check".to_string());
                    }
                }
                "git_init" => {
                    if !target_dir.join(".git").exists() {
                        info!("Initializing git repository in {}", target_dir.display());
                        // In production: std::process::Command::new("git").arg("init")...
                        hooks_executed.push("git_init".to_string());
                    }
                }
                _ => {
                    warn!("Unknown hook: {}", hook);
                }
            }
        }

        Ok(hooks_executed)
    }

    /// Determine which hooks to run based on template
    fn determine_hooks(
        &self, template: &PackTemplate, _variables: &HashMap<String, String>,
    ) -> Vec<String> {
        let mut hooks = Vec::new();

        // Determine hooks based on template name/path
        if template.name.contains("node") || template.name.contains("javascript") {
            hooks.push("npm_install".to_string());
        }

        if template.name.contains("rust") || template.name.contains("cargo") {
            hooks.push("cargo_check".to_string());
        }

        hooks.push("git_init".to_string());

        hooks
    }

    /// Check if npm install should be run
    fn should_run_npm_install(&self, target_dir: &Path) -> bool {
        target_dir.join("package.json").exists()
    }

    /// Interactive variable prompt (for CLI usage)
    pub fn prompt_variables(&self, template: &PackTemplate) -> Result<HashMap<String, String>> {
        let var_defs = self.extract_variable_definitions(template);
        let mut variables = HashMap::new();

        info!(
            "Template '{}' requires {} variables",
            template.name,
            var_defs.len()
        );

        for var_def in &var_defs {
            let _prompt = if let Some(default) = &var_def.default {
                format!("{} [{}]: ", var_def.description, default)
            } else {
                format!("{}: ", var_def.description)
            };

            // In a real CLI, we'd use something like dialoguer
            // For now, just use defaults or placeholder values
            let value = var_def
                .default
                .clone()
                .unwrap_or_else(|| format!("value_for_{}", var_def.name));

            variables.insert(var_def.name.clone(), value);
        }

        Ok(variables)
    }
}

// NOTE: Default implementation removed - TemplateGenerator::new() is infallible
// but we remove it to maintain consistent patterns across codebase
// Use TemplateGenerator::new().unwrap_or_else(...) explicitly if Default is needed
impl Default for TemplateGenerator {
    fn default() -> Self {
        // Tera::default() is infallible, so this is safe
        Self {
            tera: tera::Tera::default(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_template() -> PackTemplate {
        PackTemplate {
            name: "test-template".to_string(),
            path: "templates/test".to_string(),
            description: "A test template".to_string(),
            variables: vec!["project_name".to_string(), "author".to_string()],
        }
    }

    #[test]
    fn test_template_generator_creation() {
        let generator = TemplateGenerator::new();
        assert!(generator.is_ok());
    }

    #[test]
    fn test_validate_variables_success() {
        let generator = TemplateGenerator::new().unwrap();
        let template = create_test_template();

        let mut variables = HashMap::new();
        variables.insert("project_name".to_string(), "my-project".to_string());
        variables.insert("author".to_string(), "Test Author".to_string());

        let result = generator.validate_variables(&template, &variables);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_variables_missing_required() {
        let generator = TemplateGenerator::new().unwrap();
        let template = create_test_template();

        let mut variables = HashMap::new();
        variables.insert("project_name".to_string(), "my-project".to_string());
        // Missing 'author' variable

        let result = generator.validate_variables(&template, &variables);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_variable_value_integer() {
        let generator = TemplateGenerator::new().unwrap();

        let var_def = VariableDefinition {
            name: "port".to_string(),
            var_type: VariableType::Integer,
            description: "Port number".to_string(),
            default: None,
            required: true,
            pattern: None,
        };

        // Valid integer
        assert!(generator.validate_variable_value(&var_def, "8080").is_ok());

        // Invalid integer
        assert!(generator
            .validate_variable_value(&var_def, "not-a-number")
            .is_err());
    }

    #[test]
    fn test_validate_variable_value_boolean() {
        let generator = TemplateGenerator::new().unwrap();

        let var_def = VariableDefinition {
            name: "enabled".to_string(),
            var_type: VariableType::Boolean,
            description: "Enabled flag".to_string(),
            default: None,
            required: true,
            pattern: None,
        };

        assert!(generator.validate_variable_value(&var_def, "true").is_ok());
        assert!(generator.validate_variable_value(&var_def, "false").is_ok());
        assert!(generator.validate_variable_value(&var_def, "yes").is_err());
    }

    #[test]
    fn test_validate_variable_value_email() {
        let generator = TemplateGenerator::new().unwrap();

        let var_def = VariableDefinition {
            name: "email".to_string(),
            var_type: VariableType::Email,
            description: "Email address".to_string(),
            default: None,
            required: true,
            pattern: None,
        };

        assert!(generator
            .validate_variable_value(&var_def, "test@example.com")
            .is_ok());
        assert!(generator
            .validate_variable_value(&var_def, "invalid-email")
            .is_err());
    }

    #[test]
    fn test_generate_from_template() {
        let mut generator = TemplateGenerator::new().unwrap();
        let template = create_test_template();

        let mut variables = HashMap::new();
        variables.insert("project_name".to_string(), "test-project".to_string());
        variables.insert("author".to_string(), "Test Author".to_string());

        let temp_dir = tempfile::tempdir().unwrap();
        let result = generator.generate_from_template(&template, variables, temp_dir.path());

        assert!(result.is_ok());
        let report = result.unwrap();
        assert!(report.success);
        assert!(!report.files_created.is_empty());
    }

    #[test]
    fn test_determine_hooks() {
        let generator = TemplateGenerator::new().unwrap();

        let node_template = PackTemplate {
            name: "node-app".to_string(),
            path: "templates/node".to_string(),
            description: "Node.js app".to_string(),
            variables: vec![],
        };

        let hooks = generator.determine_hooks(&node_template, &HashMap::new());
        assert!(hooks.contains(&"npm_install".to_string()));

        let rust_template = PackTemplate {
            name: "rust-lib".to_string(),
            path: "templates/rust".to_string(),
            description: "Rust library".to_string(),
            variables: vec![],
        };

        let hooks = generator.determine_hooks(&rust_template, &HashMap::new());
        assert!(hooks.contains(&"cargo_check".to_string()));
    }
}