ggen-core 26.7.3

Core graph-aware code generation engine
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
//! Business logic separation for CLI commands
//!
//! This module provides functionality for separating CLI layer (thin wrapper)
//! from business logic (domain implementation) to enable easier maintenance
//! and testing. This follows the 2026 best practice of keeping CLI layers thin
//! and business logic in a separate domain layer.
//!
//! ## Features
//!
//! - **CLI Wrapper Generation**: Creates thin synchronous wrappers for async business logic
//! - **Domain Skeleton Generation**: Generates async business logic templates
//! - **Non-Destructive**: Never overwrites existing business logic files
//! - **Frozen Sections**: Uses frozen tags to protect user code
//! - **Type Safety**: Generates type-safe argument structures
//! - **Test Templates**: Includes test boilerplate for business logic
//!
//! ## Architecture
//!
//! The separation creates two layers:
//!
//! 1. **CLI Layer** (`cli/`): Thin synchronous wrappers that:
//!    - Parse CLI arguments using clap
//!    - Create a Tokio runtime
//!    - Call async business logic
//!    - Handle errors and output
//!
//! 2. **Domain Layer** (`domain/`): Async business logic that:
//!    - Contains actual implementation
//!    - Is never overwritten once created
//!    - Can be freely modified by users
//!    - Includes frozen sections for regeneration safety
//!
//! ## Examples
//!
//! ### Generating Separated Files
//!
//! ```rust,no_run
//! use crate::templates::business_logic::BusinessLogicSeparator;
//! use std::path::Path;
//!
//! # fn main() -> crate::utils::error::Result<()> {
//! let cli_path = Path::new("cli/create_project.rs");
//! let domain_path = Path::new("domain/create_project.rs");
//!
//! BusinessLogicSeparator::generate_separated_files(
//!     cli_path,
//!     domain_path,
//!     "create",
//!     "project",
//!     false, // Don't overwrite existing domain file
//! )?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Generated CLI Wrapper
//!
//! The CLI wrapper will look like:
//!
//! ```ignore
//! //! CLI wrapper for create project (illustrative generated output)
//! use clap_noun_verb::{NounVerbError, Result};
//! use clap_noun_verb_macros::verb;
//! use serde_json::{json, Value};
//! use crate::domain::create_project as domain_impl;
//!
//! /// create project command.
//! #[verb("create", "project")]
//! pub fn create_project() -> Result<Value> {
//!     let _result = tokio::runtime::Runtime::new()
//!         .map_err(|e| NounVerbError::execution_error(e.to_string()))?
//!         .block_on(async { domain_impl().await });
//!     Ok(json!({ "status": "ok" }))
//! }
//! ```
//!
//! ### Generated Domain Logic
//!
//! The domain logic will look like:
//!
//! ```ignore
//! //! Business logic for create project (illustrative generated output)
//! use crate::utils::error::Result;
//!
//! #[derive(Debug)]
//! pub struct CreateProjectArgs {
//!     // Business logic arguments
//! }
//!
//! pub async fn create_project(args: CreateProjectArgs) -> Result<()> {
//!     {% frozen id="implementation" %}
//!     // User's implementation here
//!     {% endfrozen %}
//! }
//! ```

use crate::utils::error::Result;
use std::fs;
use std::path::Path;

/// Generator for separated business logic files
///
/// Generates code that separates CLI layer (thin wrapper) from business logic
/// (domain implementation), following 2026 best practices for maintainable CLI projects.
///
/// # Examples
///
/// ```rust,no_run
/// use crate::templates::business_logic::BusinessLogicSeparator;
/// use std::path::Path;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let cli_path = Path::new("cli/create_project.rs");
/// let domain_path = Path::new("domain/create_project.rs");
///
/// BusinessLogicSeparator::generate_separated_files(
///     cli_path,
///     domain_path,
///     "create",
///     "project",
///     false,
/// )?;
/// # Ok(())
/// # }
/// ```
pub struct BusinessLogicSeparator;

