uxn-tal-defined 0.4.1

Shared protocol definitions for uxntal://
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
use std::{borrow::Cow, fmt};
// This module is intended to be extended with per-emulator mapping modules (see emu_cuxn.rs, emu_uxn.rs, emu_buxn.rs)
use std::collections::HashMap;

use crate::{emu_buxn, emu_cuxn, emu_uxn, percent_decode_or_original};

impl ProtocolQueryVarVar {
    pub fn as_str(&self) -> Option<&str> {
        match self {
            ProtocolQueryVarVar::String(s) => Some(s),
            ProtocolQueryVarVar::Enum(e) => Some(e),
            ProtocolQueryVarVar::Invalid(s) => Some(s),
            _ => None,
        }
    }
    pub fn as_i64(&self) -> Option<i64> {
        match self {
            ProtocolQueryVarVar::Int(i) => Some(*i),
            ProtocolQueryVarVar::String(s) => s.parse().ok(),
            _ => None,
        }
    }
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            ProtocolQueryVarVar::Float(f) => Some(*f),
            ProtocolQueryVarVar::String(s) => s.parse().ok(),
            _ => None,
        }
    }
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            ProtocolQueryVarVar::Bool(b) => Some(*b),
            ProtocolQueryVarVar::String(s) => match s.to_ascii_lowercase().as_str() {
                "1" | "true" | "yes" | "on" => Some(true),
                "0" | "false" | "no" | "off" => Some(false),
                _ => None,
            },
            _ => None,
        }
    }
}

impl fmt::Display for ProtocolQueryVarVar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ProtocolQueryVarVar::Bool(b) => write!(f, "{}", b),
            ProtocolQueryVarVar::String(s) => write!(f, "{}", s),
            ProtocolQueryVarVar::Enum(e) => write!(f, "{}", e),
            ProtocolQueryVarVar::Int(i) => write!(f, "{}", i),
            ProtocolQueryVarVar::Float(fl) => write!(f, "{}", fl),
            ProtocolQueryVarVar::Invalid(s) => write!(f, "Invalid({})", s),
        }
    }
}

impl ProtocolVarVar {
    pub fn as_str(&self) -> Option<&str> {
        match self {
            ProtocolVarVar::String(s) => Some(s),
            _ => None,
        }
    }
    pub fn as_i64(&self) -> Option<i64> {
        match self {
            ProtocolVarVar::Int(i) => Some(*i),
            ProtocolVarVar::String(s) => s.parse().ok(),
            _ => None,
        }
    }
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            ProtocolVarVar::Float(f) => Some(*f),
            ProtocolVarVar::String(s) => s.parse().ok(),
            _ => None,
        }
    }
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            ProtocolVarVar::Bool(b) => Some(*b),
            ProtocolVarVar::String(s) => match s.to_ascii_lowercase().as_str() {
                "1" | "true" | "yes" | "on" => Some(true),
                "0" | "false" | "no" | "off" => Some(false),
                _ => None,
            },
            _ => None,
        }
    }
}

impl fmt::Display for ProtocolVarVar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ProtocolVarVar::Bool(b) => write!(f, "{}", b),
            ProtocolVarVar::String(s) => write!(f, "{}", s),
            ProtocolVarVar::Enum(e) => write!(f, "{}", e),
            ProtocolVarVar::Int(i) => write!(f, "{}", i),
            ProtocolVarVar::Float(fl) => write!(f, "{}", fl),
            ProtocolVarVar::Invalid(s) => write!(f, "Invalid({})", s),
        }
    }
}

// Shared protocol variable types and statics for both lib.rs and build.rs
// use std::borrow::Cow; // already imported above if needed

