raz-core 0.2.4

Universal command generator for Rust projects - Core library with stateless file analysis and cursor-aware execution
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
//! Comprehensive Framework CLI validation system
//!
//! Provides validation for Dioxus (`dx serve`) and Leptos (`cargo-leptos watch`)
//! options with proper type checking, enum validation, and intelligent suggestions for typos.

use regex::Regex;
use std::collections::HashMap;

/// Dioxus option value types
#[derive(Debug, Clone, PartialEq)]
pub enum DioxusOptionType {
    /// Flag option (no value required)
    Flag,
    /// Boolean option with explicit true/false values
    Bool { default: Option<bool> },
    /// String value
    String { default: Option<String> },
    /// Port number (1-65535)
    Port { default: Option<u16> },
    /// Network address
    Address { default: Option<String> },
    /// Platform enum
    Platform { default: Option<String> },
    /// Architecture enum
    Architecture,
    /// Profile name
    Profile { default: Option<String> },
    /// Features list (space-separated)
    Features,
    /// Multiple string values
    Multiple,
    /// Interval in seconds
    Interval { default: Option<u64> },
}

/// Dioxus option metadata
#[derive(Debug, Clone)]
pub struct DioxusOptionInfo {
    pub long: &'static str,
    pub short: Option<&'static str>,
    pub option_type: DioxusOptionType,
    pub description: &'static str,
    pub possible_values: Option<Vec<&'static str>>,
}

/// Validation result
#[derive(Debug, Clone)]
pub enum ValidationResult {
    Valid,
    InvalidOption {
        suggestion: Option<String>,
    },
    InvalidValue {
        expected: String,
        got: String,
        suggestions: Vec<String>,
    },
    MissingValue {
        expected: String,
    },
}

/// Framework CLI validator for Dioxus and Leptos
pub struct FrameworkValidator {
    dioxus_serve_options: HashMap<&'static str, DioxusOptionInfo>,
    leptos_watch_options: HashMap<&'static str, DioxusOptionInfo>,
    platform_values: Vec<&'static str>,
    arch_values: Vec<&'static str>,
    bool_values: Vec<&'static str>,
}

/// Legacy alias for backward compatibility
pub type DioxusValidator = FrameworkValidator;