impl BusinessLogicSeparator {
    /// Generate CLI wrapper file (sync wrapper calling async business logic)
    ///
    /// Creates a thin CLI layer with `#[verb]` attribute that delegates to
    /// async business logic in the domain layer.
    ///
    /// # Arguments
    /// * `verb` - The CLI verb (e.g., "create", "delete", "list")
    /// * `noun` - The CLI noun (e.g., "project", "user", "task")
    /// * `module_path` - Optional module path for business logic (defaults to "domain")
    pub fn generate_cli_wrapper(verb: &str, noun: &str, module_path: Option<&str>) -> String {
        let module = module_path.unwrap_or("domain");
        let struct_name = Self::to_pascal_case(&format!("{}-{}", verb, noun));
        let function_name = Self::to_snake_case(&format!("{}_{}", verb, noun));

        let _ = struct_name; // no longer emitted: clap-noun-verb verbs take params, not an Args struct
        format!(
            r#"//! CLI wrapper for {verb} {noun}
//!
//! Thin clap-noun-verb verb that delegates to async business logic.

use clap_noun_verb::{{NounVerbError, Result}};
use clap_noun_verb_macros::verb;
use serde_json::{{json, Value}};
use crate::{module}::{function_name} as domain_impl;

/// {verb} {noun} command.
#[verb("{verb}", "{noun}")]
pub fn {function_name}() -> Result<Value> {{
    // Delegate to async business logic.
    let _result = tokio::runtime::Runtime::new()
        .map_err(|e| NounVerbError::execution_error(e.to_string()))?
        .block_on(async {{ domain_impl().await }});
    Ok(json!({{ "status": "ok" }}))
}}
"#,
            verb = verb,
            noun = noun,
            module = module,
            function_name = function_name
        )
    }

