esp-generate 1.3.0-rc.0

Template generation tool to create no_std applications targeting Espressif's chips
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
use esp_metadata::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 {
    fn option_requires(option: &GeneratorOption, requirement: &str) -> bool {
        option.requires.iter().any(|r| r == requirement)
    }

    pub fn option_is_method_specific_for_state(
        option: &GeneratorOption,
        method_option: &str,
        method_option_selected: bool,
    ) -> bool {
        if method_option_selected {
            Self::option_requires(option, method_option)
        } else {
            let requirement = format!("!{method_option}");
            Self::option_requires(option, &requirement)
        }
    }

    pub fn can_switch_method_option(&self, method_option: &str) -> bool {
        let mut has_selected_side = false;
        let mut has_unselected_side = false;

        let negated = format!("!{method_option}");
        for option in &self.flat_options {
            if Self::option_requires(option, method_option) {
                has_selected_side = true;
            }
            if Self::option_requires(option, &negated) {
                has_unselected_side = true;
            }
            if has_selected_side && has_unselected_side {
                return true;
            }
        }

        false
    }

    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);
    }

    pub fn select_idx(&mut self, idx: usize) {
        let o = &self.flat_options[idx];
        if !self.is_option_active(o) {
            return;
        }
        if !Self::deselect_group(&mut self.selected, &self.flat_options, &o.selection_group) {
            return;
        }
        self.selected.push(idx);
    }

    /// Returns whether an item is active (can be selected).
    ///
    /// This function is different from `is_option_active` in that it handles categories as well.
    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_active(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
    }

    /// Returns whether an option is active (can be selected).
    ///
    /// This involves checking if the option is available for the current chip, if it's not
    /// disabled by any other selected option, and if all its requirements are met.
    pub fn is_option_active(&self, option: &GeneratorOption) -> bool {
        if !option.chips.is_empty() && !option.chips.contains(&self.chip) {
            return false;
        }

        // Are this option's requirements met?
        if !self.requirements_met(&option.requires) {
            return false;
        }

        // Does any of the enabled options have a requirement against this one?
        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
    }

    // 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 esp_metadata::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 requiring_not_option_only_rejects_existing_group() {
        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!["!option1".to_string()],
            }),
        ];
        let mut active = ActiveConfiguration {
            chip: Chip::Esp32,
            selected: vec![],
            flat_options: flatten_options(&options),
            options,
        };

        active.select("option1");
        let (_, opt2) = find_option("option2", &active.flat_options, Chip::Esp32).unwrap();
        assert!(!active.is_option_active(opt2));
    }

    fn empty() -> &'static [usize] {
        &[]
    }
}