1use std::time::Duration;
4
5#[derive(Debug, Clone)]
7pub struct RedisConfig {
8 pub url: String,
10 pub username: Option<String>,
12 pub password: Option<String>,
14 pub connection_timeout: Duration,
16 pub key_prefix: String,
18}
19
20impl RedisConfig {
21 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 pub fn with_username(mut self, username: impl Into<String>) -> Self {
34 self.username = Some(username.into());
35 self
36 }
37
38 pub fn with_password(mut self, password: impl Into<String>) -> Self {
40 self.password = Some(password.into());
41 self
42 }
43
44 pub fn with_connection_timeout(mut self, timeout: Duration) -> Self {
46 self.connection_timeout = timeout;
47 self
48 }
49
50 pub fn with_key_prefix(mut self, prefix: impl Into<String>) -> Self {
52 self.key_prefix = prefix.into();
53 self
54 }
55
56 pub(crate) fn build_connection_url(&self) -> String {
58 let mut url = self.url.clone();
59
60 if !url.starts_with("redis://") && !url.starts_with("rediss://") {
62 url = format!("redis://{}", url);
63 }
64
65 if !url.ends_with('/') {
67 url.push('/');
68 }
69
70 url
71 }
72}