#[derive(Debug, Clone, PartialEq)]
pub enum ProtocolQueryVarVar {
    Bool(bool),
    String(String),
    Enum(&'static str),
    Int(i64),
    Float(f64),
    Invalid(String),
}

#[derive(Debug, Clone, PartialEq)]
pub enum ProtocolVarVar {
    Bool(bool),
    String(String),
    Enum(&'static str),
    Int(i64),
    Float(f64),
    Invalid(String),
}

#[derive(Debug, Clone)]
pub enum ProtocolVarType {
    Bool,
    String,
    Enum(&'static [&'static str]),
    Int,
    Float,
}

#[derive(Debug, Clone)]
pub struct ProtocolVar {
    pub name: &'static str,
    pub description: &'static str,
    pub example: &'static str,
    pub var_type: ProtocolVarType,
}

pub static PROTOCOL_VARS: &[ProtocolVar] = &[
    ProtocolVar {
        name: "transparent",
        description: "Transparent color for widget/background (hex RGB, e.g. ffffff for white)",
        example: "transparent^ff00ff",
        var_type: ProtocolVarType::String,
    },
    ProtocolVar {
        name: "scale",
        description: "Scale factor for the window (float)",
        example: "scale^2.0",
        var_type: ProtocolVarType::Float,
    },
    ProtocolVar {
        name: "fit",
        description: "Fit mode for ROM display (none, contain, cover, stretch)",
        example: "fit^cover",
        var_type: ProtocolVarType::String,
    },
    ProtocolVar {
        name: "timeout",
        description: "Timeout in seconds before the emulator exits (alias: t)",
        example: "timeout^60",
        var_type: ProtocolVarType::Float,
    },
    ProtocolVar {
        name: "t",
        description: "Timeout in seconds before the emulator exits (alias for timeout)",
        example: "t^60",
        var_type: ProtocolVarType::Float,
    },
    ProtocolVar {
        name: "emu",
        description: "Select emulator backend",
        example: "emu^^buxn",
        var_type: ProtocolVarType::Enum(&["buxn", "cuxn", "uxn"]),
    },
    ProtocolVar {
        name: "widget",
        description: "Enable widget mode (transparent, no decorations, always-on-top)",
        example: "widget",
        var_type: ProtocolVarType::Bool,
    },
    ProtocolVar {
        name: "ontop",
        description: "Control always-on-top (true/false)",
        example: "ontop^false",
        var_type: ProtocolVarType::Bool,
    },
    ProtocolVar {
        name: "debug",
        description: "Enable debug console (Windows only)",
        example: "debug",
        var_type: ProtocolVarType::Bool,
    },
    ProtocolVar {
        name: "efx",
        description: "Effect name or identifier for emulator (string)",
        example: "efx^invert",
        var_type: ProtocolVarType::String,
    },
    ProtocolVar {
        name: "efxmode",
        description: "Effect mode for emulator (string)",
        example: "efxmode^blend",
        var_type: ProtocolVarType::String,
    },
    ProtocolVar {
        name: "orca",
        description: "Orca mode: run the orca ROM with the given .orca file. Automatically set if the URL ends with .orca.",
        example: "orca",
        var_type: ProtocolVarType::Bool,
    },
    ProtocolVar {
        name: "basic",
        description: "Basic mode: run the basic ROM with the given .bas file. Automatically set if the URL ends with .bas.",
        example: "basic",
        var_type: ProtocolVarType::Bool,
    },
    // Added to match bang vars for compatibility matrix
    ProtocolVar {
        name: "x",
        description: "Window X position (pixels or percent or complex)",
        example: "x^100",
        var_type: ProtocolVarType::String,
    },
    ProtocolVar {
        name: "y",
        description: "Window Y position (pixels or percent or complex)",
        example: "y^100",
        var_type: ProtocolVarType::String,
    },
    ProtocolVar {
        name: "w",
        description: "Window width (pixels or percent or complex)",
        example: "w^800",
        var_type: ProtocolVarType::String,
    },
    ProtocolVar {
        name: "h",
        description: "Window height (pixels or percent or complex)",
        example: "h^600",
        var_type: ProtocolVarType::String,
    },
];

#[derive(Debug, Clone)]
pub enum ProtocolQueryType {
    Bool,
    String,
    Enum(&'static [&'static str]),
    Int,
    Float,
}

#[derive(Debug, Clone)]
pub struct ProtocolQueryVar {
    pub name: Cow<'static, str>,
    pub description: Cow<'static, str>,
    pub example: Cow<'static, str>,
    pub var_type: ProtocolQueryType,
    pub value: ProtocolQueryVarVar, // DUMMY for static tables, real value for runtime
}

