Skip to main content

camel_core/shared/components/domain/
registry.rs

1use std::collections::HashMap;
2
3use camel_api::CamelError;
4use camel_component::Component;
5
6/// Registry that stores components by their URI scheme.
7pub struct Registry {
8    components: HashMap<String, Box<dyn Component>>,
9}
10
11impl Registry {
12    /// Create an empty registry.
13    pub fn new() -> Self {
14        Self {
15            components: HashMap::new(),
16        }
17    }
18
19    /// Register a component. Replaces any existing component with the same scheme.
20    pub fn register<C: Component + 'static>(&mut self, component: C) {
21        self.components
22            .insert(component.scheme().to_string(), Box::new(component));
23    }
24
25    /// Look up a component by scheme.
26    pub fn get(&self, scheme: &str) -> Option<&dyn Component> {
27        self.components.get(scheme).map(|c| c.as_ref())
28    }
29
30    /// Look up a component by scheme, returning an error if not found.
31    pub fn get_or_err(&self, scheme: &str) -> Result<&dyn Component, CamelError> {
32        self.get(scheme)
33            .ok_or_else(|| CamelError::ComponentNotFound(scheme.to_string()))
34    }
35
36    /// Returns the number of registered components.
37    pub fn len(&self) -> usize {
38        self.components.len()
39    }
40
41    /// Returns true if no components are registered.
42    pub fn is_empty(&self) -> bool {
43        self.components.is_empty()
44    }
45}
46
47impl Default for Registry {
48    fn default() -> Self {
49        Self::new()
50    }
51}