rs-zero 0.1.0

Rust-first microservice framework inspired by go-zero engineering practices
Documentation
use crate::cache_redis::{RedisCacheConfig, RedisCacheError, RedisCacheResult};

/// Factory for Redis clients.
#[derive(Debug, Clone)]
pub struct RedisClientFactory {
    config: RedisCacheConfig,
}

impl RedisClientFactory {
    /// Creates a factory and validates the URL shape.
    pub fn new(config: RedisCacheConfig) -> RedisCacheResult<Self> {
        if !config.url.starts_with("redis://") && !config.url.starts_with("rediss://") {
            return Err(RedisCacheError::InvalidUrl { url: config.url });
        }
        Ok(Self { config })
    }

    /// Returns the configured Redis URL.
    pub fn url(&self) -> &str {
        &self.config.url
    }

    /// Creates a real Redis client when the `cache-redis` feature is enabled.
    #[cfg(feature = "cache-redis")]
    pub fn create_client(&self) -> RedisCacheResult<redis::Client> {
        redis::Client::open(self.config.url.as_str())
            .map_err(|error| RedisCacheError::Backend(error.to_string()))
    }
}