impl FrameworkValidator {
    pub fn new() -> Self {
        let mut dioxus_serve_options = HashMap::new();
        let mut leptos_watch_options = HashMap::new();

        // ===== DIOXUS DX SERVE OPTIONS =====

        // Port option
        dioxus_serve_options.insert(
            "--port",
            DioxusOptionInfo {
                long: "--port",
                short: None,
                option_type: DioxusOptionType::Port { default: None },
                description: "The port the server will run on",
                possible_values: None,
            },
        );

        // Address option
        dioxus_serve_options.insert(
            "--addr",
            DioxusOptionInfo {
                long: "--addr",
                short: None,
                option_type: DioxusOptionType::Address {
                    default: Some("localhost".to_string()),
                },
                description: "The address the server will run on",
                possible_values: None,
            },
        );

        // Open option - boolean with default true
        dioxus_serve_options.insert(
            "--open",
            DioxusOptionInfo {
                long: "--open",
                short: None,
                option_type: DioxusOptionType::Bool {
                    default: Some(true),
                },
                description: "Open the app in the default browser",
                possible_values: Some(vec!["true", "false"]),
            },
        );

        // Hot reload option - boolean with default true
        dioxus_serve_options.insert(
            "--hot-reload",
            DioxusOptionInfo {
                long: "--hot-reload",
                short: None,
                option_type: DioxusOptionType::Bool {
                    default: Some(true),
                },
                description: "Enable full hot reloading for the app",
                possible_values: Some(vec!["true", "false"]),
            },
        );

        // Always on top option - boolean with default true
        dioxus_serve_options.insert(
            "--always-on-top",
            DioxusOptionInfo {
                long: "--always-on-top",
                short: None,
                option_type: DioxusOptionType::Bool {
                    default: Some(true),
                },
                description: "Configure always-on-top for desktop apps",
                possible_values: Some(vec!["true", "false"]),
            },
        );

        // Cross origin policy - flag
        dioxus_serve_options.insert(
            "--cross-origin-policy",
            DioxusOptionInfo {
                long: "--cross-origin-policy",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Set cross-origin-policy to same-origin",
                possible_values: None,
            },
        );

        // Args option
        dioxus_serve_options.insert(
            "--args",
            DioxusOptionInfo {
                long: "--args",
                short: None,
                option_type: DioxusOptionType::String { default: None },
                description: "Additional arguments to pass to the executable",
                possible_values: None,
            },
        );

        // WSL file poll interval
        dioxus_serve_options.insert(
            "--wsl-file-poll-interval",
            DioxusOptionInfo {
                long: "--wsl-file-poll-interval",
                short: None,
                option_type: DioxusOptionType::Interval { default: None },
                description:
                    "Sets the interval in seconds that the CLI will poll for file changes on WSL",
                possible_values: None,
            },
        );

        // Interactive option - boolean with optional value
        dioxus_serve_options.insert(
            "--interactive",
            DioxusOptionInfo {
                long: "--interactive",
                short: Some("-i"),
                option_type: DioxusOptionType::Bool {
                    default: Some(false),
                },
                description: "Run the server in interactive mode",
                possible_values: Some(vec!["true", "false"]),
            },
        );

        // Release flag
        dioxus_serve_options.insert(
            "--release",
            DioxusOptionInfo {
                long: "--release",
                short: Some("-r"),
                option_type: DioxusOptionType::Flag,
                description: "Build in release mode",
                possible_values: None,
            },
        );

        // Force sequential flag
        dioxus_serve_options.insert(
            "--force-sequential",
            DioxusOptionInfo {
                long: "--force-sequential",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Force fullstack builds to run server first, then client",
                possible_values: None,
            },
        );

        // Profile option
        dioxus_serve_options.insert(
            "--profile",
            DioxusOptionInfo {
                long: "--profile",
                short: None,
                option_type: DioxusOptionType::Profile { default: None },
                description: "Build the app with custom a profile",
                possible_values: None,
            },
        );

        // Verbose flag
        dioxus_serve_options.insert(
            "--verbose",
            DioxusOptionInfo {
                long: "--verbose",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Use verbose output",
                possible_values: None,
            },
        );

        // Server profile option
        dioxus_serve_options.insert(
            "--server-profile",
            DioxusOptionInfo {
                long: "--server-profile",
                short: None,
                option_type: DioxusOptionType::Profile {
                    default: Some("server-dev".to_string()),
                },
                description: "Build with custom profile for the fullstack server",
                possible_values: None,
            },
        );

        // Trace flag
        dioxus_serve_options.insert(
            "--trace",
            DioxusOptionInfo {
                long: "--trace",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Use trace output",
                possible_values: None,
            },
        );

        // JSON output flag
        dioxus_serve_options.insert(
            "--json-output",
            DioxusOptionInfo {
                long: "--json-output",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Output logs in JSON format",
                possible_values: None,
            },
        );

        // Platform option - enum with specific values
        dioxus_serve_options.insert(
            "--platform",
            DioxusOptionInfo {
                long: "--platform",
                short: None,
                option_type: DioxusOptionType::Platform {
                    default: Some("default_platform".to_string()),
                },
                description: "Build platform: support Web & Desktop",
                possible_values: Some(vec![
                    "web", "macos", "windows", "linux", "ios", "android", "server", "liveview",
                ]),
            },
        );

        // Fullstack flag
        dioxus_serve_options.insert(
            "--fullstack",
            DioxusOptionInfo {
                long: "--fullstack",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Build the fullstack variant of this app",
                possible_values: None,
            },
        );

        // SSG flag
        dioxus_serve_options.insert(
            "--ssg",
            DioxusOptionInfo {
                long: "--ssg",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Run the ssg config of the app and generate the files",
                possible_values: None,
            },
        );

        // Skip assets flag
        dioxus_serve_options.insert(
            "--skip-assets",
            DioxusOptionInfo {
                long: "--skip-assets",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Skip collecting assets from dependencies",
                possible_values: None,
            },
        );

        // Inject loading scripts flag
        dioxus_serve_options.insert(
            "--inject-loading-scripts",
            DioxusOptionInfo {
                long: "--inject-loading-scripts",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Inject scripts to load the wasm and js files",
                possible_values: None,
            },
        );

        // Debug symbols flag
        dioxus_serve_options.insert(
            "--debug-symbols",
            DioxusOptionInfo {
                long: "--debug-symbols",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Generate debug symbols for the wasm binary",
                possible_values: None,
            },
        );

        // Nightly flag
        dioxus_serve_options.insert(
            "--nightly",
            DioxusOptionInfo {
                long: "--nightly",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Build for nightly",
                possible_values: None,
            },
        );

        // Example option
        dioxus_serve_options.insert(
            "--example",
            DioxusOptionInfo {
                long: "--example",
                short: None,
                option_type: DioxusOptionType::String {
                    default: Some("".to_string()),
                },
                description: "Build a example",
                possible_values: None,
            },
        );

        // Bin option
        dioxus_serve_options.insert(
            "--bin",
            DioxusOptionInfo {
                long: "--bin",
                short: None,
                option_type: DioxusOptionType::String {
                    default: Some("".to_string()),
                },
                description: "Build a binary",
                possible_values: None,
            },
        );

        // Package option
        dioxus_serve_options.insert(
            "--package",
            DioxusOptionInfo {
                long: "--package",
                short: Some("-p"),
                option_type: DioxusOptionType::String { default: None },
                description: "The package to build",
                possible_values: None,
            },
        );

        // Features option
        dioxus_serve_options.insert(
            "--features",
            DioxusOptionInfo {
                long: "--features",
                short: None,
                option_type: DioxusOptionType::Features,
                description: "Space separated list of features to activate",
                possible_values: None,
            },
        );

        // Client features option
        dioxus_serve_options.insert(
            "--client-features",
            DioxusOptionInfo {
                long: "--client-features",
                short: None,
                option_type: DioxusOptionType::String {
                    default: Some("web".to_string()),
                },
                description: "The feature to use for the client in a fullstack app",
                possible_values: None,
            },
        );

        // Server features option
        dioxus_serve_options.insert(
            "--server-features",
            DioxusOptionInfo {
                long: "--server-features",
                short: None,
                option_type: DioxusOptionType::String {
                    default: Some("server".to_string()),
                },
                description: "The feature to use for the server in a fullstack app",
                possible_values: None,
            },
        );

        // No default features flag
        dioxus_serve_options.insert(
            "--no-default-features",
            DioxusOptionInfo {
                long: "--no-default-features",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Don't include the default features in the build",
                possible_values: None,
            },
        );

        // Architecture option
        dioxus_serve_options.insert(
            "--arch",
            DioxusOptionInfo {
                long: "--arch",
                short: None,
                option_type: DioxusOptionType::Architecture,
                description: "The architecture to build for",
                possible_values: Some(vec!["arm", "arm64", "x86", "x64"]),
            },
        );

        // Device option - boolean
        dioxus_serve_options.insert(
            "--device",
            DioxusOptionInfo {
                long: "--device",
                short: None,
                option_type: DioxusOptionType::Bool { default: None },
                description: "Are we building for a device or just the simulator",
                possible_values: Some(vec!["true", "false"]),
            },
        );

        // Target option
        dioxus_serve_options.insert(
            "--target",
            DioxusOptionInfo {
                long: "--target",
                short: None,
                option_type: DioxusOptionType::String { default: None },
                description: "Rustc platform triple",
                possible_values: None,
            },
        );

        // ===== LEPTOS CARGO-LEPTOS WATCH OPTIONS =====

        // Release flag
        leptos_watch_options.insert(
            "--release",
            DioxusOptionInfo {
                long: "--release",
                short: Some("-r"),
                option_type: DioxusOptionType::Flag,
                description: "Build artifacts in release mode, with optimizations",
                possible_values: None,
            },
        );

        // Precompress flag
        leptos_watch_options.insert("--precompress", DioxusOptionInfo {
            long: "--precompress",
            short: Some("-P"),
            option_type: DioxusOptionType::Flag,
            description: "Precompress static assets with gzip and brotli. Applies to release builds only",
            possible_values: None,
        });

        // Hot reload flag
        leptos_watch_options.insert(
            "--hot-reload",
            DioxusOptionInfo {
                long: "--hot-reload",
                short: None,
                option_type: DioxusOptionType::Flag,
                description: "Turn on partial hot-reloading. Requires rust nightly [beta]",
                possible_values: None,
            },
        );

        // Project option
        leptos_watch_options.insert(
            "--project",
            DioxusOptionInfo {
                long: "--project",
                short: Some("-p"),
                option_type: DioxusOptionType::String { default: None },
                description: "Which project to use, from a list of projects defined in a workspace",
                possible_values: None,
            },
        );

        // Features option
        leptos_watch_options.insert(
            "--features",
            DioxusOptionInfo {
                long: "--features",
                short: None,
                option_type: DioxusOptionType::Features,
                description: "The features to use when compiling all targets",
                possible_values: None,
            },
        );

        // Lib features option
        leptos_watch_options.insert(
            "--lib-features",
            DioxusOptionInfo {
                long: "--lib-features",
                short: None,
                option_type: DioxusOptionType::Features,
                description: "The features to use when compiling the lib target",
                possible_values: None,
            },
        );

        // Lib cargo args option
        leptos_watch_options.insert(
            "--lib-cargo-args",
            DioxusOptionInfo {
                long: "--lib-cargo-args",
                short: None,
                option_type: DioxusOptionType::String { default: None },
                description: "The cargo flags to pass to cargo when compiling the lib target",
                possible_values: None,
            },
        );

        // Bin features option
        leptos_watch_options.insert(
            "--bin-features",
            DioxusOptionInfo {
                long: "--bin-features",
                short: None,
                option_type: DioxusOptionType::Features,
                description: "The features to use when compiling the bin target",
                possible_values: None,
            },
        );

        // Bin cargo args option
        leptos_watch_options.insert(
            "--bin-cargo-args",
            DioxusOptionInfo {
                long: "--bin-cargo-args",
                short: None,
                option_type: DioxusOptionType::String { default: None },
                description: "The cargo flags to pass to cargo when compiling the bin target",
                possible_values: None,
            },
        );

        // WASM debug flag
        leptos_watch_options.insert("--wasm-debug", DioxusOptionInfo {
            long: "--wasm-debug",
            short: None,
            option_type: DioxusOptionType::Flag,
            description: "Include debug information in Wasm output. Includes source maps and DWARF debug info",
            possible_values: None,
        });

        // Verbosity option (multiple v's allowed)
        leptos_watch_options.insert(
            "-v",
            DioxusOptionInfo {
                long: "-v",
                short: None,
                option_type: DioxusOptionType::Multiple,
                description:
                    "Verbosity (none: info, errors & warnings, -v: verbose, -vv: very verbose)",
                possible_values: None,
            },
        );

        // JS minify option
        leptos_watch_options.insert(
            "--js-minify",
            DioxusOptionInfo {
                long: "--js-minify",
                short: None,
                option_type: DioxusOptionType::Bool {
                    default: Some(true),
                },
                description: "Minify javascript assets with swc. Applies to release builds only",
                possible_values: Some(vec!["true", "false"]),
            },
        );

        Self {
            dioxus_serve_options,
            leptos_watch_options,
            platform_values: vec![
                "web", "macos", "windows", "linux", "ios", "android", "server", "liveview",
            ],
            arch_values: vec!["arm", "arm64", "x86", "x64"],
            bool_values: vec!["true", "false"],
        }
    }

