floxide_redis/
config.rs

1//! Redis configuration for Floxide distributed workflow system.
2
3use std::time::Duration;
4
5/// Configuration for Redis connection.
6#[derive(Debug, Clone)]
7pub struct RedisConfig {
8    /// Redis connection URL (e.g., "redis://127.0.0.1:6379/")
9    pub url: String,
10    /// Optional username for Redis authentication
11    pub username: Option<String>,
12    /// Optional password for Redis authentication
13    pub password: Option<String>,
14    /// Connection timeout in seconds
15    pub connection_timeout: Duration,
16    /// Key prefix for Redis keys to avoid collisions
17    pub key_prefix: String,
18}
19
20impl RedisConfig {
21    /// Create a new Redis configuration with the given URL.
22    pub fn new(url: impl Into<String>) -> Self {
23        Self {
24            url: url.into(),
25            username: None,
26            password: None,
27            connection_timeout: Duration::from_secs(5),
28            key_prefix: "floxide:".to_string(),
29        }
30    }
31
32    /// Set the username for Redis authentication.
33    pub fn with_username(mut self, username: impl Into<String>) -> Self {
34        self.username = Some(username.into());
35        self
36    }
37
38    /// Set the password for Redis authentication.
39    pub fn with_password(mut self, password: impl Into<String>) -> Self {
40        self.password = Some(password.into());
41        self
42    }
43
44    /// Set the connection timeout.
45    pub fn with_connection_timeout(mut self, timeout: Duration) -> Self {
46        self.connection_timeout = timeout;
47        self
48    }
49
50    /// Set the key prefix for Redis keys.
51    pub fn with_key_prefix(mut self, prefix: impl Into<String>) -> Self {
52        self.key_prefix = prefix.into();
53        self
54    }
55
56    /// Build the Redis connection URL with authentication if provided.
57    pub(crate) fn build_connection_url(&self) -> String {
58        let mut url = self.url.clone();
59
60        // If URL doesn't have redis:// or rediss:// prefix, add it
61        if !url.starts_with("redis://") && !url.starts_with("rediss://") {
62            url = format!("redis://{}", url);
63        }
64
65        // If URL doesn't end with /, add it
66        if !url.ends_with('/') {
67            url.push('/');
68        }
69
70        url
71    }
72}