pub static BANG_VARS: &[ProtocolQueryVar] = &[
    ProtocolQueryVar {
        name: Cow::Borrowed("fit"),
        description: Cow::Borrowed("Fit mode for ROM display (none, contain, cover, stretch)"),
        example: Cow::Borrowed("!fit=cover"),
        var_type: ProtocolQueryType::Enum(&["none", "contain", "cover", "stretch"]),
        value: ProtocolQueryVarVar::String(String::new()),
    },
    ProtocolQueryVar {
        name: Cow::Borrowed("timeout"),
        description: Cow::Borrowed("Timeout in seconds before the emulator exits (alias: t)"),
        example: Cow::Borrowed("!timeout=60"),
        var_type: ProtocolQueryType::Float,
        value: ProtocolQueryVarVar::String(String::new()),
    },
    ProtocolQueryVar {
        name: Cow::Borrowed("t"),
        description: Cow::Borrowed(
            "Timeout in seconds before the emulator exits (alias for timeout)",
        ),
        example: Cow::Borrowed("!t=60"),
        var_type: ProtocolQueryType::Float,
        value: ProtocolQueryVarVar::String(String::new()),
    },
    ProtocolQueryVar {
        name: Cow::Borrowed("x"),
        description: Cow::Borrowed("Window X position (pixels or percent or complex)"),
        example: Cow::Borrowed("!x=100"),
        var_type: ProtocolQueryType::String,
        value: ProtocolQueryVarVar::String(String::new()),
    },
    ProtocolQueryVar {
        name: Cow::Borrowed("y"),
        description: Cow::Borrowed("Window Y position (pixels or percent or complex)"),
        example: Cow::Borrowed("!y=100"),
        var_type: ProtocolQueryType::String,
        value: ProtocolQueryVarVar::String(String::new()),
    },
    ProtocolQueryVar {
        name: Cow::Borrowed("w"),
        description: Cow::Borrowed("Window width (pixels or percent or complex)"),
        example: Cow::Borrowed("!w=800"),
        var_type: ProtocolQueryType::String,
        value: ProtocolQueryVarVar::String(String::new()),
    },
    ProtocolQueryVar {
        name: Cow::Borrowed("h"),
        description: Cow::Borrowed("Window height (pixels or percent or complex)"),
        example: Cow::Borrowed("!h=600"),
        var_type: ProtocolQueryType::String,
        value: ProtocolQueryVarVar::String(String::new()),
    },
    ProtocolQueryVar {
        name: Cow::Borrowed("arg1"),
        description: Cow::Borrowed("First argument to pass to the emulator"),
        example: Cow::Borrowed("!arg1=somefile.input"),
        var_type: ProtocolQueryType::String,
        value: ProtocolQueryVarVar::String(String::new()),
    },
    ProtocolQueryVar {
        name: Cow::Borrowed("stdin"),
        description: Cow::Borrowed(
            "Data to pipe to emulator stdin (for batch-mode input, e.g. BASIC ROMs)",
        ),
        example: Cow::Borrowed("!stdin=RUN%0A"),
        var_type: ProtocolQueryType::String,
        value: ProtocolQueryVarVar::String(String::new()),
    },
];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RepoRef {
    pub provider: String,
    pub owner: String,
    pub repo: String,
    pub branch: String,
    pub path: String,
    pub url_git: String,
}

/// Result of protocol parsing
#[derive(Debug, Clone)]
pub struct ProtocolParseResult {
    pub url_raw: String,
    pub raw: HashMap<String, String>,
    pub proto_vars: HashMap<String, ProtocolVarVar>,
    pub query_vars: HashMap<String, ProtocolQueryVar>,
    pub bang_vars: HashMap<String, ProtocolQueryVar>,
    pub url: String,
    pub protocol: String,     // the uxntal protocol portion (before //url)
    pub query_string: String, // the full query string, including '?' if present
    pub repo_ref: Option<RepoRef>,
}

impl ProtocolParseResult {
    /// Get a validated variable by name
    pub fn get(&self, name: &str) -> Option<&ProtocolVarVar> {
        self.proto_vars.get(name)
    }
}

/// ProtocolParser provides parsing and extraction for uxntal URLs
pub struct ProtocolParser;

