Skip to main content

camel_component_redis/
lib.rs

1pub mod config;
2pub mod consumer;
3pub mod producer;
4pub mod commands;
5
6use camel_api::{BoxProcessor, CamelError};
7use camel_component::{Component, Consumer, Endpoint, ProducerContext};
8
9pub use config::{RedisConfig, RedisCommand};
10pub use consumer::RedisConsumer;
11pub use producer::RedisProducer;
12
13pub struct RedisComponent;
14
15impl RedisComponent {
16    pub fn new() -> Self { Self }
17}
18
19impl Default for RedisComponent {
20    fn default() -> Self { Self::new() }
21}
22
23impl Component for RedisComponent {
24    fn scheme(&self) -> &str { "redis" }
25
26    fn create_endpoint(&self, uri: &str) -> Result<Box<dyn Endpoint>, CamelError> {
27        let config = RedisConfig::from_uri(uri)?;
28        Ok(Box::new(RedisEndpoint { uri: uri.to_string(), config }))
29    }
30}
31
32struct RedisEndpoint {
33    uri: String,
34    config: RedisConfig,
35}
36
37impl Endpoint for RedisEndpoint {
38    fn uri(&self) -> &str { &self.uri }
39
40    fn create_producer(&self, _ctx: &ProducerContext) -> Result<BoxProcessor, CamelError> {
41        Ok(BoxProcessor::new(RedisProducer::new(self.config.clone())))
42    }
43
44    fn create_consumer(&self) -> Result<Box<dyn Consumer>, CamelError> {
45        Ok(Box::new(RedisConsumer::new(self.config.clone())))
46    }
47}