    /// Validate a framework option and its value
    pub fn validate_option(
        &self,
        framework: &str,
        option: &str,
        value: Option<&str>,
    ) -> ValidationResult {
        // Check if option exists in the specified framework
        let option_info = match framework {
            "dioxus" => self.dioxus_serve_options.get(option),
            "leptos" => self.leptos_watch_options.get(option),
            _ => None,
        };

        let option_info = match option_info {
            Some(info) => info,
            None => {
                // Try to find a close match for typo suggestion
                let suggestion = self.find_closest_option(framework, option);
                return ValidationResult::InvalidOption { suggestion };
            }
        };

        // Validate based on option type
        match &option_info.option_type {
            DioxusOptionType::Flag => {
                if let Some(val) = value {
                    ValidationResult::InvalidValue {
                        expected: "no value (this is a flag)".to_string(),
                        got: val.to_string(),
                        suggestions: vec![
                            "Remove the value - this option doesn't take a value".to_string(),
                        ],
                    }
                } else {
                    ValidationResult::Valid
                }
            }

            DioxusOptionType::Bool { default: _ } => {
                if let Some(val) = value {
                    if self.bool_values.contains(&val) {
                        ValidationResult::Valid
                    } else {
                        ValidationResult::InvalidValue {
                            expected: "true or false".to_string(),
                            got: val.to_string(),
                            suggestions: self.suggest_bool_values(val),
                        }
                    }
                } else {
                    // For boolean options, missing value might be valid (uses default)
                    ValidationResult::Valid
                }
            }

            DioxusOptionType::Port { default: _ } => match value {
                Some(val) => match val.parse::<u16>() {
                    Ok(port) => {
                        if port > 0 {
                            ValidationResult::Valid
                        } else {
                            ValidationResult::InvalidValue {
                                expected: "port number (1-65535)".to_string(),
                                got: val.to_string(),
                                suggestions: vec![
                                    "Try a port number like 3000, 8080, or 8000".to_string(),
                                ],
                            }
                        }
                    }
                    Err(_) => ValidationResult::InvalidValue {
                        expected: "port number (1-65535)".to_string(),
                        got: val.to_string(),
                        suggestions: vec![
                            "Port must be a number, e.g., 3000, 8080, 8000".to_string(),
                        ],
                    },
                },
                None => ValidationResult::MissingValue {
                    expected: "port number (e.g., 3000, 8080)".to_string(),
                },
            },

            DioxusOptionType::Address { default: _ } => match value {
                Some(val) => {
                    if self.is_valid_address(val) {
                        ValidationResult::Valid
                    } else {
                        ValidationResult::InvalidValue {
                            expected: "valid IP address or hostname".to_string(),
                            got: val.to_string(),
                            suggestions: vec![
                                "localhost".to_string(),
                                "127.0.0.1".to_string(),
                                "0.0.0.0".to_string(),
                            ],
                        }
                    }
                }
                None => ValidationResult::MissingValue {
                    expected: "IP address or hostname (e.g., localhost, 127.0.0.1)".to_string(),
                },
            },

            DioxusOptionType::Platform { default: _ } => match value {
                Some(val) => {
                    if self.platform_values.contains(&val) {
                        ValidationResult::Valid
                    } else {
                        ValidationResult::InvalidValue {
                            expected: format!("one of: {}", self.platform_values.join(", ")),
                            got: val.to_string(),
                            suggestions: self.suggest_platform_values(val),
                        }
                    }
                }
                None => ValidationResult::MissingValue {
                    expected: format!("platform ({})", self.platform_values.join(", ")),
                },
            },

            DioxusOptionType::Architecture => match value {
                Some(val) => {
                    if self.arch_values.contains(&val) {
                        ValidationResult::Valid
                    } else {
                        ValidationResult::InvalidValue {
                            expected: format!("one of: {}", self.arch_values.join(", ")),
                            got: val.to_string(),
                            suggestions: self.suggest_arch_values(val),
                        }
                    }
                }
                None => ValidationResult::MissingValue {
                    expected: format!("architecture ({})", self.arch_values.join(", ")),
                },
            },

            DioxusOptionType::Interval { default: _ } => match value {
                Some(val) => match val.parse::<u64>() {
                    Ok(_) => ValidationResult::Valid,
                    Err(_) => ValidationResult::InvalidValue {
                        expected: "number of seconds".to_string(),
                        got: val.to_string(),
                        suggestions: vec!["Try a number like 1, 5, or 30".to_string()],
                    },
                },
                None => ValidationResult::MissingValue {
                    expected: "interval in seconds (e.g., 1, 5, 30)".to_string(),
                },
            },

            DioxusOptionType::String { default: _ }
            | DioxusOptionType::Profile { default: _ }
            | DioxusOptionType::Features
            | DioxusOptionType::Multiple => match value {
                Some(_) => ValidationResult::Valid,
                None => ValidationResult::MissingValue {
                    expected: "string value".to_string(),
                },
            },
        }
    }