impl ProtocolParser {
    /// Parse a uxntal: URL into a ProtocolParseResult with raw and validated maps
    pub fn parse(raw_url: &str) -> ProtocolParseResult {
        let mut raw_map: HashMap<String, String> = HashMap::new();
        let mut proto_vars_map: HashMap<String, ProtocolVarVar> = HashMap::new();
        let mut query_vars_map: HashMap<String, ProtocolQueryVar> = HashMap::new();
        let mut bang_vars_map: HashMap<String, ProtocolQueryVar> = HashMap::new();
        if !raw_url.starts_with("uxntal:") {
            return ProtocolParseResult {
                url_raw: raw_url.to_string(),
                raw: raw_map,
                proto_vars: HashMap::new(),
                query_vars: HashMap::new(),
                bang_vars: HashMap::new(),
                url: String::new(),
                protocol: String::new(),
                query_string: String::new(),
                repo_ref: None,
            };
        }
        let s = raw_url.trim_start_matches("uxntal:");
        // Accept both uxntal:...://url and uxntal:...//url (with or without trailing colon)
        let (kv_part, url_part) = if let Some(idx) = s.find("//") {
            // If the part before // ends with ':', trim it
            let (kv, url) = (&s[..idx], &s[idx..]);
            let kv = if kv.ends_with(':') {
                kv.strip_suffix(':').unwrap()
            } else {
                kv
            };
            (kv, url)
        } else {
            (s, "")
        };
        // The protocol field should be the full valid uxntal:...:// prefix
        let protocol = format!("uxntal:{}://", kv_part);
        // Parse protocol section variables (always proto_vars, even if they start with '!')
        for part in kv_part.split(':') {
            if part.is_empty() {
                continue;
            }
            // If the part contains '=', split on that first (before percent-decoding)
            let (key, value) = if let Some(eq_idx) = part.find('=') {
                let (k, v) = part.split_at(eq_idx);
                let k_decoded = percent_decode_or_original(k);
                let v_decoded = percent_decode_or_original(&v[1..]);
                (k_decoded.to_string(), v_decoded.to_string())
            } else {
                let decoded = percent_decode_or_original(part);
                let mut split_idx = None;
                let mut sep_len = 0;
                if let Some(idx) = decoded.find("^^") {
                    split_idx = Some(idx);
                    sep_len = 2;
                } else if let Some(idx) = decoded.find('^') {
                    split_idx = Some(idx);
                    sep_len = 1;
                }
                if let Some(idx) = split_idx {
                    (
                        decoded[..idx].to_string(),
                        decoded[idx + sep_len..].to_string(),
                    )
                } else {
                    (decoded.to_string(), "true".to_string())
                }
            };
            raw_map.insert(key.to_string(), value.to_string());
            if let Some(var_def) = PROTOCOL_VARS.iter().find(|v| v.name == key) {
                match &var_def.var_type {
                    ProtocolVarType::Bool => {
                        let val = value.trim().to_ascii_lowercase();
                        let parsed = match val.as_str() {
                            "1" | "true" | "yes" | "on" => Some(true),
                            "0" | "false" | "no" | "off" => Some(false),
                            "" => Some(true),
                            _ => None,
                        };
                        if let Some(b) = parsed {
                            proto_vars_map.insert(key.to_string(), ProtocolVarVar::Bool(b));
                        }
                    }
                    ProtocolVarType::Enum(valids) => {
                        let val = value.trim();
                        if let Some(&valid) = valids.iter().find(|&&v| v.eq_ignore_ascii_case(val))
                        {
                            proto_vars_map.insert(key.to_string(), ProtocolVarVar::Enum(valid));
                        }
                    }
                    ProtocolVarType::String => {
                        if !value.is_empty() {
                            proto_vars_map
                                .insert(key.to_string(), ProtocolVarVar::String(value.to_string()));
                        }
                    }
                    ProtocolVarType::Int => {
                        if let Ok(i) = value.trim().parse::<i64>() {
                            proto_vars_map.insert(key.to_string(), ProtocolVarVar::Int(i));
                        }
                    }
                    ProtocolVarType::Float => {
                        let v = value.trim().replace('%', "");
                        if let Ok(f) = v.parse::<f64>() {
                            proto_vars_map.insert(key.to_string(), ProtocolVarVar::Float(f));
                        }
                    }
                }
            } else {
                // Store unknown protocol section vars as string
                proto_vars_map.insert(key.to_string(), ProtocolVarVar::String(value.to_string()));
            }
        }
        // Parse query string for bang vars and query vars BEFORE url normalization
        let (url_for_normalization, query_string) = if let Some(qmark) = url_part.find('?') {
            let raw_query_string = &url_part[qmark..];
            // Normalize &amp; to &
            let normalized_query_string = raw_query_string.replace("&amp;", "&");
            let query = &normalized_query_string[1..]; // skip the '?'
            for pair in query.split('&') {
                if pair.is_empty() {
                    continue;
                }
                let mut it = pair.splitn(2, '=');
                let k = it.next().unwrap_or("");
                if k.is_empty() {
                    continue;
                }
                let v = it.next().unwrap_or("");
                // Percent-decode key and value
                let k_decoded = percent_decode_or_original(k);
                let v_decoded = percent_decode_or_original(v);
                if k_decoded.starts_with('!') {
                    if let Some(key) = k_decoded.strip_prefix('!') {
                        if let Some(var_def) = BANG_VARS.iter().find(|bv| bv.name == key) {
                            let value = match &var_def.var_type {
                                ProtocolQueryType::Bool => {
                                    let val = v_decoded.trim().to_ascii_lowercase();
                                    let parsed = match val.as_str() {
                                        "1" | "true" | "yes" | "on" => Some(true),
                                        "0" | "false" | "no" | "off" => Some(false),
                                        "" => Some(true),
                                        _ => None,
                                    };
                                    if let Some(b) = parsed {
                                        ProtocolQueryVarVar::Bool(b)
                                    } else {
                                        ProtocolQueryVarVar::String(v_decoded.clone())
                                    }
                                }
                                ProtocolQueryType::Enum(valids) => {
                                    let val = v_decoded.trim();
                                    if let Some(&valid) =
                                        valids.iter().find(|&&v| v.eq_ignore_ascii_case(val))
                                    {
                                        ProtocolQueryVarVar::Enum(valid)
                                    } else {
                                        ProtocolQueryVarVar::Invalid(v_decoded.clone())
                                    }
                                }
                                ProtocolQueryType::String => {
                                    ProtocolQueryVarVar::String(v_decoded.clone())
                                }
                                ProtocolQueryType::Int => {
                                    if let Ok(i) = v_decoded.trim().parse::<i64>() {
                                        ProtocolQueryVarVar::Int(i)
                                    } else {
                                        ProtocolQueryVarVar::String(v_decoded.clone())
                                    }
                                }
                                ProtocolQueryType::Float => {
                                    let v = v_decoded.trim().replace('%', "");
                                    if let Ok(f) = v.parse::<f64>() {
                                        ProtocolQueryVarVar::Float(f)
                                    } else {
                                        ProtocolQueryVarVar::String(v_decoded.clone())
                                    }
                                }
                            };
                            bang_vars_map.insert(
                                key.to_string(),
                                ProtocolQueryVar {
                                    name: var_def.name.clone(),
                                    description: var_def.description.clone(),
                                    example: var_def.example.clone(),
                                    var_type: var_def.var_type.clone(),
                                    value,
                                },
                            );
                        } else {
                            bang_vars_map.insert(
                                key.to_string(),
                                ProtocolQueryVar {
                                    name: Cow::Owned(key.to_string()),
                                    description: Cow::Borrowed(""),
                                    example: Cow::Borrowed(""),
                                    var_type: ProtocolQueryType::String,
                                    value: ProtocolQueryVarVar::String(v_decoded.clone()),
                                },
                            );
                        }
                    }
                } else {
                    // Standard query var
                    query_vars_map.insert(
                        k_decoded.to_string(),
                        ProtocolQueryVar {
                            name: Cow::Owned(k_decoded.clone()),
                            description: Cow::Borrowed(""),
                            example: Cow::Borrowed(""),
                            var_type: ProtocolQueryType::String,
                            value: ProtocolQueryVarVar::String(v_decoded.clone()),
                        },
                    );
                }
            }
            (&url_part[..qmark], normalized_query_string)
        } else {
            (url_part, String::new())
        };
        let url = if url_for_normalization.starts_with("//https://")
            || url_for_normalization.starts_with("//http://")
            || url_for_normalization.starts_with("//file://")
        {
            // Remove all but one protocol prefix (e.g., //https://https://... -> https://...)
            let mut s = url_for_normalization.trim_start_matches("//");
            if s.starts_with("https://https://") {
                s = &s[8..];
            } else if s.starts_with("http://http://") {
                s = &s[7..];
            }
            s.to_string()
        } else if let Some(stripped) = url_for_normalization.strip_prefix("//https//") {
            format!("https://{}", stripped)
        } else if let Some(stripped) = url_for_normalization.strip_prefix("//http//") {
            format!("http://{}", stripped)
        } else if let Some(stripped) = url_for_normalization.strip_prefix("//file//") {
            format!("file://{}", stripped)
        } else if let Some(stripped) = url_for_normalization.strip_prefix("//git@") {
            // Handle git@ URLs by removing the // prefix
            format!("git@{}", stripped)
        } else if let Some(stripped) = url_for_normalization.strip_prefix("//open") {
            // Handle uxntal://open?url=ENC and uxntal://open/?url=ENC formats
            // Need to reconstruct the full open URL with query string for extract_target
            let full_open_url = format!("open{}{}", stripped, &query_string);
            if let Some(extracted) = Self::extract_target(&full_open_url) {
                extracted
            } else {
                url_for_normalization.to_string()
            }
        } else {
            url_for_normalization.to_string()
        };

        // If the url ends with .orca, set orca=true in proto_vars_map
        if url.trim().to_ascii_lowercase().ends_with(".orca") {
            proto_vars_map.insert("orca".to_string(), ProtocolVarVar::Bool(true));
        }

        // If the url ends with .bas, set basic=true in proto_vars_map
        if url.trim().to_ascii_lowercase().ends_with(".bas") {
            proto_vars_map.insert("basic".to_string(), ProtocolVarVar::Bool(true));
        }

        let repo_ref = None;
        ProtocolParseResult {
            url_raw: raw_url.to_string(),
            raw: raw_map,
            proto_vars: proto_vars_map,
            query_vars: query_vars_map,
            bang_vars: bang_vars_map,
            url,
            protocol,
            query_string,
            repo_ref,
        }
    }

