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
use crate::Chip;
use crate::template::{GeneratorOption, GeneratorOptionItem};
#[derive(Debug)]
pub struct ActiveConfiguration {
/// The chip that is configured for
pub chip: Chip,
/// The names of the selected options
pub selected: Vec<usize>,
/// The tree of all available options
pub options: Vec<GeneratorOptionItem>,
/// All available option items (categories are not included), flattened to avoid the need for recursion.
pub flat_options: Vec<GeneratorOption>,
}
pub fn flatten_options(options: &[GeneratorOptionItem]) -> Vec<GeneratorOption> {
options
.iter()
.flat_map(|item| match item {
GeneratorOptionItem::Category(category) => flatten_options(&category.options),
GeneratorOptionItem::Option(option) => vec![option.clone()],
})
.collect()
}
impl ActiveConfiguration {
pub fn is_group_selected(&self, group: &str) -> bool {
self.selected
.iter()
.any(|s| self.flat_options[*s].selection_group == group)
}
pub fn is_selected(&self, option: &str) -> bool {
self.selected_index(option).is_some()
}
pub fn selected_index(&self, option: &str) -> Option<usize> {
self.selected
.iter()
.position(|s| self.flat_options[*s].name == option)
}
/// Tries to deselect all options in a selection group. Returns false if it's prevented by some
/// requirement.
fn deselect_group(selected: &mut Vec<usize>, options: &[GeneratorOption], group: &str) -> bool {
// No group, nothing to deselect
if group.is_empty() {
return true;
}
// Avoid deselecting some options then failing.
if !selected.iter().copied().all(|s| {
let o = &options[s];
if o.selection_group == group {
// We allow deselecting group options because we are changing the options in the
// group, so after this operation the group have a selected item still.
Self::can_be_disabled_impl(selected, options, s, true)
} else {
true
}
}) {
return false;
}
selected.retain(|s| options[*s].selection_group != group);
true
}
pub fn select(&mut self, option: &str) {
let (index, _o) = find_option(option, &self.flat_options, self.chip).unwrap();
self.select_idx(index);
}
/// Selects the option at `idx`.
///
/// Negative requirements (`!X`) on this option force-deselect `X` if it's currently
/// selected. The deselection cascades: anything whose requirements are no longer
/// met is dropped as well (there is no save/restore mechanism; swapped-out options
/// simply disappear until the user re-enables them).
///
/// Positive requirements remain hard gates — if any of them is unmet the call is
/// a no-op.
pub fn select_idx(&mut self, idx: usize) {
let o = self.flat_options[idx].clone();
// Positive requirements can't be materialised, so they still gate selection.
for req in &o.requires {
if req.starts_with('!') {
continue;
}
if self.is_selected(req) {
continue;
}
if Self::group_exists(req, &self.flat_options) && self.is_group_selected(req) {
continue;
}
return;
}
// If the option is already selected, leave state as-is.
if self.selected.contains(&idx) {
return;
}
// Swap out same-group siblings (the existing "radio-button" behaviour).
if !Self::deselect_group(&mut self.selected, &self.flat_options, &o.selection_group) {
return;
}
// Force-deselect anything directly forbidden by a `!X` requirement.
for req in &o.requires {
let Some(disables) = req.strip_prefix('!') else {
continue;
};
if let Some(pos) = self.selected_index(disables) {
self.selected.swap_remove(pos);
}
}
self.selected.push(idx);
// Cascade: drop anything whose requirements broke as a side effect.
self.drop_unsatisfied();
}
/// Deselects the option at `idx`, then cascades: any remaining selection whose
/// requirements were propped up by the one we just removed is dropped too. This is
/// the counterpart to [`Self::select_idx`]'s cascade, and gives the user a
/// predictable "clear, don't save" experience when toggling off an option that
/// others depend on (e.g. toggling probe-rs off clears panic-rtt-target).
pub fn deselect_idx(&mut self, idx: usize) {
let Some(pos) = self.selected.iter().position(|&s| s == idx) else {
return;
};
self.selected.swap_remove(pos);
self.drop_unsatisfied();
}
/// Fixpoint loop that evicts selected options whose requirements are no longer
/// met. Used after cascading deselection.
fn drop_unsatisfied(&mut self) {
loop {
let victim = self.selected.iter().position(|&idx| {
let opt = &self.flat_options[idx];
!self.requirements_met(&opt.requires)
});
match victim {
Some(pos) => {
self.selected.swap_remove(pos);
}
None => return,
}
}
}
/// Returns the names of currently-selected options that would be force-deselected
/// (directly or via cascade) if the user toggled the given option. Empty when
/// toggling would be non-destructive.
///
/// Symmetric: works whether the option is currently selected (simulates deselect,
/// reports cascade) or not (simulates select, reports same-group siblings,
/// negative-requirement targets, and cascade). The option being toggled is never
/// itself reported — only collateral damage.
///
/// Defensive short-circuit: if the option is currently unselected and cannot
/// actually be toggled on (chip-mismatch or an unmet *positive* requirement),
/// the toggle would be a no-op, so we return an empty list. We gate on
/// [`Self::is_option_toggleable`] rather than the strict [`Self::is_option_active`]
/// on purpose — a row whose only conflict is a negative requirement *is*
/// toggleable (selecting it cascades the conflict out), and its cascade is
/// exactly what callers want reported.
pub fn would_force_deselect(&self, option: &GeneratorOption) -> Vec<String> {
let option_idx = find_option(&option.name, &self.flat_options, self.chip).map(|(i, _)| i);
let currently_selected = option_idx
.map(|idx| self.selected.contains(&idx))
.unwrap_or(false);
if !currently_selected && !self.is_option_toggleable(option) {
return Vec::new();
}
let mut simulated = self.selected.clone();
if currently_selected {
// Simulated deselect: remove the option, then let cascade evict dependents.
if let Some(idx) = option_idx {
if let Some(pos) = simulated.iter().position(|&i| i == idx) {
simulated.swap_remove(pos);
}
}
} else {
// Simulated select: kick same-group siblings…
if !option.selection_group.is_empty() {
simulated.retain(|idx| {
let o = &self.flat_options[*idx];
o.selection_group != option.selection_group
});
}
// …and anything directly named by a `!X` requirement.
for req in &option.requires {
let Some(disables) = req.strip_prefix('!') else {
continue;
};
if let Some(pos) = simulated
.iter()
.position(|&i| self.flat_options[i].name == disables)
{
simulated.swap_remove(pos);
}
}
// Put the option into the simulated set so cascade evaluates it in context.
if let Some(idx) = option_idx {
if !simulated.contains(&idx) {
simulated.push(idx);
}
}
}
// Shared cascade: drop anything whose requirements are no longer met.
loop {
let victim = simulated.iter().position(|&idx| {
let opt = &self.flat_options[idx];
!Self::requirements_met_against(opt, &simulated, &self.flat_options)
});
match victim {
Some(pos) => {
simulated.swap_remove(pos);
}
None => break,
}
}
// Collateral = things that were selected but aren't in the simulated set
// (excluding the option itself, which is the user's direct action).
self.selected
.iter()
.copied()
.filter(|idx| !simulated.contains(idx) && Some(*idx) != option_idx)
.map(|idx| self.flat_options[idx].name.clone())
.collect()
}
/// Static helper: evaluate `option.requires` against an arbitrary selected set.
fn requirements_met_against(
option: &GeneratorOption,
selected: &[usize],
flat_options: &[GeneratorOption],
) -> bool {
for requirement in &option.requires {
let (key, expected) = if let Some(rest) = requirement.strip_prefix('!') {
(rest, false)
} else {
(requirement.as_str(), true)
};
let is_selected = selected.iter().any(|s| flat_options[*s].name == key);
if is_selected == expected {
continue;
}
let is_group = flat_options.iter().any(|o| o.selection_group == key);
if is_group {
let group_selected = selected
.iter()
.any(|s| flat_options[*s].selection_group == key);
if group_selected == expected {
continue;
}
}
return false;
}
true
}
/// Returns whether an item is toggleable in the UI.
///
/// For a category, the category's own requirements must be met (strict) *and* at
/// least one descendant must itself be toggleable; for a leaf option this uses
/// [`Self::is_option_toggleable`], i.e. an option with an unmet `!X` is still
/// considered reachable because selecting it would cascade `X` out.
///
/// This is the TUI-facing predicate. For strict validation (e.g. the CLI
/// checking whether a user's selection set is self-consistent), use
/// [`Self::is_option_active`] directly.
pub fn is_active(&self, item: &GeneratorOptionItem) -> bool {
match item {
GeneratorOptionItem::Category(category) => {
if !self.requirements_met(&category.requires) {
return false;
}
for sub in category.options.iter() {
if self.is_active(sub) {
return true;
}
}
false
}
GeneratorOptionItem::Option(option) => self.is_option_toggleable(option),
}
}
/// Returns whether all requirements are met.
///
/// A requirement may be:
/// - an `option`
/// - the absence of an `!option`
/// - a `selection_group`, which means one option in that selection group must be selected
/// - the absence of a `!selection_group`, which means no option in that selection group must
/// be selected
///
/// A selection group must not have the same name as an option.
fn requirements_met(&self, requires: &[String]) -> bool {
for requirement in requires {
let (key, expected) = if let Some(requirement) = requirement.strip_prefix('!') {
(requirement, false)
} else {
(requirement.as_str(), true)
};
// Requirement is an option that must be selected?
if self.is_selected(key) == expected {
continue;
}
// Requirement is a group that must have a selected option?
let is_group = Self::group_exists(key, &self.flat_options);
if is_group && self.is_group_selected(key) == expected {
continue;
}
return false;
}
true
}
/// Strict "is this option consistent with the current selection?" predicate.
///
/// Returns `true` only when:
/// * the option is available for the current chip,
/// * every one of its requirements is satisfied (including negative ones), and
/// * no other currently-selected option has `!{option.name}` in its `requires`.
///
/// This is the right check for validation: the CLI (`esp-generate --headless -o …`)
/// and the xtask regression harness rely on it to reject inconsistent selection
/// sets. It is *not* the right check for "can the user click this row in the TUI"
/// — that's [`Self::is_option_toggleable`], which permits negative-requirement
/// conflicts on the understanding that selecting the row will cascade them out
/// via [`Self::select_idx`].
pub fn is_option_active(&self, option: &GeneratorOption) -> bool {
if !option.chips.is_empty() && !option.chips.contains(&self.chip) {
return false;
}
if !self.requirements_met(&option.requires) {
return false;
}
for selected in self.selected.iter().copied() {
let Some(selected_option) = self.flat_options.get(selected) else {
ratatui::restore();
panic!("selected option not found: {selected}");
};
for requirement in selected_option.requires.iter() {
if let Some(requirement) = requirement.strip_prefix('!') {
if requirement == option.name {
return false;
}
}
}
}
true
}
/// Permissive "can the user toggle this row in the TUI?" predicate.
///
/// Returns `true` when the option is available for the current chip and every
/// *positive* requirement is met. Negative requirements (`!X`) are intentionally
/// ignored here: the TUI treats them as force-deselect hints, not gates.
/// Selecting a row in this state routes through [`Self::select_idx`], which
/// cascades the negative-requirement targets (and their dependents) out; use
/// [`Self::would_force_deselect`] to preview that cascade.
///
/// Callers that care about full self-consistency (CLI validation, xtask
/// coverage) must use [`Self::is_option_active`] instead.
pub fn is_option_toggleable(&self, option: &GeneratorOption) -> bool {
if !option.chips.is_empty() && !option.chips.contains(&self.chip) {
return false;
}
for req in &option.requires {
if req.starts_with('!') {
continue;
}
if self.is_selected(req) {
continue;
}
if Self::group_exists(req, &self.flat_options) && self.is_group_selected(req) {
continue;
}
return false;
}
true
}
// An option can only be disabled if it's not required by any other selected option.
pub fn can_be_disabled(&self, option: &str) -> bool {
let (option, _) = find_option(option, &self.flat_options, self.chip).unwrap();
Self::can_be_disabled_impl(&self.selected, &self.flat_options, option, false)
}
fn can_be_disabled_impl(
selected: &[usize],
options: &[GeneratorOption],
option: usize,
allow_deselecting_group: bool,
) -> bool {
let op = &options[option];
for selected in selected.iter().copied() {
let selected_option = &options[selected];
if selected_option
.requires
.iter()
.any(|o| o == &op.name || (o == &op.selection_group && !allow_deselecting_group))
{
return false;
}
}
true
}
pub fn collect_relationships<'a>(
&'a self,
option: &'a GeneratorOptionItem,
) -> Relationships<'a> {
let mut requires = Vec::new();
let mut required_by = Vec::new();
let mut disabled_by = Vec::new();
self.selected.iter().for_each(|opt| {
let opt = &self.flat_options[*opt];
for o in opt.requires.iter() {
if let Some(disables) = o.strip_prefix("!") {
if disables == option.name() {
disabled_by.push(opt.name.as_str());
}
} else if o == option.name() {
required_by.push(opt.name.as_str());
}
}
});
for req in option.requires() {
if let Some(disables) = req.strip_prefix("!") {
if self.is_selected(disables) {
disabled_by.push(disables);
}
} else {
requires.push(req.as_str());
}
}
Relationships {
requires,
required_by,
disabled_by,
}
}
fn group_exists(key: &str, options: &[GeneratorOption]) -> bool {
options.iter().any(|o| o.selection_group == key)
}
}
pub struct Relationships<'a> {
pub requires: Vec<&'a str>,
pub required_by: Vec<&'a str>,
pub disabled_by: Vec<&'a str>,
}
pub fn find_option<'c>(
option: &str,
options: &'c [GeneratorOption],
chip: Chip,
) -> Option<(usize, &'c GeneratorOption)> {
options
.iter()
.enumerate()
.find(|(_, opt)| opt.name == option && (opt.chips.is_empty() || opt.chips.contains(&chip)))
}
#[cfg(test)]
mod test {
use crate::Chip;
use crate::{
config::*,
template::{GeneratorOption, GeneratorOptionCategory, GeneratorOptionItem},
};
#[test]
fn required_by_and_requires_pick_the_right_options() {
let options = vec![
GeneratorOptionItem::Option(GeneratorOption {
name: "option1".to_string(),
display_name: "Foobar".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["option2".to_string()],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "option2".to_string(),
display_name: "Barfoo".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
];
let active = ActiveConfiguration {
chip: Chip::Esp32,
selected: vec![0],
flat_options: flatten_options(&options),
options,
};
let rels = active.collect_relationships(&active.options[0]);
assert_eq!(rels.requires, &["option2"]);
assert_eq!(rels.required_by, <&[&str]>::default());
let rels = active.collect_relationships(&active.options[1]);
assert_eq!(rels.requires, <&[&str]>::default());
assert_eq!(rels.required_by, &["option1"]);
}
#[test]
fn selecting_one_in_group_deselects_other() {
let options = vec![
GeneratorOptionItem::Option(GeneratorOption {
name: "option1".to_string(),
display_name: "Foobar".to_string(),
selection_group: "group".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "option2".to_string(),
display_name: "Barfoo".to_string(),
selection_group: "group".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "option3".to_string(),
display_name: "Prevents deselecting option2".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["option2".to_string()],
}),
];
let mut active = ActiveConfiguration {
chip: Chip::Esp32,
selected: vec![],
flat_options: flatten_options(&options),
options,
};
active.select("option1");
assert_eq!(active.selected, &[0]);
active.select("option2");
assert_eq!(active.selected, &[1]);
// Enable option3, which prevents deselecting option2, which disallows selecting option1
active.select("option3");
assert_eq!(active.selected, &[1, 2]);
active.select("option1");
assert_eq!(active.selected, &[1, 2]);
}
#[test]
fn depending_on_group_allows_changing_group_option() {
let options = vec![
GeneratorOptionItem::Category(GeneratorOptionCategory {
name: "group-options".to_string(),
display_name: "Group options".to_string(),
help: "".to_string(),
requires: vec![],
options: vec![
GeneratorOptionItem::Option(GeneratorOption {
name: "option1".to_string(),
display_name: "Foobar".to_string(),
selection_group: "group".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "option2".to_string(),
display_name: "Barfoo".to_string(),
selection_group: "group".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "option3".to_string(),
display_name: "Requires any in group to be selected".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["group".to_string()],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "option4".to_string(),
display_name: "Extra option that depends on something".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["option3".to_string()],
}),
];
let mut active = ActiveConfiguration {
chip: Chip::Esp32,
selected: vec![],
flat_options: flatten_options(&options),
options,
};
// Nothing is selected in group, so option3 can't be selected
active.select("option3");
assert_eq!(active.selected, empty());
active.select("option1");
assert_eq!(active.selected, &[0]);
active.select("option3");
assert_eq!(active.selected, &[0, 2]);
// The rejection algorithm must not trigger on unrelated options. This option is
// meant to test the group filtering. It prevents disabling "option3" but it does not
// belong to `group`, so it should not prevent selecting between "option1" or "option2".
active.select("option4");
assert_eq!(active.selected, &[0, 2, 3]);
active.select("option2");
assert_eq!(active.selected, &[2, 3, 1]);
}
#[test]
fn depending_on_group_prevents_deselecting() {
let options = vec![
GeneratorOptionItem::Option(GeneratorOption {
name: "option1".to_string(),
display_name: "Foobar".to_string(),
selection_group: "group".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "option2".to_string(),
display_name: "Barfoo".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["group".to_string()],
}),
];
let mut active = ActiveConfiguration {
chip: Chip::Esp32,
selected: vec![],
flat_options: flatten_options(&options),
options,
};
active.select("option1");
active.select("option2");
// Option1 can't be deselected because option2 requires that a `group` option is selected
assert!(!active.can_be_disabled("option1"));
}
#[test]
fn negative_requirement_force_deselects_instead_of_blocking() {
// option2 requires `!option1`. option1 is selected. option2 must be *active*
// (negative requirements no longer block selection) and selecting it must
// clear option1 rather than save it somewhere for later.
let options = vec![
GeneratorOptionItem::Option(GeneratorOption {
name: "option1".to_string(),
display_name: "Foobar".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "option2".to_string(),
display_name: "Barfoo".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["!option1".to_string()],
}),
];
let mut active = ActiveConfiguration {
chip: Chip::Esp32,
selected: vec![],
flat_options: flatten_options(&options),
options,
};
active.select("option1");
let opt2 = active.flat_options[1].clone();
// Strict validation (CLI/xtask): selection set is inconsistent → false.
assert!(!active.is_option_active(&opt2));
// TUI predicate: row is toggleable, selecting it will cascade `option1` out.
assert!(active.is_option_toggleable(&opt2));
assert_eq!(
active.would_force_deselect(&opt2),
vec!["option1".to_string()]
);
active.select("option2");
assert!(active.is_selected("option2"));
assert!(!active.is_selected("option1"));
}
#[test]
fn select_cascade_evicts_dependents_of_deselected_option() {
// Real-world analogue: selecting `probe-rs` must clear `log` (which has
// `!probe-rs`) and `embedded-test` (which requires `log`). Nothing is
// remembered — if the user toggles probe-rs back off they start from
// scratch. Symmetrically, once probe-rs *is* selected, deselecting it must
// cascade out anything requiring it (here, `panic-rtt-target`) and the
// warning predicate must list those dependents.
let options = vec![
GeneratorOptionItem::Option(GeneratorOption {
name: "probe-rs".to_string(),
display_name: "probe-rs".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "log".to_string(),
display_name: "log".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["!probe-rs".to_string()],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "embedded-test".to_string(),
display_name: "embedded-test".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["log".to_string()],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "panic-rtt-target".to_string(),
display_name: "panic-rtt-target".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["probe-rs".to_string()],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "wifi".to_string(),
display_name: "wifi".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
];
let mut active = ActiveConfiguration {
chip: Chip::Esp32,
selected: vec![],
flat_options: flatten_options(&options),
options,
};
active.select("log");
active.select("embedded-test");
active.select("wifi");
// Select side: hovering `probe-rs` while `log`/`embedded-test` are on
// reports both as going away; actually selecting it does the cascade.
let probe_rs = active.flat_options[0].clone();
let mut evicted = active.would_force_deselect(&probe_rs);
evicted.sort();
assert_eq!(
evicted,
vec!["embedded-test".to_string(), "log".to_string()]
);
active.select("probe-rs");
assert!(active.is_selected("probe-rs"));
assert!(!active.is_selected("log"));
assert!(!active.is_selected("embedded-test"));
assert!(active.is_selected("wifi"));
// Deselect side: with probe-rs on, add `panic-rtt-target` (requires
// probe-rs). Hovering probe-rs must now list panic-rtt-target in the
// warning; `deselect_idx` does the same cascade.
active.select("panic-rtt-target");
let probe_rs = active.flat_options[0].clone();
let mut evicted = active.would_force_deselect(&probe_rs);
evicted.sort();
assert_eq!(evicted, vec!["panic-rtt-target".to_string()]);
assert!(!evicted.contains(&"probe-rs".to_string())); // never itself
assert!(!evicted.contains(&"wifi".to_string())); // unrelated stays
let (probe_rs_flat_idx, _) =
find_option("probe-rs", &active.flat_options, Chip::Esp32).unwrap();
active.deselect_idx(probe_rs_flat_idx);
assert!(!active.is_selected("probe-rs"));
assert!(!active.is_selected("panic-rtt-target"));
// Previously-cleared `!probe-rs` options are NOT resurrected.
assert!(!active.is_selected("log"));
assert!(!active.is_selected("embedded-test"));
assert!(active.is_selected("wifi"));
// Short-circuit: an unselected option that isn't actually toggleable must
// report an empty cascade — toggling it is a no-op, so advertising
// collateral damage would be a lie.
//
// Fresh config (no prior selections) with three would-be-destructive
// rows whose toggle is *not* actionable:
// * chip-mismatch (`chips = [Esp32c6]` on an Esp32 config),
// * unmet positive requirement (`requires: ["missing"]`),
// * both of the above.
// Each of them also carries `!victim`, which — without the guard —
// `would_force_deselect` would still happily report.
let options = vec![
GeneratorOptionItem::Option(GeneratorOption {
name: "victim".to_string(),
display_name: "victim".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec![],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "wrong-chip".to_string(),
display_name: "wrong-chip".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32c6],
requires: vec!["!victim".to_string()],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "unmet-pos".to_string(),
display_name: "unmet-pos".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["missing".to_string(), "!victim".to_string()],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "neg-conflict".to_string(),
display_name: "neg-conflict".to_string(),
selection_group: "".to_string(),
help: "".to_string(),
chips: vec![Chip::Esp32],
requires: vec!["!victim".to_string()],
}),
];
let mut active = ActiveConfiguration {
chip: Chip::Esp32,
selected: vec![],
flat_options: flatten_options(&options),
options,
};
active.select("victim");
let wrong_chip = active
.flat_options
.iter()
.find(|o| o.name == "wrong-chip")
.cloned()
.unwrap();
let unmet_pos = active
.flat_options
.iter()
.find(|o| o.name == "unmet-pos")
.cloned()
.unwrap();
let neg_conflict = active
.flat_options
.iter()
.find(|o| o.name == "neg-conflict")
.cloned()
.unwrap();
assert!(active.would_force_deselect(&wrong_chip).is_empty());
assert!(active.would_force_deselect(&unmet_pos).is_empty());
// Negative-only conflict is still actionable — the cascade must be reported.
assert_eq!(
active.would_force_deselect(&neg_conflict),
vec!["victim".to_string()]
);
}
fn empty() -> &'static [usize] {
&[]
}
}