mockforge-plugin-core 0.3.116

Core plugin interfaces and types for MockForge extensible architecture
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
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
//! Backend Generator Plugin Interface
//!
//! This module defines the traits and types for plugins that generate
//! framework-specific backend server code from OpenAPI specifications.

use crate::client_generator::{GeneratedFile, OpenApiSpec};
use crate::types::{PluginMetadata, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Backend generator plugin trait for generating framework-specific backend servers
#[async_trait::async_trait]
pub trait BackendGeneratorPlugin: Send + Sync {
    /// Get the backend type/framework name this plugin supports
    fn backend_type(&self) -> &str;

    /// Get a human-readable name for this backend
    fn backend_name(&self) -> &str;

    /// Get the supported OpenAPI spec versions
    fn supported_spec_versions(&self) -> Vec<&str>;

    /// Get the supported file extensions for generated code
    fn supported_extensions(&self) -> Vec<&str>;

    /// Generate backend server code from OpenAPI specification
    async fn generate_backend(
        &self,
        spec: &OpenApiSpec,
        config: &BackendGeneratorConfig,
    ) -> Result<BackendGenerationResult>;

    /// Get plugin metadata
    async fn get_metadata(&self) -> PluginMetadata;

    /// Validate the plugin configuration
    async fn validate_config(&self, _config: &BackendGeneratorConfig) -> Result<()> {
        // Default implementation - plugins can override for custom validation
        Ok(())
    }

    /// Check if this generator supports the given database type
    fn supports_database(&self, db_type: &str) -> bool;

    /// Get default port for this backend type
    fn default_port(&self) -> u16;
}

/// Backend generator configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendGeneratorConfig {
    /// Output directory for generated files
    pub output_dir: String,
    /// Server port (default varies by backend)
    pub port: Option<u16>,
    /// Base URL for the API
    pub base_url: Option<String>,
    /// Whether to generate test files
    pub with_tests: bool,
    /// Whether to generate API documentation stubs
    pub with_docs: bool,
    /// Database type for integration hints (postgres, mysql, sqlite, mongo, etc.)
    pub database: Option<String>,
    /// Whether to generate TODO.md file
    pub generate_todo_md: bool,
    /// Additional configuration options
    pub options: HashMap<String, serde_json::Value>,
}

impl Default for BackendGeneratorConfig {
    fn default() -> Self {
        Self {
            output_dir: "./generated-backend".to_string(),
            port: None,
            base_url: None,
            with_tests: false,
            with_docs: false,
            database: None,
            generate_todo_md: true,
            options: HashMap::new(),
        }
    }
}

/// Result of backend generation
#[derive(Debug, Clone)]
pub struct BackendGenerationResult {
    /// Generated files with their content
    pub files: Vec<GeneratedFile>,
    /// Generation warnings
    pub warnings: Vec<String>,
    /// Generation metadata
    pub metadata: BackendGenerationMetadata,
    /// TODO items extracted from generated code
    pub todos: Vec<TodoItem>,
}

/// Generation metadata for backend code
#[derive(Debug, Clone)]
pub struct BackendGenerationMetadata {
    /// Backend framework name
    pub framework: String,
    /// Generated backend name
    pub backend_name: String,
    /// API title from spec
    pub api_title: String,
    /// API version from spec
    pub api_version: String,
    /// Number of operations/endpoints generated
    pub operation_count: usize,
    /// Number of schemas/models generated
    pub schema_count: usize,
    /// Default port for the server
    pub default_port: u16,
}

/// TODO item extracted from generated code
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TodoItem {
    /// TODO description
    pub description: String,
    /// File path where TODO was found
    pub file_path: String,
    /// Line number where TODO was found
    pub line_number: usize,
    /// Operation/endpoint this TODO relates to
    pub related_operation: Option<String>,
    /// Category (handler, model, config, test, etc.)
    pub category: TodoCategory,
    /// Definition of Done criteria
    pub definition_of_done: Vec<String>,
    /// Estimated complexity (Low, Medium, High)
    pub complexity: Complexity,
    /// Dependencies on other TODOs
    pub dependencies: Vec<String>,
}

/// TODO category
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TodoCategory {
    /// Handler implementation TODO
    Handler,
    /// Model/schema TODO
    Model,
    /// Configuration TODO
    Config,
    /// Test TODO
    Test,
    /// Documentation TODO
    Docs,
    /// Database/migration TODO
    Database,
    /// Other
    Other,
}