    /// Render a ProtocolParseResult back into a uxntal URL
    pub fn render_url(result: &ProtocolParseResult) -> String {
        let mut url = String::from("uxntal:");

        // Add protocol variables
        let mut proto_parts = Vec::new();
        for (key, value) in &result.proto_vars {
            // Skip bang variables in protocol section
            if key.starts_with('!') {
                continue;
            }

            let value_str = match value {
                ProtocolVarVar::Bool(true) => key.clone(),
                ProtocolVarVar::Bool(false) => format!("{}^^false", key),
                ProtocolVarVar::String(s) => format!("{}^^{}", key, s),
                ProtocolVarVar::Int(i) => format!("{}^^{}", key, i),
                ProtocolVarVar::Float(f) => format!("{}^^{}", key, f),
                ProtocolVarVar::Enum(e) => format!("{}^^{}", key, e),
                ProtocolVarVar::Invalid(s) => format!("{}^^{}", key, s),
            };
            proto_parts.push(value_str);
        }

        // Add bang variables to protocol section
        for (key, var) in &result.bang_vars {
            if key.starts_with('!') {
                let bang_key = key;
                let value_str = match &var.value {
                    ProtocolQueryVarVar::Bool(true) => format!("{}=true", bang_key),
                    ProtocolQueryVarVar::Bool(false) => format!("{}=false", bang_key),
                    ProtocolQueryVarVar::String(s) => format!("{}={}", bang_key, s),
                    ProtocolQueryVarVar::Int(i) => format!("{}={}", bang_key, i),
                    ProtocolQueryVarVar::Float(f) => format!("{}={}", bang_key, f),
                    ProtocolQueryVarVar::Enum(e) => format!("{}={}", bang_key, e),
                    ProtocolQueryVarVar::Invalid(s) => format!("{}={}", bang_key, s),
                };
                proto_parts.push(value_str);
            }
        }

        // Join protocol parts with colons
        if !proto_parts.is_empty() {
            url.push_str(&proto_parts.join(":"));
        }

        // Add the URL part
        url.push_str("://");
        url.push_str(&result.url);

        // Add query string if present
        if !result.query_string.is_empty() {
            if !result.query_string.starts_with('?') {
                url.push('?');
            }
            url.push_str(&result.query_string);
        } else if !result.query_vars.is_empty() {
            // Reconstruct query string from query_vars
            url.push('?');
            let mut query_parts = Vec::new();
            for (key, var) in &result.query_vars {
                if key.starts_with('!') {
                    continue; // Bang vars go in protocol section
                }
                let value_str = match &var.value {
                    ProtocolQueryVarVar::Bool(true) => format!("{}=true", key),
                    ProtocolQueryVarVar::Bool(false) => format!("{}=false", key),
                    ProtocolQueryVarVar::String(s) => format!("{}={}", key, s),
                    ProtocolQueryVarVar::Int(i) => format!("{}={}", key, i),
                    ProtocolQueryVarVar::Float(f) => format!("{}={}", key, f),
                    ProtocolQueryVarVar::Enum(e) => format!("{}={}", key, e),
                    ProtocolQueryVarVar::Invalid(s) => format!("{}={}", key, s),
                };
                query_parts.push(value_str);
            }
            url.push_str(&query_parts.join("&"));
        }

        url
    }

