camel_component_redis/
bundle.rs1use std::sync::Arc;
2
3use camel_component_api::{CamelError, ComponentBundle, ComponentRegistrar};
4
5use crate::{RedisComponent, config::RedisConfig};
6
7pub struct RedisBundle {
8 config: RedisConfig,
9}
10
11impl ComponentBundle for RedisBundle {
12 fn config_key() -> &'static str {
13 "redis"
14 }
15
16 fn from_toml(value: toml::Value) -> Result<Self, CamelError> {
17 let config: RedisConfig = 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(RedisComponent::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 redis_bundle_from_toml_empty_uses_defaults() {
48 let value: toml::Value = toml::from_str("").unwrap();
49 let result = RedisBundle::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 redis_bundle_registers_expected_schemes() {
59 let bundle = RedisBundle::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!["redis"]);
65 }
66
67 #[test]
68 fn redis_bundle_from_toml_returns_error_on_invalid_config() {
69 let mut table = toml::map::Map::new();
70 table.insert(
71 "port".to_string(),
72 toml::Value::String("not-a-number".to_string()),
73 );
74
75 let result = RedisBundle::from_toml(toml::Value::Table(table));
76 assert!(result.is_err(), "expected Err on malformed config");
77 let err_msg = match result {
78 Err(err) => err.to_string(),
79 Ok(_) => panic!("expected Err on malformed config"),
80 };
81 assert!(
82 err_msg.contains("Configuration error"),
83 "expected CamelError::Config, got: {err_msg}"
84 );
85 }
86}