espforge_lib/nibblers/
mod.rs

1use crate::config::EspforgeConfiguration;
2use inventory;
3
4pub trait ConfigNibbler: Send + Sync {
5    fn name(&self) -> &str;
6
7    /// Priority determines execution order.
8    /// Lower numbers run first.
9    fn priority(&self) -> u8 {
10        50
11    }
12
13    fn process(&self, config: &EspforgeConfiguration) -> Result<NibblerResult, String>;
14}
15
16#[derive(Debug)]
17pub struct NibblerResult {
18    pub nibbler_name: String,
19    pub findings: Vec<String>,
20    pub status: NibblerStatus,
21}
22
23#[derive(Debug, PartialEq, Eq)]
24pub enum NibblerStatus {
25    Ok,
26    Warning,
27    Error,
28}
29
30pub type NibblerFactory = fn() -> Box<dyn ConfigNibbler>;
31
32pub struct NibblerRegistration {
33    pub factory: NibblerFactory,
34}
35
36inventory::collect!(NibblerRegistration);
37
38#[macro_export]
39macro_rules! register_nibbler {
40    ($nibbler:ty) => {
41        inventory::submit! {
42            $crate::nibblers::NibblerRegistration {
43                factory: || Box::new(<$nibbler>::default())
44            }
45        }
46    };
47}
48
49pub struct NibblerDispatcher {
50    nibblers: Vec<Box<dyn ConfigNibbler>>,
51}
52
53impl Default for NibblerDispatcher {
54    fn default() -> Self {
55        Self::new()
56    }
57}
58
59impl NibblerDispatcher {
60    pub fn new() -> Self {
61        let mut nibblers = Vec::new();
62
63        for registration in inventory::iter::<NibblerRegistration> {
64            nibblers.push((registration.factory)());
65        }
66
67        // Sort by priority, lower number higher priority
68        nibblers.sort_by_key(|a| a.priority());
69
70        Self { nibblers }
71    }
72
73    pub fn process_config(&self, config: &EspforgeConfiguration) -> Vec<NibblerResult> {
74        self.nibblers
75            .iter()
76            .filter_map(|nibbler| match nibbler.process(config) {
77                Ok(result) => Some(result),
78                Err(e) => {
79                    eprintln!("Internal Error in nibbler '{}': {}", nibbler.name(), e);
80                    None
81                }
82            })
83            .collect()
84    }
85}
86
87pub mod app;
88pub mod components;
89pub mod esp32;
90pub mod project;
91pub mod template;