    /// Extract the actual target URL from a parsed uxntal URL
    pub fn extract_target(url: &str) -> Option<String> {
        fn pct_decode(s: &str) -> String {
            percent_decode_or_original(s)
        }
        fn qs_get(query: &str, key: &str) -> Option<String> {
            for pair in query.split('&') {
                let mut it = pair.splitn(2, '=');
                let k = it.next().unwrap_or("");
                let v = it.next().unwrap_or("");
                if k.eq_ignore_ascii_case(key) {
                    return Some(pct_decode(v));
                }
            }
            None
        }
        let s = url;
        let s = if s.starts_with("//https://")
            || s.starts_with("//http://")
            || s.starts_with("//file://")
        {
            s.trim_start_matches("//").to_string()
        } else if let Some(stripped) = s.strip_prefix("//https//") {
            format!("https://{}", stripped)
        } else if let Some(stripped) = s.strip_prefix("//http//") {
            format!("http://{}", stripped)
        } else if let Some(stripped) = s.strip_prefix("//file//") {
            format!("file://{}", stripped)
        } else {
            s.to_string()
        };
        if s.starts_with("open") {
            let (path, rest) = if let Some(qpos) = s.find('?') {
                (&s[..qpos], &s[qpos + 1..])
            } else {
                (&s[..], "")
            };
            if path == "open" || path == "open/" {
                if let Some(v) = qs_get(rest, "url") {
                    return Some(v);
                }
            }
        }
        for (bad, good, cut) in [
            ("https///", "https://", 8usize),
            ("http///", "http://", 7usize),
            ("file///", "file://", 7usize),
            ("https//", "https://", 7usize),
            ("http//", "http://", 6usize),
            ("file//", "file://", 7usize),
        ] {
            if s.starts_with(bad) {
                return Some(format!("{}{}", good, &s[cut..]));
            }
        }
        if s.contains('%') {
            let dec = pct_decode(&s);
            if dec.starts_with("http://")
                || dec.starts_with("https://")
                || dec.starts_with("file://")
            {
                return Some(dec);
            }
        }
        if s.starts_with("http://") || s.starts_with("https://") || s.starts_with("file://") {
            return Some(s.to_string());
        }
        Some(s.to_string())
    }
}

