dampen-cli 0.3.2

Developer CLI for Dampen UI framework
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
//! File generation logic for creating window files from templates.

use crate::commands::add::errors::GenerationError;
use crate::commands::add::integration::add_module_to_mod_rs;
use crate::commands::add::templates::{TemplateKind, WindowTemplate};
use crate::commands::add::validation::{TargetPath, WindowName};
use crate::commands::add::view_switching::{activate_view_switching, detect_second_window};
use std::fs;
use std::path::PathBuf;

/// Result of file generation
#[derive(Debug, Clone)]
pub struct GeneratedFiles {
    /// Path to generated .rs file
    pub rust_file: PathBuf,

    /// Path to generated .dampen file
    pub dampen_file: PathBuf,

    /// Validated window name
    pub window_name: WindowName,

    /// Target directory where files were created
    pub target_dir: PathBuf,

    /// Path to mod.rs that was updated (if integration was performed)
    pub updated_mod_file: Option<PathBuf>,

    /// Whether main.rs was modified to enable view switching
    pub view_switching_activated: bool,
}

impl GeneratedFiles {
    /// Generate a success message showing what was created
    pub fn success_message(&self) -> String {
        let mut message = format!(
            "✓ Created UI window '{}'\n{}\n{}",
            self.window_name.snake,
            self.rust_file.display(),
            self.dampen_file.display()
        );

        // Show mod.rs update if integration was performed
        if let Some(mod_file) = &self.updated_mod_file {
            message.push_str(&format!("\n  → Updated {}", mod_file.display()));
        }

        // Show view switching activation
        if self.view_switching_activated {
            message.push_str("\n  → Activated multi-view in src/main.rs");
        }

        message.push_str("\n\nNext steps:");

        // Only show manual mod.rs step if integration was NOT performed
        if self.updated_mod_file.is_none() {
            message.push_str(&format!(
                "\n  1. Add `pub mod {};` to src/ui/mod.rs",
                self.window_name.snake
            ));
            message.push_str("\n  2. Run `dampen check` to validate");
            message.push_str("\n  3. Run your application to see the new window");
        } else {
            message.push_str("\n  1. Run `dampen check` to validate");
            message.push_str("\n  2. Run your application to see the new window");
        }

        message
    }
}

