ggen 4.0.0

ggen is a deterministic, language-agnostic code generation framework that treats software artifacts as projections of knowledge graphs.
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# Domain Module API Signatures

**Date**: 2025-11-20
**Architecture**: Clean Architecture with Zero CLI Dependencies

---

## Marketplace Module API

**Module**: `ggen_domain::marketplace`

### Public Functions

```rust
/// Search for packages in the marketplace
///
/// # Arguments
/// * `query` - Search query with filters
///
/// # Returns
/// * `Result<SearchResults>` - List of matching packages
///
/// # Examples
/// ```rust
/// use ggen_domain::marketplace::{search, SearchQuery};
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let query = SearchQuery {
///         query: "rust web".to_string(),
///         limit: 10,
///         category: Some("backend".to_string()),
///         min_score: Some(80.0),
///     };
///
///     let results = search(query).await?;
///     println!("Found {} packages", results.total);
///     Ok(())
/// }
/// ```
pub async fn search(query: SearchQuery) -> Result<SearchResults, MarketplaceError>

/// Install a package with dependencies
///
/// # Arguments
/// * `input` - Installation parameters
///
/// # Returns
/// * `Result<InstallResult>` - Installation details
///
/// # Examples
/// ```rust
/// use ggen_domain::marketplace::{install, InstallInput};
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let input = InstallInput {
///         package: "io.ggen.rust.microservice".to_string(),
///         target: None,
///         force: false,
///         no_dependencies: false,
///         dry_run: false,
///     };
///
///     let result = install(input).await?;
///     println!("Installed {} to {}", result.package_name, result.install_path.display());
///     Ok(())
/// }
/// ```
pub async fn install(input: InstallInput) -> Result<InstallResult, MarketplaceError>

/// Publish a package to the marketplace
///
/// # Arguments
/// * `input` - Publishing parameters
///
/// # Returns
/// * `Result<PublishResult>` - Publication details
///
/// # Examples
/// ```rust
/// use ggen_domain::marketplace::{publish, PublishInput};
/// use std::path::PathBuf;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let input = PublishInput {
///         path: PathBuf::from("./my-package"),
///         tag: Some("v1.0.0".to_string()),
///         dry_run: false,
///         force: false,
///     };
///
///     let result = publish(input).await?;
///     println!("Published {} version {}", result.package_name, result.version);
///     Ok(())
/// }
/// ```
pub async fn publish(input: PublishInput) -> Result<PublishResult, MarketplaceError>

/// Uninstall a package
///
/// # Arguments
/// * `input` - Uninstallation parameters
///
/// # Returns
/// * `Result<UninstallResult>` - Uninstallation details
pub async fn uninstall(input: UninstallInput) -> Result<UninstallResult, MarketplaceError>

/// List installed packages
///
/// # Arguments
/// * `input` - Listing parameters
///
/// # Returns
/// * `Result<ListOutput>` - List of installed packages
pub async fn list(input: ListInput) -> Result<ListOutput, MarketplaceError>

/// Validate package for production readiness
///
/// # Arguments
/// * `input` - Validation parameters
///
/// # Returns
/// * `Result<ValidationResult>` - Validation results
pub async fn validate(input: ValidateInput) -> Result<ValidationResult, MarketplaceError>
```

---

## Template Module API

**Module**: `ggen_domain::template`

### Public Functions

```rust
/// Generate code from template
///
/// # Arguments
/// * `input` - Generation parameters
///
/// # Returns
/// * `Result<GenerateResult>` - Generated files
///
/// # Examples
/// ```rust
/// use ggen_domain::template::{generate, GenerateInput};
/// use std::path::PathBuf;
/// use std::collections::BTreeMap;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let mut vars = BTreeMap::new();
///     vars.insert("name".to_string(), "MyProject".to_string());
///
///     let input = GenerateInput {
///         template: "rust-microservice".to_string(),
///         output_dir: PathBuf::from("./output"),
///         vars,
///         force: false,
///     };
///
///     let result = generate(input).await?;
///     println!("Generated {} files", result.total_files);
///     Ok(())
/// }
/// ```
pub async fn generate(input: GenerateInput) -> Result<GenerateResult, TemplateError>

/// Validate template syntax
///
/// # Arguments
/// * `input` - Validation parameters
///
/// # Returns
/// * `Result<ValidationResult>` - Validation results
///
/// # Examples
/// ```rust
/// use ggen_domain::template::{validate, ValidateInput};
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let input = ValidateInput {
///         template: "my-template".to_string(),
///     };
///
///     let result = validate(input).await?;
///     if !result.valid {
///         for error in result.errors {
///             println!("Error at line {}: {}", error.line, error.message);
///         }
///     }
///     Ok(())
/// }
/// ```
pub async fn validate(input: ValidateInput) -> Result<ValidationResult, TemplateError>

/// Discover templates in directories
///
/// # Arguments
/// * `input` - Discovery parameters
///
/// # Returns
/// * `Result<DiscoveryResult>` - Discovered templates
///
/// # Examples
/// ```rust
/// use ggen_domain::template::{discover, DiscoverInput};
/// use std::path::PathBuf;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let input = DiscoverInput {
///         search_dirs: vec![PathBuf::from("./templates")],
///         pattern: Some("*.tmpl".to_string()),
///     };
///
///     let result = discover(input).await?;
///     println!("Found {} templates", result.total);
///     Ok(())
/// }
/// ```
pub async fn discover(input: DiscoverInput) -> Result<DiscoveryResult, TemplateError>

/// List all available templates
///
/// # Arguments
/// * `input` - Listing parameters
///
/// # Returns
/// * `Result<ListOutput>` - List of templates
pub async fn list(input: ListInput) -> Result<ListOutput, TemplateError>

/// Show template metadata
///
/// # Arguments
/// * `input` - Show parameters
///
/// # Returns
/// * `Result<ShowOutput>` - Template details
pub async fn show(input: ShowInput) -> Result<ShowOutput, TemplateError>

/// Create new template
///
/// # Arguments
/// * `input` - Creation parameters
///
/// # Returns
/// * `Result<CreateResult>` - Created template details
pub async fn create(input: CreateInput) -> Result<CreateResult, TemplateError>
```

---

## Project Module API

**Module**: `ggen_domain::project`

### Public Functions

```rust
/// Create a new project from template
///
/// # Arguments
/// * `input` - Creation parameters
///
/// # Returns
/// * `Result<CreateResult>` - Created project details
///
/// # Examples
/// ```rust
/// use ggen_domain::project::{create, CreateInput};
/// use std::path::PathBuf;
/// use std::collections::BTreeMap;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let mut vars = BTreeMap::new();
///     vars.insert("author".to_string(), "Alice".to_string());
///
///     let input = CreateInput {
///         name: "my-project".to_string(),
///         template: Some("rust-web-app".to_string()),
///         output_dir: PathBuf::from("./projects"),
///         vars,
///     };
///
///     let result = create(input).await?;
///     println!("Created project at {}", result.project_path.display());
///     println!("Generated {} files", result.files_created.len());
///     Ok(())
/// }
/// ```
pub async fn create(input: CreateInput) -> Result<CreateResult, ProjectError>

/// Configure project settings
///
/// # Arguments
/// * `input` - Configuration parameters
///
/// # Returns
/// * `Result<ConfigureResult>` - Configuration result
///
/// # Examples
/// ```rust
/// use ggen_domain::project::{configure, ConfigureInput, ProjectConfig};
/// use std::path::PathBuf;
/// use std::collections::BTreeMap;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let mut settings = BTreeMap::new();
///     settings.insert("rust_edition".to_string(), "2021".to_string());
///
///     let config = ProjectConfig {
///         name: "my-project".to_string(),
///         version: "1.0.0".to_string(),
///         description: Some("My project description".to_string()),
///         author: Some("Alice <alice@example.com>".to_string()),
///         settings,
///     };
///
///     let input = ConfigureInput {
///         project_path: PathBuf::from("./my-project"),
///         config,
///     };
///
///     let result = configure(input).await?;
///     println!("Configuration updated: {}", result.config_updated);
///     Ok(())
/// }
/// ```
pub async fn configure(input: ConfigureInput) -> Result<ConfigureResult, ProjectError>

/// Generate project files from plan
///
/// # Arguments
/// * `input` - Generation parameters
///
/// # Returns
/// * `Result<GenerateResult>` - Generation result
///
/// # Examples
/// ```rust
/// use ggen_domain::project::{generate, GenerateInput};
/// use std::path::PathBuf;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let input = GenerateInput {
///         project_path: PathBuf::from("./my-project"),
///         plan: None, // Will use default plan
///         force: false,
///     };
///
///     let result = generate(input).await?;
///     println!("Generated {} files", result.total_files);
///     for file in result.files_generated {
///         println!("  - {}", file.display());
///     }
///     Ok(())
/// }
/// ```
pub async fn generate(input: GenerateInput) -> Result<GenerateResult, ProjectError>

/// List available project configurations
///
/// # Arguments
/// * `input` - Listing parameters
///
/// # Returns
/// * `Result<ListOutput>` - List of projects
pub async fn list(input: ListInput) -> Result<ListOutput, ProjectError>

/// Initialize project structure
///
/// # Arguments
/// * `input` - Initialization parameters
///
/// # Returns
/// * `Result<InitResult>` - Initialization result
pub async fn init(input: InitInput) -> Result<InitResult, ProjectError>

/// Build project plan
///
/// # Arguments
/// * `input` - Planning parameters
///
/// # Returns
/// * `Result<PlanResult>` - Generated plan
pub async fn plan(input: PlanInput) -> Result<PlanResult, ProjectError>

/// Apply project changes
///
/// # Arguments
/// * `input` - Apply parameters
///
/// # Returns
/// * `Result<ApplyResult>` - Apply result
pub async fn apply(input: ApplyInput) -> Result<ApplyResult, ProjectError>
```

---

## Type System Overview

### Naming Conventions

All types follow consistent naming patterns:

1. **Input Types**: Suffix with `Input`
   - `SearchQuery` (exception: common term)
   - `InstallInput`
   - `PublishInput`
   - `GenerateInput`

2. **Output Types**: Suffix with `Result`, `Output`, `Info`, or domain-specific names
   - `SearchResults`
   - `InstallResult`
   - `ListOutput`
   - `PackageInfo`
   - `TemplateMetadata`

3. **Error Types**: Suffix with `Error`
   - `MarketplaceError`
   - `TemplateError`
   - `ProjectError`

### Derive Traits

All input/output types must derive:

```rust
#[derive(Debug, Clone, Serialize, Deserialize)]
```

All error types must derive:

```rust
#[derive(Debug, Error, Serialize)]
```

### Function Return Types

All public functions return:

```rust
pub async fn function_name(input: InputType) -> Result<OutputType, ErrorType>
```

---

## CLI Integration Example

```rust
// ggen-cli/src/cmds/marketplace.rs
use clap_noun_verb::Result;
use clap_noun_verb_macros::verb;
use serde::Serialize;

use crate::runtime_helper::execute_async_verb;
use ggen_domain::marketplace::{search, SearchQuery};

#[derive(Serialize)]
struct SearchOutput {
    packages: Vec<PackageInfo>,
    total: usize,
}

#[verb]
fn search(query: String, limit: Option<usize>, category: Option<String>) -> Result<SearchOutput> {
    let input = SearchQuery {
        query,
        limit: limit.unwrap_or(10),
        category,
        min_score: None,
    };

    execute_async_verb(async move {
        let results = ggen_domain::marketplace::search(input)
            .await
            .map_err(|e| clap_noun_verb::NounVerbError::execution_error(e.to_string()))?;

        Ok(SearchOutput {
            packages: results.packages,
            total: results.total,
        })
    })
}
```

---

## Web API Integration Example

```rust
// web-api/src/routes/marketplace.rs
use axum::{Json, extract::Query};
use ggen_domain::marketplace::{search, SearchQuery, SearchResults};

async fn search_endpoint(
    Query(params): Query<SearchQuery>
) -> Result<Json<SearchResults>, StatusCode> {
    search(params)
        .await
        .map(Json)
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}
```

---

## Agent Integration Example

```rust
// agents/marketplace_agent.rs
use ggen_domain::marketplace::{search, SearchQuery, PackageInfo};

pub async fn find_production_ready_packages(domain: &str) -> Vec<PackageInfo> {
    let query = SearchQuery {
        query: domain.to_string(),
        limit: 20,
        category: Some("production".to_string()),
        min_score: Some(90.0),
    };

    search(query)
        .await
        .ok()
        .map(|r| r.packages)
        .unwrap_or_default()
}
```

---

## Testing Examples

### Unit Test (Chicago TDD)

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

    #[tokio::test]
    async fn test_search_returns_results() {
        // ARRANGE
        let query = SearchQuery {
            query: "rust web".to_string(),
            limit: 5,
            category: None,
            min_score: None,
        };

        // ACT
        let results = search(query).await.unwrap();

        // ASSERT - Verify observable outputs
        assert!(results.total > 0);
        assert!(results.packages.len() <= 5);
        assert!(results.packages.iter().any(|p| p.name.contains("rust")));
    }

    #[tokio::test]
    async fn test_search_filters_by_category() {
        // ARRANGE
        let query = SearchQuery {
            query: "microservice".to_string(),
            limit: 10,
            category: Some("backend".to_string()),
            min_score: None,
        };

        // ACT
        let results = search(query).await.unwrap();

        // ASSERT - Verify filtering behavior
        assert!(results.packages.iter().all(|p| {
            // Verify category filtering logic
            true // Placeholder for actual verification
        }));
    }
}
```

### Integration Test

```rust
#[tokio::test]
async fn test_install_creates_files() {
    use tempfile::TempDir;

    // ARRANGE
    let temp_dir = TempDir::new().unwrap();
    let input = InstallInput {
        package: "io.ggen.test-package".to_string(),
        target: Some(temp_dir.path().to_path_buf()),
        force: false,
        no_dependencies: false,
        dry_run: false,
    };

    // ACT
    let result = install(input).await.unwrap();

    // ASSERT - Verify state changes
    assert!(result.install_path.exists());
    assert_eq!(result.package_name, "io.ggen.test-package");
    assert!(result.install_path.join("package.toml").exists());
}
```

---

## Performance Requirements

All domain functions must meet:

| Operation | Target Latency | Notes |
|-----------|---------------|-------|
| search() | ≤ 100ms | In-memory index |
| search() | ≤ 500ms | Disk-based search |
| install() | ≤ 3s | Small packages (< 10MB) |
| publish() | ≤ 5s | Including validation |
| generate() | ≤ 2s | Templates with < 100 files |
| validate() | ≤ 500ms | Single package |
| list() | ≤ 200ms | < 1000 items |

---

**API Design Complete** ✅
**Ready for Implementation** ⚡