    /// Find the closest matching option for typo suggestions
    fn find_closest_option(&self, framework: &str, input: &str) -> Option<String> {
        let mut best_match = None;
        let mut best_distance = usize::MAX;

        let options = match framework {
            "dioxus" => self.dioxus_serve_options.keys(),
            "leptos" => self.leptos_watch_options.keys(),
            _ => return None,
        };

        for option in options {
            let distance = levenshtein_distance(input, option);
            if distance < best_distance && distance <= 3 {
                best_distance = distance;
                best_match = Some(option.to_string());
            }
        }

        best_match
    }

    /// Suggest boolean values based on input
    fn suggest_bool_values(&self, input: &str) -> Vec<String> {
        let lower = input.to_lowercase();
        if lower.starts_with('t') || lower.starts_with('y') || lower == "1" {
            vec!["true".to_string()]
        } else if lower.starts_with('f') || lower.starts_with('n') || lower == "0" {
            vec!["false".to_string()]
        } else {
            vec!["true".to_string(), "false".to_string()]
        }
    }

    /// Suggest platform values based on input
    fn suggest_platform_values(&self, input: &str) -> Vec<String> {
        let mut suggestions = Vec::new();
        let lower = input.to_lowercase();

        for platform in &self.platform_values {
            if platform.starts_with(&lower) || levenshtein_distance(&lower, platform) <= 2 {
                suggestions.push(platform.to_string());
            }
        }

        if suggestions.is_empty() {
            // Provide common suggestions
            suggestions.extend_from_slice(&["web".to_string(), "desktop".to_string()]);
        }

        suggestions
    }