/// Generate window files (.rs and .dampen) from templates
///
/// # Arguments
///
/// * `target_path` - Validated target path with project context
/// * `window_name` - Validated window name with case variants
/// * `enable_integration` - Whether to automatically add module to mod.rs
///
/// # Returns
///
/// `GeneratedFiles` struct with paths to created files
///
/// # Errors
///
/// Returns `GenerationError` if:
/// - Files already exist (prevents overwriting)
/// - Directory creation fails
/// - File write operations fail
///
/// # Note
///
/// Integration errors (mod.rs updates) are non-fatal and emitted as warnings
pub fn generate_window_files(
    target_path: &TargetPath,
    window_name: &WindowName,
    enable_integration: bool,
) -> Result<GeneratedFiles, GenerationError> {
    // 1. Check if files already exist (prevent overwriting)
    let rust_file = target_path.file_path(&window_name.snake, "rs");
    let dampen_file = target_path.file_path(&window_name.snake, "dampen");

    if rust_file.exists() {
        return Err(GenerationError::FileExists {
            window_name: window_name.snake.clone(),
            path: rust_file,
        });
    }

    if dampen_file.exists() {
        return Err(GenerationError::FileExists {
            window_name: window_name.snake.clone(),
            path: dampen_file,
        });
    }

    // 2. Create target directory if it doesn't exist
    fs::create_dir_all(&target_path.absolute).map_err(|e| GenerationError::DirectoryCreation {
        path: target_path.absolute.clone(),
        source: e,
    })?;

    // 3. Load and render templates
    let rust_template = WindowTemplate::load(TemplateKind::RustModule);
    let dampen_template = WindowTemplate::load(TemplateKind::DampenXml);

    let variants = window_name.to_variants();
    let rust_content = rust_template.render(&variants);
    let dampen_content = dampen_template.render(&variants);

    // 4. Write .rs file
    fs::write(&rust_file, rust_content).map_err(|e| GenerationError::FileWrite {
        path: rust_file.clone(),
        source: e,
    })?;

    // 5. Write .dampen file (with cleanup on error)
    if let Err(e) = fs::write(&dampen_file, dampen_content) {
        // Cleanup: remove .rs file if .dampen write fails
        let _ = fs::remove_file(&rust_file);
        return Err(GenerationError::FileWrite {
            path: dampen_file,
            source: e,
        });
    }

    // 6. Attempt mod.rs integration if enabled
    let updated_mod_file = if enable_integration {
        match add_module_to_mod_rs(
            &target_path.project_root,
            &target_path.absolute,
            &window_name.snake,
        ) {
            Ok(mod_path) => {
                eprintln!("✓ Updated {}", mod_path.display());
                Some(mod_path)
            }
            Err(e) => {
                eprintln!("⚠ Warning: Failed to update mod.rs: {}", e);
                eprintln!(
                    "  Please manually add `pub mod {};` to the appropriate mod.rs file",
                    window_name.snake
                );
                None
            }
        }
    } else {
        None
    };

    // 7. Activate view switching if this is the second window
    let view_switching_activated = if enable_integration {
        match detect_second_window(&target_path.project_root) {
            Ok(true) => {
                // This is the second window, activate view switching
                match activate_view_switching(&target_path.project_root) {
                    Ok(()) => {
                        eprintln!("✓ Activated multi-view in src/main.rs");
                        true
                    }
                    Err(e) => {
                        eprintln!("⚠ Warning: Failed to activate view switching: {}", e);
                        eprintln!(
                            "  You may need to manually enable multi-view support in src/main.rs"
                        );
                        false
                    }
                }
            }
            Ok(false) => false,
            Err(e) => {
                eprintln!("⚠ Warning: Could not detect second window: {}", e);
                false
            }
        }
    } else {
        false
    };

    Ok(GeneratedFiles {
        rust_file,
        dampen_file,
        window_name: window_name.clone(),
        target_dir: target_path.absolute.clone(),
        updated_mod_file,
        view_switching_activated,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::add::validation::{TargetPath, WindowName};
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_generate_files_default_path() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();
        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("settings").unwrap();

        let result = generate_window_files(&target_path, &window_name, false);

        assert!(result.is_ok());
        let generated = result.unwrap();
        assert_eq!(generated.rust_file, project_root.join("src/ui/settings.rs"));
        assert_eq!(
            generated.dampen_file,
            project_root.join("src/ui/settings.dampen")
        );
        assert!(generated.rust_file.exists());
        assert!(generated.dampen_file.exists());
    }

    #[test]
    fn test_generate_files_creates_directory() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();
        let target_path = TargetPath::resolve(project_root, Some("src/ui/admin")).unwrap();
        let window_name = WindowName::new("dashboard").unwrap();

        // Directory doesn't exist yet
        assert!(!target_path.absolute.exists());

        let result = generate_window_files(&target_path, &window_name, false);

        assert!(result.is_ok());
        assert!(target_path.absolute.exists());
        assert!(target_path.absolute.join("dashboard.rs").exists());
        assert!(target_path.absolute.join("dashboard.dampen").exists());
    }

    #[test]
    fn test_generate_files_rs_content() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();
        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("user_profile").unwrap();

        let result = generate_window_files(&target_path, &window_name, false);

        assert!(result.is_ok());
        let content = fs::read_to_string(target_path.absolute.join("user_profile.rs")).unwrap();

        // Check for key patterns in generated Rust file
        assert!(
            content.contains("user_profile"),
            "Should contain module name"
        );
        assert!(
            content.contains("pub struct Model"),
            "Should contain Model struct"
        );
        assert!(
            content.contains("#[dampen_ui"),
            "Should contain dampen_ui attribute"
        );
        assert!(
            content.contains("create_app_state"),
            "Should contain create_app_state function"
        );
    }

    #[test]
    fn test_generate_files_dampen_content() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();
        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("settings").unwrap();

        let result = generate_window_files(&target_path, &window_name, false);

        assert!(result.is_ok());
        let content = fs::read_to_string(target_path.absolute.join("settings.dampen")).unwrap();

        // Check for key patterns in generated XML file
        assert!(
            !content.contains("<?xml"),
            "Should NOT have XML declaration"
        );
        assert!(
            content.contains("<dampen version=\"1.1\" encoding=\"utf-8\">"),
            "Should have correct root element"
        );
        assert!(content.contains("<column"), "Should have column layout");
        assert!(content.contains("Settings"), "Should contain window title");
    }

    #[test]
    fn test_generate_files_rejects_existing_rs() {
        // T101: Test that existing .rs file prevents generation
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();
        let target_dir = project_root.join("src/ui");
        fs::create_dir_all(&target_dir).unwrap();

        // Create existing .rs file
        let existing_file = target_dir.join("settings.rs");
        fs::write(&existing_file, "existing content").unwrap();

        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("settings").unwrap();
        let result = generate_window_files(&target_path, &window_name, false);

        assert!(result.is_err());
        match result {
            Err(GenerationError::FileExists {
                window_name: name,
                path,
            }) => {
                assert_eq!(name, "settings");
                assert_eq!(path, existing_file);
            }
            _ => panic!("Expected FileExists error"),
        }
    }

    #[test]
    fn test_generate_files_rejects_existing_dampen() {
        // T102: Test that existing .dampen file prevents generation
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();
        let target_dir = project_root.join("src/ui");
        fs::create_dir_all(&target_dir).unwrap();

        // Create existing .dampen file
        let existing_file = target_dir.join("dashboard.dampen");
        fs::write(&existing_file, "existing xml content").unwrap();

        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("dashboard").unwrap();
        let result = generate_window_files(&target_path, &window_name, false);

        assert!(result.is_err());
        match result {
            Err(GenerationError::FileExists {
                window_name: name,
                path,
            }) => {
                assert_eq!(name, "dashboard");
                assert_eq!(path, existing_file);
            }
            _ => panic!("Expected FileExists error"),
        }
    }

    #[test]
    fn test_generate_files_rejects_partial_conflict() {
        // T103: Test that partial conflict (only .rs exists) still prevents generation
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();
        let target_dir = project_root.join("src/ui");
        fs::create_dir_all(&target_dir).unwrap();

        // Create only .rs file (no .dampen file)
        let existing_rs = target_dir.join("profile.rs");
        fs::write(&existing_rs, "existing rust content").unwrap();

        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("profile").unwrap();
        let result = generate_window_files(&target_path, &window_name, false);

        // Should still reject because .rs exists
        assert!(result.is_err());
        match result {
            Err(GenerationError::FileExists {
                window_name: name,
                path,
            }) => {
                assert_eq!(name, "profile");
                assert_eq!(path, existing_rs);
            }
            _ => panic!("Expected FileExists error for partial conflict"),
        }

        // Clean up and try with only .dampen file
        fs::remove_file(&existing_rs).unwrap();
        let existing_dampen = target_dir.join("profile.dampen");
        fs::write(&existing_dampen, "existing xml").unwrap();

        let result2 = generate_window_files(&target_path, &window_name, false);

        // Should also reject because .dampen exists
        assert!(result2.is_err());
        match result2 {
            Err(GenerationError::FileExists {
                window_name: name,
                path,
            }) => {
                assert_eq!(name, "profile");
                assert_eq!(path, existing_dampen);
            }
            _ => panic!("Expected FileExists error for .dampen conflict"),
        }
    }

    #[test]
    fn test_success_message_format() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();
        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("dashboard").unwrap();

        let generated = GeneratedFiles {
            rust_file: target_path.absolute.join("dashboard.rs"),
            dampen_file: target_path.absolute.join("dashboard.dampen"),
            window_name: window_name.clone(),
            target_dir: target_path.absolute.clone(),
            updated_mod_file: None,
            view_switching_activated: false,
        };

        let message = generated.success_message();

        assert!(message.contains("Created UI window 'dashboard'"));
        assert!(message.contains("dashboard.rs"));
        assert!(message.contains("dashboard.dampen"));
        assert!(message.contains("pub mod dashboard"));
        assert!(message.contains("Next steps"));
    }

    #[test]
    fn test_success_message_with_integration() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();
        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("settings").unwrap();

        let generated = GeneratedFiles {
            rust_file: target_path.absolute.join("settings.rs"),
            dampen_file: target_path.absolute.join("settings.dampen"),
            window_name: window_name.clone(),
            target_dir: target_path.absolute.clone(),
            updated_mod_file: Some(project_root.join("src/ui/mod.rs")),
            view_switching_activated: false,
        };

        let message = generated.success_message();

        assert!(message.contains("Created UI window 'settings'"));
        assert!(message.contains("settings.rs"));
        assert!(message.contains("settings.dampen"));
        assert!(message.contains("Updated"), "Should mention mod.rs update");
        assert!(message.contains("src/ui/mod.rs"), "Should show mod.rs path");
        assert!(
            !message.contains("pub mod settings"),
            "Should NOT show manual mod instruction"
        );
        assert!(message.contains("Next steps"));
    }

    #[test]
    fn test_generate_files_with_integration() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();

        // Create src/ui directory and empty mod.rs
        let ui_dir = project_root.join("src/ui");
        fs::create_dir_all(&ui_dir).unwrap();
        fs::write(ui_dir.join("mod.rs"), "").unwrap();

        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("settings").unwrap();

        let result = generate_window_files(&target_path, &window_name, true);

        assert!(result.is_ok());
        let generated = result.unwrap();

        // Files should be created
        assert!(generated.rust_file.exists());
        assert!(generated.dampen_file.exists());

        // mod.rs should be updated
        assert!(generated.updated_mod_file.is_some());
        let mod_file = generated.updated_mod_file.unwrap();
        assert_eq!(mod_file, ui_dir.join("mod.rs"));

        // Check mod.rs content
        let mod_content = fs::read_to_string(&mod_file).unwrap();
        assert!(mod_content.contains("pub mod settings;"));
    }

    #[test]
    fn test_generate_files_integration_disabled() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();

        // Create src/ui directory and empty mod.rs
        let ui_dir = project_root.join("src/ui");
        fs::create_dir_all(&ui_dir).unwrap();
        fs::write(ui_dir.join("mod.rs"), "").unwrap();

        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("settings").unwrap();

        let result = generate_window_files(&target_path, &window_name, false);

        assert!(result.is_ok());
        let generated = result.unwrap();

        // Files should be created
        assert!(generated.rust_file.exists());
        assert!(generated.dampen_file.exists());

        // mod.rs should NOT be updated
        assert!(generated.updated_mod_file.is_none());

        // Check mod.rs is still empty
        let mod_content = fs::read_to_string(ui_dir.join("mod.rs")).unwrap();
        assert!(mod_content.is_empty());
    }

    #[test]
    fn test_view_switching_activated_on_second_window() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();

        // Create src/ui directory with mod.rs and first window
        let ui_dir = project_root.join("src/ui");
        fs::create_dir_all(&ui_dir).unwrap();
        fs::write(ui_dir.join("mod.rs"), "pub mod window;\n").unwrap();
        fs::write(ui_dir.join("window.rs"), "// First window").unwrap();

        // Create a minimal main.rs with commented view switching
        let src_dir = project_root.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        let main_content = r#"
