doido_controller/
initializers.rs1use doido_core::Result;
6
7type Init = Box<dyn FnOnce() -> Result<()> + Send>;
8
9#[derive(Default)]
11pub struct Initializers {
12 inits: Vec<(String, Init)>,
13}
14
15impl Initializers {
16 pub fn new() -> Self {
17 Self::default()
18 }
19
20 pub fn register(
22 &mut self,
23 name: &str,
24 init: impl FnOnce() -> Result<()> + Send + 'static,
25 ) -> &mut Self {
26 self.inits.push((name.to_string(), Box::new(init)));
27 self
28 }
29
30 pub fn names(&self) -> Vec<&str> {
32 self.inits.iter().map(|(n, _)| n.as_str()).collect()
33 }
34
35 pub fn run_all(self) -> Result<()> {
37 for (name, init) in self.inits {
38 init().map_err(|e| doido_core::anyhow::anyhow!("initializer `{name}` failed: {e}"))?;
39 }
40 Ok(())
41 }
42}