Skip to main content

camel_component_container/
bundle.rs

1use std::sync::Arc;
2
3use camel_component_api::{CamelError, ComponentBundle, ComponentRegistrar};
4
5use crate::{ContainerComponent, ContainerGlobalConfig};
6
7pub struct ContainerBundle {
8    config: ContainerGlobalConfig,
9}
10
11impl ComponentBundle for ContainerBundle {
12    fn config_key() -> &'static str {
13        "container"
14    }
15
16    fn from_toml(value: toml::Value) -> Result<Self, CamelError> {
17        let config: ContainerGlobalConfig = value
18            .try_into()
19            .map_err(|e: toml::de::Error| CamelError::Config(e.to_string()))?;
20        Ok(Self { config })
21    }
22
23    fn register_all(self, ctx: &mut dyn ComponentRegistrar) {
24        ctx.register_component_dyn(Arc::new(ContainerComponent::with_config(self.config)));
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use camel_component_api::ComponentBundle;
32
33    struct TestRegistrar {
34        schemes: Vec<String>,
35    }
36
37    impl camel_component_api::ComponentRegistrar for TestRegistrar {
38        fn register_component_dyn(
39            &mut self,
40            component: std::sync::Arc<dyn camel_component_api::Component>,
41        ) {
42            self.schemes.push(component.scheme().to_string());
43        }
44    }
45
46    #[test]
47    fn container_bundle_from_toml_empty_uses_defaults() {
48        let value: toml::Value = toml::from_str("").unwrap();
49        let result = ContainerBundle::from_toml(value);
50        assert!(
51            result.is_ok(),
52            "empty TOML must use defaults: {:?}",
53            result.err()
54        );
55    }
56
57    #[test]
58    fn container_bundle_registers_expected_schemes() {
59        let bundle = ContainerBundle::from_toml(toml::Value::Table(toml::map::Map::new())).unwrap();
60        let mut registrar = TestRegistrar { schemes: vec![] };
61
62        bundle.register_all(&mut registrar);
63
64        assert_eq!(registrar.schemes, vec!["container"]);
65    }
66
67    #[test]
68    fn container_bundle_from_toml_returns_error_on_invalid_config() {
69        let mut table = toml::map::Map::new();
70        table.insert("docker_host".to_string(), toml::Value::Integer(123));
71
72        let result = ContainerBundle::from_toml(toml::Value::Table(table));
73        assert!(result.is_err(), "expected Err on malformed config");
74        let err_msg = match result {
75            Err(err) => err.to_string(),
76            Ok(_) => panic!("expected Err on malformed config"),
77        };
78        assert!(
79            err_msg.contains("Configuration error"),
80            "expected CamelError::Config, got: {err_msg}"
81        );
82    }
83}