impl std::fmt::Display for TodoCategory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TodoCategory::Handler => write!(f, "Handler"),
            TodoCategory::Model => write!(f, "Model"),
            TodoCategory::Config => write!(f, "Configuration"),
            TodoCategory::Test => write!(f, "Testing"),
            TodoCategory::Docs => write!(f, "Documentation"),
            TodoCategory::Database => write!(f, "Database"),
            TodoCategory::Other => write!(f, "Other"),
        }
    }
}

/// Estimated complexity level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Complexity {
    /// Low complexity - straightforward implementation
    Low,
    /// Medium complexity - requires some design decisions
    Medium,
    /// High complexity - complex logic or architecture
    High,
}

impl std::fmt::Display for Complexity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Complexity::Low => write!(f, "Low"),
            Complexity::Medium => write!(f, "Medium"),
            Complexity::High => write!(f, "High"),
        }
    }
}

/// Backend generator plugin configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendGeneratorPluginConfig {
    /// Plugin name
    pub name: String,
    /// Backend type/framework
    pub backend_type: String,
    /// Plugin-specific options
    pub options: HashMap<String, serde_json::Value>,
}

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

    // ==================== BackendGeneratorConfig Tests ====================

    #[test]
    fn test_backend_generator_config_default() {
        let config = BackendGeneratorConfig::default();

        assert_eq!(config.output_dir, "./generated-backend");
        assert!(config.port.is_none());
        assert!(config.base_url.is_none());
        assert!(!config.with_tests);
        assert!(!config.with_docs);
        assert!(config.database.is_none());
        assert!(config.generate_todo_md);
        assert!(config.options.is_empty());
    }

    #[test]
    fn test_backend_generator_config_custom() {
        let config = BackendGeneratorConfig {
            output_dir: "/custom/output".to_string(),
            port: Some(8080),
            base_url: Some("http://localhost".to_string()),
            with_tests: true,
            with_docs: true,
            database: Some("postgres".to_string()),
            generate_todo_md: false,
            options: HashMap::new(),
        };

        assert_eq!(config.output_dir, "/custom/output");
        assert_eq!(config.port, Some(8080));
        assert_eq!(config.base_url, Some("http://localhost".to_string()));
        assert!(config.with_tests);
        assert!(config.with_docs);
        assert_eq!(config.database, Some("postgres".to_string()));
        assert!(!config.generate_todo_md);
    }

    #[test]
    fn test_backend_generator_config_clone() {
        let config = BackendGeneratorConfig {
            output_dir: "./test".to_string(),
            port: Some(3000),
            ..Default::default()
        };

        let cloned = config.clone();
        assert_eq!(cloned.output_dir, config.output_dir);
        assert_eq!(cloned.port, config.port);
    }

    #[test]
    fn test_backend_generator_config_serialization() {
        let config = BackendGeneratorConfig::default();

        let json = serde_json::to_string(&config).unwrap();
        let deserialized: BackendGeneratorConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.output_dir, config.output_dir);
        assert_eq!(deserialized.with_tests, config.with_tests);
    }

    #[test]
    fn test_backend_generator_config_with_options() {
        let mut options = HashMap::new();
        options.insert("key1".to_string(), serde_json::json!("value1"));
        options.insert("count".to_string(), serde_json::json!(42));

        let config = BackendGeneratorConfig {
            options,
            ..Default::default()
        };

        assert_eq!(config.options.len(), 2);
        assert_eq!(config.options.get("key1"), Some(&serde_json::json!("value1")));
    }

    // ==================== BackendGenerationMetadata Tests ====================

    #[test]
    fn test_backend_generation_metadata() {
        let metadata = BackendGenerationMetadata {
            framework: "axum".to_string(),
            backend_name: "MyApi".to_string(),
            api_title: "My API".to_string(),
            api_version: "1.0.0".to_string(),
            operation_count: 10,
            schema_count: 5,
            default_port: 8080,
        };

        assert_eq!(metadata.framework, "axum");
        assert_eq!(metadata.backend_name, "MyApi");
        assert_eq!(metadata.api_title, "My API");
        assert_eq!(metadata.api_version, "1.0.0");
        assert_eq!(metadata.operation_count, 10);
        assert_eq!(metadata.schema_count, 5);
        assert_eq!(metadata.default_port, 8080);
    }

    #[test]
    fn test_backend_generation_metadata_clone() {
        let metadata = BackendGenerationMetadata {
            framework: "actix-web".to_string(),
            backend_name: "TestBackend".to_string(),
            api_title: "Test".to_string(),
            api_version: "2.0.0".to_string(),
            operation_count: 20,
            schema_count: 10,
            default_port: 3000,
        };

        let cloned = metadata.clone();
        assert_eq!(cloned.framework, metadata.framework);
        assert_eq!(cloned.operation_count, metadata.operation_count);
    }

    // ==================== TodoCategory Tests ====================

    #[test]
    fn test_todo_category_display_handler() {
        assert_eq!(format!("{}", TodoCategory::Handler), "Handler");
    }

    #[test]
    fn test_todo_category_display_model() {
        assert_eq!(format!("{}", TodoCategory::Model), "Model");
    }

    #[test]
    fn test_todo_category_display_config() {
        assert_eq!(format!("{}", TodoCategory::Config), "Configuration");
    }

    #[test]
    fn test_todo_category_display_test() {
        assert_eq!(format!("{}", TodoCategory::Test), "Testing");
    }

    #[test]
    fn test_todo_category_display_docs() {
        assert_eq!(format!("{}", TodoCategory::Docs), "Documentation");
    }

    #[test]
    fn test_todo_category_display_database() {
        assert_eq!(format!("{}", TodoCategory::Database), "Database");
    }

    #[test]
    fn test_todo_category_display_other() {
        assert_eq!(format!("{}", TodoCategory::Other), "Other");
    }

    #[test]
    fn test_todo_category_equality() {
        assert_eq!(TodoCategory::Handler, TodoCategory::Handler);
        assert_ne!(TodoCategory::Handler, TodoCategory::Model);
    }

    #[test]
    fn test_todo_category_serialization() {
        let category = TodoCategory::Handler;
        let json = serde_json::to_string(&category).unwrap();
        let deserialized: TodoCategory = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, category);
    }

    // ==================== Complexity Tests ====================

    #[test]
    fn test_complexity_display_low() {
        assert_eq!(format!("{}", Complexity::Low), "Low");
    }

    #[test]
    fn test_complexity_display_medium() {
        assert_eq!(format!("{}", Complexity::Medium), "Medium");
    }

    #[test]
    fn test_complexity_display_high() {
        assert_eq!(format!("{}", Complexity::High), "High");
    }

    #[test]
    fn test_complexity_equality() {
        assert_eq!(Complexity::Low, Complexity::Low);
        assert_ne!(Complexity::Low, Complexity::High);
    }

    #[test]
    fn test_complexity_serialization() {
        let complexity = Complexity::Medium;
        let json = serde_json::to_string(&complexity).unwrap();
        let deserialized: Complexity = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, complexity);
    }

    // ==================== TodoItem Tests ====================

    #[test]
    fn test_todo_item_creation() {
        let todo = TodoItem {
            description: "Implement user authentication".to_string(),
            file_path: "src/handlers/auth.rs".to_string(),
            line_number: 42,
            related_operation: Some("login".to_string()),
            category: TodoCategory::Handler,
            definition_of_done: vec!["Add JWT validation".to_string()],
            complexity: Complexity::Medium,
            dependencies: vec![],
        };

        assert_eq!(todo.description, "Implement user authentication");
        assert_eq!(todo.file_path, "src/handlers/auth.rs");
        assert_eq!(todo.line_number, 42);
        assert_eq!(todo.related_operation, Some("login".to_string()));
        assert_eq!(todo.category, TodoCategory::Handler);
        assert_eq!(todo.complexity, Complexity::Medium);
    }

    #[test]
    fn test_todo_item_without_operation() {
        let todo = TodoItem {
            description: "Configure logging".to_string(),
            file_path: "src/config.rs".to_string(),
            line_number: 10,
            related_operation: None,
            category: TodoCategory::Config,
            definition_of_done: vec![],
            complexity: Complexity::Low,
            dependencies: vec![],
        };

        assert!(todo.related_operation.is_none());
        assert!(todo.definition_of_done.is_empty());
    }

    #[test]
    fn test_todo_item_with_dependencies() {
        let todo = TodoItem {
            description: "Implement order processing".to_string(),
            file_path: "src/handlers/orders.rs".to_string(),
            line_number: 100,
            related_operation: Some("processOrder".to_string()),
            category: TodoCategory::Handler,
            definition_of_done: vec!["Handle edge cases".to_string()],
            complexity: Complexity::High,
            dependencies: vec!["auth_todo".to_string(), "db_todo".to_string()],
        };

        assert_eq!(todo.dependencies.len(), 2);
        assert!(todo.dependencies.contains(&"auth_todo".to_string()));
    }

    #[test]
    fn test_todo_item_clone() {
        let todo = TodoItem {
            description: "Test item".to_string(),
            file_path: "test.rs".to_string(),
            line_number: 1,
            related_operation: None,
            category: TodoCategory::Test,
            definition_of_done: vec![],
            complexity: Complexity::Low,
            dependencies: vec![],
        };

        let cloned = todo.clone();
        assert_eq!(cloned.description, todo.description);
        assert_eq!(cloned.category, todo.category);
    }

    #[test]
    fn test_todo_item_serialization() {
        let todo = TodoItem {
            description: "Serialize test".to_string(),
            file_path: "test.rs".to_string(),
            line_number: 1,
            related_operation: Some("test".to_string()),
            category: TodoCategory::Test,
            definition_of_done: vec!["Done".to_string()],
            complexity: Complexity::Low,
            dependencies: vec![],
        };

        let json = serde_json::to_string(&todo).unwrap();
        let deserialized: TodoItem = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.description, todo.description);
        assert_eq!(deserialized.category, todo.category);
    }

    // ==================== BackendGeneratorPluginConfig Tests ====================

    #[test]
    fn test_backend_generator_plugin_config() {
        let config = BackendGeneratorPluginConfig {
            name: "axum-generator".to_string(),
            backend_type: "axum".to_string(),
            options: HashMap::new(),
        };

        assert_eq!(config.name, "axum-generator");
        assert_eq!(config.backend_type, "axum");
        assert!(config.options.is_empty());
    }

    #[test]
    fn test_backend_generator_plugin_config_with_options() {
        let mut options = HashMap::new();
        options.insert("feature_flags".to_string(), serde_json::json!(["auth", "logging"]));

        let config = BackendGeneratorPluginConfig {
            name: "custom-generator".to_string(),
            backend_type: "custom".to_string(),
            options,
        };

        assert_eq!(config.options.len(), 1);
    }

    #[test]
    fn test_backend_generator_plugin_config_clone() {
        let config = BackendGeneratorPluginConfig {
            name: "test".to_string(),
            backend_type: "test".to_string(),
            options: HashMap::new(),
        };

        let cloned = config.clone();
        assert_eq!(cloned.name, config.name);
    }

    #[test]
    fn test_backend_generator_plugin_config_serialization() {
        let config = BackendGeneratorPluginConfig {
            name: "test-plugin".to_string(),
            backend_type: "actix".to_string(),
            options: HashMap::new(),
        };

        let json = serde_json::to_string(&config).unwrap();
        let deserialized: BackendGeneratorPluginConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.name, config.name);
        assert_eq!(deserialized.backend_type, config.backend_type);
    }

    // ==================== BackendGenerationResult Tests ====================

    #[test]
    fn test_backend_generation_result() {
        use crate::client_generator::GeneratedFile;

        let result = BackendGenerationResult {
            files: vec![GeneratedFile {
                path: "src/main.rs".to_string(),
                content: "fn main() {}".to_string(),
                file_type: "rust".to_string(),
            }],
            warnings: vec!["Warning 1".to_string()],
            metadata: BackendGenerationMetadata {
                framework: "axum".to_string(),
                backend_name: "Test".to_string(),
                api_title: "Test API".to_string(),
                api_version: "1.0.0".to_string(),
                operation_count: 5,
                schema_count: 3,
                default_port: 8080,
            },
            todos: vec![],
        };

        assert_eq!(result.files.len(), 1);
        assert_eq!(result.warnings.len(), 1);
        assert!(result.todos.is_empty());
        assert_eq!(result.metadata.framework, "axum");
    }

    #[test]
    fn test_backend_generation_result_clone() {
        use crate::client_generator::GeneratedFile;

        let result = BackendGenerationResult {
            files: vec![GeneratedFile {
                path: "test.rs".to_string(),
                content: "test".to_string(),
                file_type: "rust".to_string(),
            }],
            warnings: vec![],
            metadata: BackendGenerationMetadata {
                framework: "test".to_string(),
                backend_name: "test".to_string(),
                api_title: "Test".to_string(),
                api_version: "1.0.0".to_string(),
                operation_count: 0,
                schema_count: 0,
                default_port: 8080,
            },
            todos: vec![],
        };

        let cloned = result.clone();
        assert_eq!(cloned.files.len(), result.files.len());
        assert_eq!(cloned.metadata.framework, result.metadata.framework);
    }
}