Skip to main content

camel_component_wasm/
bundle.rs

1use std::path::PathBuf;
2use std::sync::Arc;
3
4use camel_component_api::{CamelError, ComponentBundle, ComponentContext, ComponentRegistrar};
5
6use crate::WasmComponent;
7
8pub struct WasmBundle {
9    registry: Arc<dyn ComponentContext>,
10    base_dir: PathBuf,
11}
12
13impl WasmBundle {
14    pub fn new(registry: Arc<dyn ComponentContext>, base_dir: PathBuf) -> Self {
15        Self { registry, base_dir }
16    }
17}
18
19impl ComponentBundle for WasmBundle {
20    fn config_key() -> &'static str {
21        "wasm"
22    }
23
24    fn from_toml(_value: toml::Value) -> Result<Self, CamelError> {
25        Err(CamelError::Config(
26            "WasmBundle requires registry and base_dir — use WasmBundle::new() instead".to_string(),
27        ))
28    }
29
30    fn register_all(self, ctx: &mut dyn ComponentRegistrar) {
31        let component = WasmComponent::new(self.registry, self.base_dir);
32        ctx.register_component_dyn(Arc::new(component));
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use camel_component_api::ComponentContext;
40
41    struct TestRegistrar {
42        schemes: Vec<String>,
43    }
44
45    impl ComponentRegistrar for TestRegistrar {
46        fn register_component_dyn(&mut self, component: Arc<dyn camel_component_api::Component>) {
47            self.schemes.push(component.scheme().to_string());
48        }
49    }
50
51    #[test]
52    fn wasm_bundle_from_toml_returns_error() {
53        let value: toml::Value = toml::from_str("").unwrap();
54        let result = WasmBundle::from_toml(value);
55        assert!(result.is_err());
56    }
57
58    #[test]
59    fn wasm_bundle_registers_wasm_scheme() {
60        let registry: Arc<dyn ComponentContext> =
61            Arc::new(camel_component_api::NoOpComponentContext);
62        let bundle = WasmBundle::new(registry, PathBuf::from("."));
63        let mut registrar = TestRegistrar { schemes: vec![] };
64
65        bundle.register_all(&mut registrar);
66
67        assert_eq!(registrar.schemes, vec!["wasm"]);
68    }
69}