    /// Generate business logic skeleton (only if doesn't exist)
    ///
    /// Creates an async business logic template in the domain layer with
    /// frozen sections to protect user code. This file is NEVER overwritten
    /// once it exists, allowing users to freely modify business logic.
    ///
    /// # Arguments
    ///
    /// * `verb` - The CLI verb (e.g., "create", "delete", "list")
    /// * `noun` - The CLI noun (e.g., "project", "user", "task")
    ///
    /// # Returns
    ///
    /// Generated Rust code as a string containing the async business logic skeleton
    /// with frozen sections for safe regeneration.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use crate::templates::business_logic::BusinessLogicSeparator;
    ///
    /// let code = BusinessLogicSeparator::generate_domain_skeleton("delete", "user");
    /// assert!(code.contains("pub async fn delete_user"));
    /// assert!(code.contains("{% frozen"));
    /// assert!(code.contains("#[tokio::test]"));
    /// ```
    pub fn generate_domain_skeleton(verb: &str, noun: &str) -> String {
        let function_name = Self::to_snake_case(&format!("{}_{}", verb, noun));
        let struct_name = Self::to_pascal_case(&format!("{}-{}", verb, noun));

        let template = format!(
            r"//! Business logic for {} {}
//!
//! This module contains the async business logic implementation.
//! Modify this file freely - it will never be regenerated.

use crate::utils::error::Result;

/// Arguments for {} {} operation
#[derive(Debug)]
pub struct {}Args {{
    // FUTURE: Add business logic arguments here
}}

/// {} {} business logic (async)
pub async fn {}(args: {}Args) -> Result<()> {{
    // Default implementation - replace with your logic
    // FUTURE: Implement actual business logic here
    Ok(())
}}

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

    #[tokio::test]
    async fn test_{}() {{
        let args = {}Args {{}};
        let result = {}(args).await;
        assert!(result.is_ok());
    }}
}}
",
            verb,
            noun,
            verb,
            noun,
            struct_name,
            verb,
            noun,
            function_name,
            struct_name,
            function_name,
            struct_name,
            function_name
        );

        // Add frozen tags after format to avoid format string conflicts
        template
            .replace(
                &format!("/// {} {} business logic (async)", verb, noun),
                &format!("/// {} {} business logic (async)\n///\n/// {{% frozen id=\"business_logic\" %}}\n/// FUTURE: Implement business logic here\n/// {{% endfrozen %}}", verb, noun)
            )
            .replace(
                "// Default implementation - replace with your logic",
                "{{% frozen id=\"implementation\" %}}\n    // Default implementation - replace with your logic"
            )
            .replace(
                &format!("println!(\"Executing {} {} with args: {{{{:?}}}}\", args);\n    Ok(())", verb, noun),
                &format!("println!(\"Executing {} {} with args: {{{{:?}}}}\", args);\n    Ok(())\n    {{% endfrozen %}}", verb, noun)
            )
    }

    /// Check if business logic file exists
    ///
    /// Returns `true` if the business logic file already exists and should
    /// NOT be overwritten. This prevents accidental loss of user code.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the business logic file to check
    ///
    /// # Returns
    ///
    /// `true` if the file exists, `false` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use crate::templates::business_logic::BusinessLogicSeparator;
    /// use std::path::Path;
    ///
    /// let exists = BusinessLogicSeparator::business_logic_exists(Path::new("domain/create.rs"));
    /// if !exists {
    ///     // Safe to generate new file
    /// }
    /// ```
    pub fn business_logic_exists(path: &Path) -> bool {
        path.exists()
    }

    /// Generate both CLI wrapper and domain logic files
    ///
    /// Generates the complete separation: a CLI wrapper file and a domain logic file.
    /// The CLI wrapper is always generated (it's thin and safe to regenerate).
    /// The domain logic file is only generated if it doesn't exist, unless `force_domain`
    /// is `true` (which is dangerous and should be used with caution).
    ///
    /// # Arguments
    ///
    /// * `cli_path` - Path for CLI wrapper file (always generated)
    /// * `domain_path` - Path for business logic file (only if missing or forced)
    /// * `verb` - The CLI verb (e.g., "create", "delete", "list")
    /// * `noun` - The CLI noun (e.g., "project", "user", "task")
    /// * `force_domain` - Force overwrite of domain file (dangerous! Use with caution)
    ///
    /// # Errors
    ///
    /// Returns an error if file system operations fail.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use crate::templates::business_logic::BusinessLogicSeparator;
    /// use std::path::Path;
    ///
    /// # fn main() -> crate::utils::error::Result<()> {
    /// BusinessLogicSeparator::generate_separated_files(
    ///     Path::new("cli/create_project.rs"),
    ///     Path::new("domain/create_project.rs"),
    ///     "create",
    ///     "project",
    ///     false, // Don't overwrite existing domain file
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn generate_separated_files(
        cli_path: &Path, domain_path: &Path, verb: &str, noun: &str, force_domain: bool,
    ) -> Result<()> {
        // Always generate CLI wrapper (it's thin and can be regenerated)
        let cli_content = Self::generate_cli_wrapper(verb, noun, None);
        if let Some(parent) = cli_path.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(cli_path, cli_content)?;

        // Only generate domain skeleton if it doesn't exist or force is true
        if force_domain || !Self::business_logic_exists(domain_path) {
            let domain_content = Self::generate_domain_skeleton(verb, noun);
            if let Some(parent) = domain_path.parent() {
                fs::create_dir_all(parent)?;
            }
            fs::write(domain_path, domain_content)?;
        }

        Ok(())
    }

    /// Convert string to PascalCase
    fn to_pascal_case(s: &str) -> String {
        s.split(|c: char| !c.is_alphanumeric())
            .filter(|s| !s.is_empty())
            .map(|word| {
                let mut chars = word.chars();
                match chars.next() {
                    None => String::new(),
                    Some(first) => first.to_uppercase().chain(chars).collect(),
                }
            })
            .collect()
    }

    /// Convert string to snake_case
    fn to_snake_case(s: &str) -> String {
        s.split(|c: char| !c.is_alphanumeric())
            .filter(|s| !s.is_empty())
            .map(|s| s.to_lowercase())
            .collect::<Vec<_>>()
            .join("_")
    }
}

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

    #[test]
    fn test_generate_cli_wrapper() {
        let cli_code = BusinessLogicSeparator::generate_cli_wrapper("create", "project", None);

        assert!(cli_code.contains("#[verb(\"create\", \"project\")]"));
        assert!(cli_code.contains("pub fn create_project"));
        assert!(cli_code.contains("use crate::domain::create_project"));
    }

    #[test]
    fn test_generate_domain_skeleton() {
        let domain_code = BusinessLogicSeparator::generate_domain_skeleton("delete", "user");

        assert!(domain_code.contains("DeleteUserArgs"));
        assert!(domain_code.contains("pub async fn delete_user"));
        assert!(domain_code.contains("{% frozen id=\"business_logic\" %}"));
        assert!(domain_code.contains("#[tokio::test]"));
    }

    #[test]
    fn test_business_logic_exists() {
        let temp_dir = TempDir::new().unwrap();
        let existing_file = temp_dir.path().join("existing.rs");
        let nonexistent_file = temp_dir.path().join("nonexistent.rs");

        fs::write(&existing_file, "content").unwrap();

        assert!(BusinessLogicSeparator::business_logic_exists(
            &existing_file
        ));
        assert!(!BusinessLogicSeparator::business_logic_exists(
            &nonexistent_file
        ));
    }

    #[test]
    fn test_generate_separated_files() {
        let temp_dir = TempDir::new().unwrap();
        let cli_path = temp_dir.path().join("cli/list_task.rs");
        let domain_path = temp_dir.path().join("domain/list_task.rs");

        let result = BusinessLogicSeparator::generate_separated_files(
            &cli_path,
            &domain_path,
            "list",
            "task",
            false,
        );

        assert!(result.is_ok());
        assert!(cli_path.exists());
        assert!(domain_path.exists());

        let cli_content = fs::read_to_string(&cli_path).unwrap();

        let domain_content = fs::read_to_string(&domain_path).unwrap();
        assert!(domain_content.contains("pub async fn list_task"));
    }

    #[test]
    fn test_no_overwrite_existing_domain() {
        let temp_dir = TempDir::new().unwrap();
        let cli_path = temp_dir.path().join("cli/update_file.rs");
        let domain_path = temp_dir.path().join("domain/update_file.rs");

        // Create existing domain file with custom content
        fs::create_dir_all(domain_path.parent().unwrap()).unwrap();
        fs::write(&domain_path, "// My custom implementation").unwrap();

        let result = BusinessLogicSeparator::generate_separated_files(
            &cli_path,
            &domain_path,
            "update",
            "file",
            false, // force_domain = false
        );

        assert!(result.is_ok());

        // Domain file should NOT be overwritten
        let domain_content = fs::read_to_string(&domain_path).unwrap();
        assert!(domain_content.contains("// My custom implementation"));
        assert!(!domain_content.contains("pub async fn update_file"));
    }

    #[test]
    fn test_force_overwrite_domain() {
        let temp_dir = TempDir::new().unwrap();
        let cli_path = temp_dir.path().join("cli/update_file.rs");
        let domain_path = temp_dir.path().join("domain/update_file.rs");

        // Create existing domain file
        fs::create_dir_all(domain_path.parent().unwrap()).unwrap();
        fs::write(&domain_path, "// Old implementation").unwrap();

        let result = BusinessLogicSeparator::generate_separated_files(
            &cli_path,
            &domain_path,
            "update",
            "file",
            true, // force_domain = true
        );

        assert!(result.is_ok());

        // Domain file SHOULD be overwritten
        let domain_content = fs::read_to_string(&domain_path).unwrap();
        assert!(!domain_content.contains("// Old implementation"));
        assert!(domain_content.contains("pub async fn update_file"));
    }

    #[test]
    fn test_to_pascal_case() {
        assert_eq!(
            BusinessLogicSeparator::to_pascal_case("create-project"),
            "CreateProject"
        );
        assert_eq!(
            BusinessLogicSeparator::to_pascal_case("delete_user"),
            "DeleteUser"
        );
        assert_eq!(
            BusinessLogicSeparator::to_pascal_case("list-all-tasks"),
            "ListAllTasks"
        );
    }

    #[test]
    fn test_to_snake_case() {
        assert_eq!(
            BusinessLogicSeparator::to_snake_case("CreateProject"),
            "createproject"
        );
        assert_eq!(
            BusinessLogicSeparator::to_snake_case("delete-user"),
            "delete_user"
        );
        assert_eq!(
            BusinessLogicSeparator::to_snake_case("list-all-tasks"),
            "list_all_tasks"
        );
    }
}