    /// Suggest architecture values based on input
    fn suggest_arch_values(&self, input: &str) -> Vec<String> {
        let mut suggestions = Vec::new();
        let lower = input.to_lowercase();

        for arch in &self.arch_values {
            if arch.starts_with(&lower) || levenshtein_distance(&lower, arch) <= 1 {
                suggestions.push(arch.to_string());
            }
        }

        if suggestions.is_empty() {
            suggestions.extend_from_slice(&["x64".to_string(), "arm64".to_string()]);
        }

        suggestions
    }

    /// Basic address validation
    fn is_valid_address(&self, addr: &str) -> bool {
        // Allow localhost, IP addresses, and hostnames
        if addr == "localhost" || addr == "0.0.0.0" {
            return true;
        }

        // Simple IP address validation
        let ip_regex = Regex::new(r"^(\d{1,3}\.){3}\d{1,3}$").unwrap();
        if ip_regex.is_match(addr) {
            // Check each octet is valid (0-255)
            return addr.split('.').all(|octet| octet.parse::<u8>().is_ok());
        }

        // Allow valid hostnames (basic check)
        let hostname_regex = Regex::new(r"^[a-zA-Z0-9.-]+$").unwrap();
        hostname_regex.is_match(addr)
    }

    /// Check if an option is valid for the specified framework
    pub fn is_valid_option(&self, framework: &str, option: &str) -> bool {
        match framework {
            "dioxus" => self.dioxus_serve_options.contains_key(option),
            "leptos" => self.leptos_watch_options.contains_key(option),
            _ => false,
        }
    }