enum Message {
    // SwitchToView(CurrentView),
    Handler(HandlerMessage),
}

#[dampen_app(
    ui_dir = "src/ui",
    message_type = "Message",
    // switch_view_variant = "SwitchToView",
)]
struct App;
"#;
        fs::write(src_dir.join("main.rs"), main_content).unwrap();

        // Add second window
        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("settings").unwrap();

        let result = generate_window_files(&target_path, &window_name, true);

        assert!(result.is_ok());
        let generated = result.unwrap();

        // View switching should be activated
        assert!(generated.view_switching_activated);

        // Check main.rs was modified
        let main_content = fs::read_to_string(src_dir.join("main.rs")).unwrap();
        assert!(main_content.contains("SwitchToView(CurrentView),"));
        assert!(!main_content.contains("// SwitchToView(CurrentView),"));
        assert!(main_content.contains(r#"switch_view_variant = "SwitchToView","#));
        assert!(!main_content.contains(r#"// switch_view_variant"#));
    }

    #[test]
    fn test_view_switching_not_activated_on_first_window() {
        let temp = TempDir::new().unwrap();
        let project_root = temp.path();

        // Create src/ui directory with only mod.rs (no existing windows)
        let ui_dir = project_root.join("src/ui");
        fs::create_dir_all(&ui_dir).unwrap();
        fs::write(ui_dir.join("mod.rs"), "").unwrap();

        // Add first window
        let target_path = TargetPath::resolve(project_root, Some("src/ui")).unwrap();
        let window_name = WindowName::new("window").unwrap();

        let result = generate_window_files(&target_path, &window_name, true);

        assert!(result.is_ok());
        let generated = result.unwrap();

        // View switching should NOT be activated (only one window)
        assert!(!generated.view_switching_activated);
    }
}