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
21            .insert(key, Box::new(|| Box::new(S::default())));
22    }
23
24    /// Register a step factory with an explicit key and factory function.
25    pub fn register_factory(
26        &mut self,
27        key: &str,
28        factory: impl Fn() -> Box<dyn StepBody> + Send + Sync + 'static,
29    ) {
30        self.factories.insert(key.to_string(), Box::new(factory));
31    }
32
33    /// Resolve a step instance by type name.
34    pub fn resolve(&self, step_type: &str) -> Option<Box<dyn StepBody>> {
35        self.factories.get(step_type).map(|f| f())
36    }
37}
38
39impl Default for StepRegistry {
40    fn default() -> Self {
41        Self::new()
42    }
43}