/// Emulator argument mapping trait
pub trait EmulatorArgMapper {
    fn map_args(&self, result: &ProtocolParseResult) -> Vec<String>;

    /// Map arguments that should come after the ROM file
    fn map_post_args(&self, _result: &ProtocolParseResult) -> Vec<String> {
        // Default implementation returns empty vector for backward compatibility
        Vec::new()
    }
}

/// EmulatorMapperFactory: returns the correct EmulatorArgMapper for a ProtocolParseResult
/// Trait for checking if an emulator binary is available in PATH
pub trait EmulatorPathCheck {
    /// Returns Some(path) if the emulator binary is found in PATH, None otherwise
    fn is_available_in_path(result: &ProtocolParseResult) -> Option<std::path::PathBuf>;
}

/// EmulatorMapperFactory: returns the correct EmulatorArgMapper and path for a ProtocolParseResult
pub fn get_emulator_launcher<'a>(
    result: &ProtocolParseResult,
) -> Option<(Box<dyn EmulatorLauncher<'a> + 'a>, std::path::PathBuf)> {
    match result.proto_vars.get("emu") {
        Some(ProtocolVarVar::Enum("buxn")) => {
            if let Some(path) = emu_buxn::BuxnMapper::is_available_in_path() {
                Some((
                    Box::new(emu_buxn::BuxnMapper) as Box<dyn EmulatorLauncher<'a> + 'a>,
                    path,
                ))
            } else {
                None
            }
        }
        Some(ProtocolVarVar::Enum("uxn")) => {
            if let Some(path) = emu_uxn::UxnMapper::is_available_in_path() {
                Some((
                    Box::new(emu_uxn::UxnMapper) as Box<dyn EmulatorLauncher<'a> + 'a>,
                    path,
                ))
            } else {
                None
            }
        }
        _ => {
            if let Some(path) = emu_cuxn::CuxnMapper::is_available_in_path(result) {
                Some((
                    Box::new(emu_cuxn::CuxnMapper) as Box<dyn EmulatorLauncher<'a> + 'a>,
                    path,
                ))
            } else {
                None
            }
        }
    }
}