    /// Check if an option is valid for dx serve (legacy method)
    pub fn is_valid_serve_option(&self, option: &str) -> bool {
        self.is_valid_option("dioxus", option)
    }

    /// Get option information for specified framework
    pub fn get_option_info(&self, framework: &str, option: &str) -> Option<&DioxusOptionInfo> {
        match framework {
            "dioxus" => self.dioxus_serve_options.get(option),
            "leptos" => self.leptos_watch_options.get(option),
            _ => None,
        }
    }

    /// Get all valid options for specified framework
    pub fn get_all_options(&self, framework: &str) -> Vec<&str> {
        match framework {
            "dioxus" => self.dioxus_serve_options.keys().copied().collect(),
            "leptos" => self.leptos_watch_options.keys().copied().collect(),
            _ => vec![],
        }
    }

    /// Convenience method for Dioxus validation
    pub fn validate_dioxus_option(&self, option: &str, value: Option<&str>) -> ValidationResult {
        self.validate_option("dioxus", option, value)
    }

    /// Convenience method for Leptos validation
    pub fn validate_leptos_option(&self, option: &str, value: Option<&str>) -> ValidationResult {
        self.validate_option("leptos", option, value)
    }
}

impl Default for DioxusValidator {
    fn default() -> Self {
        Self::new()
    }
}

