Skip to main content

wfe_core/executor/
step_registry.rs

1use std::collections::HashMap;
2
3use crate::traits::StepBody;
4
5/// Registry of step factories keyed by type name.
6pub struct StepRegistry {
7    factories: HashMap<String, Box<dyn Fn() -> Box<dyn StepBody> + Send + Sync>>,
8}
9
10impl StepRegistry {
11    pub fn new() -> Self {
12        Self {
13            factories: HashMap::new(),
14        }
15    }
16
17    /// Register a step type using its full type name as the key.
18    pub fn register<S: StepBody + Default + 'static>(&mut self) {
19        let key = std::any::type_name::<S>().to_string();
20        self.factories.insert(key, Box::new(|| Box::new(S::default())));
21    }
22
23    /// Register a step factory with an explicit key and factory function.
24    pub fn register_factory(
25        &mut self,
26        key: &str,
27        factory: impl Fn() -> Box<dyn StepBody> + Send + Sync + 'static,
28    ) {
29        self.factories.insert(key.to_string(), Box::new(factory));
30    }
31
32    /// Resolve a step instance by type name.
33    pub fn resolve(&self, step_type: &str) -> Option<Box<dyn StepBody>> {
34        self.factories.get(step_type).map(|f| f())
35    }
36}
37
38impl Default for StepRegistry {
39    fn default() -> Self {
40        Self::new()
41    }
42}