Skip to main content

camel_component_redis/
lib.rs

1pub mod commands;
2pub mod config;
3pub mod consumer;
4pub mod producer;
5
6use camel_api::{BoxProcessor, CamelError};
7use camel_component::{Component, Consumer, Endpoint, ProducerContext};
8
9pub use config::{RedisCommand, RedisConfig, RedisEndpointConfig};
10pub use consumer::RedisConsumer;
11pub use producer::RedisProducer;
12
13pub struct RedisComponent {
14    config: Option<RedisConfig>,
15}
16
17impl RedisComponent {
18    /// Create a new RedisComponent without global config defaults.
19    /// Endpoint configs will fall back to hardcoded defaults via `resolve_defaults()`.
20    pub fn new() -> Self {
21        Self { config: None }
22    }
23
24    /// Create a RedisComponent with global config defaults.
25    /// These will be applied to endpoint configs before `resolve_defaults()`.
26    pub fn with_config(config: RedisConfig) -> Self {
27        Self {
28            config: Some(config),
29        }
30    }
31
32    /// Create a RedisComponent with optional global config defaults.
33    /// If `None`, behaves like `new()` (uses hardcoded defaults only).
34    pub fn with_optional_config(config: Option<RedisConfig>) -> Self {
35        Self { config }
36    }
37}
38
39impl Default for RedisComponent {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45impl Component for RedisComponent {
46    fn scheme(&self) -> &str {
47        "redis"
48    }
49
50    fn create_endpoint(&self, uri: &str) -> Result<Box<dyn Endpoint>, CamelError> {
51        let mut config = RedisEndpointConfig::from_uri(uri)?;
52        // Apply global config defaults if available
53        if let Some(ref global_cfg) = self.config {
54            config.apply_defaults(global_cfg);
55        }
56        // Resolve any remaining None fields to hardcoded defaults
57        config.resolve_defaults();
58        Ok(Box::new(RedisEndpoint {
59            uri: uri.to_string(),
60            config,
61        }))
62    }
63}
64
65struct RedisEndpoint {
66    uri: String,
67    config: RedisEndpointConfig,
68}
69
70impl Endpoint for RedisEndpoint {
71    fn uri(&self) -> &str {
72        &self.uri
73    }
74
75    fn create_producer(&self, _ctx: &ProducerContext) -> Result<BoxProcessor, CamelError> {
76        Ok(BoxProcessor::new(RedisProducer::new(self.config.clone())))
77    }
78
79    fn create_consumer(&self) -> Result<Box<dyn Consumer>, CamelError> {
80        Ok(Box::new(RedisConsumer::new(self.config.clone())))
81    }
82}