/// Calculate Levenshtein distance for typo suggestions
fn levenshtein_distance(s1: &str, s2: &str) -> usize {
    let len1 = s1.chars().count();
    let len2 = s2.chars().count();
    let mut matrix = vec![vec![0; len2 + 1]; len1 + 1];

    for (i, row) in matrix.iter_mut().enumerate().take(len1 + 1) {
        row[0] = i;
    }
    for j in 0..=len2 {
        matrix[0][j] = j;
    }

    let s1_chars: Vec<char> = s1.chars().collect();
    let s2_chars: Vec<char> = s2.chars().collect();

    for i in 1..=len1 {
        for j in 1..=len2 {
            let cost = if s1_chars[i - 1] == s2_chars[j - 1] {
                0
            } else {
                1
            };
            matrix[i][j] = (matrix[i - 1][j] + 1)
                .min(matrix[i][j - 1] + 1)
                .min(matrix[i - 1][j - 1] + cost);
        }
    }

    matrix[len1][len2]
}

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

    #[test]
    fn test_valid_dioxus_options() {
        let validator = FrameworkValidator::new();

        // Test flag options
        assert!(matches!(
            validator.validate_dioxus_option("--release", None),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_dioxus_option("--fullstack", None),
            ValidationResult::Valid
        ));

        // Test boolean options
        assert!(matches!(
            validator.validate_dioxus_option("--device", Some("true")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_dioxus_option("--device", Some("false")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_dioxus_option("--hot-reload", Some("true")),
            ValidationResult::Valid
        ));

        // Test port option
        assert!(matches!(
            validator.validate_dioxus_option("--port", Some("3000")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_dioxus_option("--port", Some("8080")),
            ValidationResult::Valid
        ));

        // Test platform option
        assert!(matches!(
            validator.validate_dioxus_option("--platform", Some("web")),
            ValidationResult::Valid
        ));

        // Test architecture option
        assert!(matches!(
            validator.validate_dioxus_option("--arch", Some("x64")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_dioxus_option("--arch", Some("arm64")),
            ValidationResult::Valid
        ));
    }

    #[test]
    fn test_valid_leptos_options() {
        let validator = FrameworkValidator::new();

        // Test flag options
        assert!(matches!(
            validator.validate_leptos_option("--release", None),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_leptos_option("--precompress", None),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_leptos_option("--hot-reload", None),
            ValidationResult::Valid
        ));

        // Test boolean options
        assert!(matches!(
            validator.validate_leptos_option("--js-minify", Some("true")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_leptos_option("--js-minify", Some("false")),
            ValidationResult::Valid
        ));

        // Test string options
        assert!(matches!(
            validator.validate_leptos_option("--project", Some("my-project")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_leptos_option("--features", Some("ssr,hydrate")),
            ValidationResult::Valid
        ));
    }

    #[test]
    fn test_invalid_device_value() {
        let validator = FrameworkValidator::new();

        match validator.validate_dioxus_option("--device", Some("example")) {
            ValidationResult::InvalidValue {
                expected,
                got,
                suggestions,
            } => {
                assert_eq!(expected, "true or false");
                assert_eq!(got, "example");
                assert!(!suggestions.is_empty());
            }
            _ => panic!("Expected InvalidValue result"),
        }
    }

    #[test]
    fn test_invalid_platform_value() {
        let validator = FrameworkValidator::new();

        match validator.validate_dioxus_option("--platform", Some("desktop")) {
            ValidationResult::InvalidValue {
                expected,
                got,
                suggestions,
            } => {
                assert!(expected.contains("web"));
                assert_eq!(got, "desktop");
                assert!(
                    suggestions
                        .iter()
                        .any(|s| s.contains("web") || s.contains("macos"))
                );
            }
            _ => panic!("Expected InvalidValue result"),
        }
    }

    #[test]
    fn test_typo_suggestions() {
        let validator = FrameworkValidator::new();

        match validator.validate_dioxus_option("--hot-reloads", None) {
            ValidationResult::InvalidOption { suggestion } => {
                assert_eq!(suggestion, Some("--hot-reload".to_string()));
            }
            _ => panic!("Expected InvalidOption result"),
        }
    }

    #[test]
    fn test_leptos_typo_suggestions() {
        let validator = FrameworkValidator::new();

        match validator.validate_leptos_option("--precompres", None) {
            ValidationResult::InvalidOption { suggestion } => {
                assert_eq!(suggestion, Some("--precompress".to_string()));
            }
            _ => panic!("Expected InvalidOption result"),
        }
    }

    #[test]
    fn test_port_validation() {
        let validator = FrameworkValidator::new();

        // Valid ports
        assert!(matches!(
            validator.validate_dioxus_option("--port", Some("3000")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_dioxus_option("--port", Some("8080")),
            ValidationResult::Valid
        ));

        // Invalid ports
        match validator.validate_dioxus_option("--port", Some("0")) {
            ValidationResult::InvalidValue { .. } => {}
            _ => panic!("Expected InvalidValue for port 0"),
        }

        match validator.validate_dioxus_option("--port", Some("abc")) {
            ValidationResult::InvalidValue { .. } => {}
            _ => panic!("Expected InvalidValue for non-numeric port"),
        }
    }

    #[test]
    fn test_address_validation() {
        let validator = FrameworkValidator::new();

        // Valid addresses
        assert!(matches!(
            validator.validate_dioxus_option("--addr", Some("localhost")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_dioxus_option("--addr", Some("127.0.0.1")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_dioxus_option("--addr", Some("0.0.0.0")),
            ValidationResult::Valid
        ));

        // Invalid addresses would be caught by the validation logic
    }

    #[test]
    fn test_leptos_js_minify() {
        let validator = FrameworkValidator::new();

        // Valid boolean values
        assert!(matches!(
            validator.validate_leptos_option("--js-minify", Some("true")),
            ValidationResult::Valid
        ));
        assert!(matches!(
            validator.validate_leptos_option("--js-minify", Some("false")),
            ValidationResult::Valid
        ));

        // Invalid boolean value
        match validator.validate_leptos_option("--js-minify", Some("maybe")) {
            ValidationResult::InvalidValue {
                expected,
                got,
                suggestions,
            } => {
                assert_eq!(expected, "true or false");
                assert_eq!(got, "maybe");
                assert!(!suggestions.is_empty());
            }
            _ => panic!("Expected InvalidValue result"),
        }
    }
}