/// Get emulator launcher with heuristics-based CLI selection
/// If heuristics indicate console-only usage and the emulator supports CLI, use CLI version
pub fn get_emulator_launcher_with_heuristics<'a>(
    result: &ProtocolParseResult,
    heuristics: Option<&TalHeuristics>,
) -> Option<(Box<dyn EmulatorLauncher<'a> + 'a>, std::path::PathBuf)> {
    // First try to get the regular emulator launcher
    if let Some((launcher, mut emulator_path)) = get_emulator_launcher(result) {
        // Check if we have heuristics and they indicate console-only usage
        if let Some(h) = heuristics {
            if h.uses_console && !h.uses_gui {
                if let Some(cli_name) = launcher.cli_executable_name() {
                    // Try to find the CLI version in PATH
                    #[cfg(not(target_arch = "wasm32"))]
                    if let Ok(cli_path) = which::which(cli_name) {
                        emulator_path = cli_path;
                        println!("[HEURISTICS] Detected console-only usage, switching to CLI emulator: {}", cli_name);
                    }
                }
            }
        }

        Some((launcher, emulator_path))
    } else {
        None
    }
}

/// EmulatorMapperFactory: returns the correct EmulatorArgMapper for a ProtocolParseResult
pub fn get_emulator_mapper<'a>(
    result: &ProtocolParseResult,
) -> Option<(Box<dyn EmulatorArgMapper + 'a>, Option<std::path::PathBuf>)> {
    match result.proto_vars.get("emu") {
        Some(ProtocolVarVar::Enum("buxn")) => Some((
            Box::new(emu_buxn::BuxnMapper),
            emu_buxn::BuxnMapper::is_available_in_path(),
        )),
        Some(ProtocolVarVar::Enum("uxn")) => Some((
            Box::new(emu_uxn::UxnMapper),
            emu_uxn::UxnMapper::is_available_in_path(),
        )),
        _ => Some((
            Box::new(emu_cuxn::CuxnMapper),
            emu_cuxn::CuxnMapper::is_available_in_path(result),
        )),
    }
}

/// Trait for launching an emulator with the correct command and arguments
/// Heuristics results from TAL source analysis
#[derive(Debug, Clone, Default)]
pub struct TalHeuristics {
    pub uses_console: bool,
    pub uses_gui: bool,
}

pub trait EmulatorLauncher<'a> {
    /// Build a std::process::Command for this emulator, given the protocol parse result and ROM path
    fn build_command(
        &self,
        result: &ProtocolParseResult,
        rom_path: &str,
        emulator_path: &std::path::Path,
        cache_dir: Option<&std::path::Path>,
    ) -> std::process::Command;

    /// Returns true if this emulator requires manual timeout handling (kill after timeout)
    /// Returns false if the emulator supports native --timeout flag
    fn timeout_follow_forceexit(&self, _emulator_path: &std::path::Path) -> bool {
        true // Default to manual timeout handling for compatibility
    }

    /// Returns the CLI version executable name if this emulator supports a CLI version
    /// Should return "cardinal-cli" for cuxn, None for others
    fn cli_executable_name(&self) -> Option<&'static str> {
        None // Default to no CLI version
    }
}

/// Enum of all supported emulator mappers for docgen/compatibility table
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EmulatorKind {
    Buxn,
    Uxn,
    Cuxn,
}

/// Returns all supported emulator kinds (for docgen compatibility table)
pub fn all_emulator_kinds() -> &'static [EmulatorKind] {
    use EmulatorKind::*;
    &[Buxn, Uxn, Cuxn]
}

/// Returns the display name for an emulator kind
pub fn emulator_kind_name(kind: EmulatorKind) -> &'static str {
    match kind {
        EmulatorKind::Buxn => "buxn",
        EmulatorKind::Uxn => "uxn",
        EmulatorKind::Cuxn => "cuxn",
    }
}