opendal/services/redis/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19use std::fmt::Formatter;
20use std::time::Duration;
21
22use super::backend::RedisBuilder;
23use serde::Deserialize;
24use serde::Serialize;
25
26/// Config for Redis services support.
27#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct RedisConfig {
31    /// network address of the Redis service. Can be "tcp://127.0.0.1:6379", e.g.
32    ///
33    /// default is "tcp://127.0.0.1:6379"
34    pub endpoint: Option<String>,
35    /// network address of the Redis cluster service. Can be "tcp://127.0.0.1:6379,tcp://127.0.0.1:6380,tcp://127.0.0.1:6381", e.g.
36    ///
37    /// default is None
38    pub cluster_endpoints: Option<String>,
39    /// the username to connect redis service.
40    ///
41    /// default is None
42    pub username: Option<String>,
43    /// the password for authentication
44    ///
45    /// default is None
46    pub password: Option<String>,
47    /// the working directory of the Redis service. Can be "/path/to/dir"
48    ///
49    /// default is "/"
50    pub root: Option<String>,
51    /// the number of DBs redis can take is unlimited
52    ///
53    /// default is db 0
54    pub db: i64,
55    /// The default ttl for put operations.
56    pub default_ttl: Option<Duration>,
57}
58
59impl Debug for RedisConfig {
60    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
61        let mut d = f.debug_struct("RedisConfig");
62
63        d.field("db", &self.db.to_string());
64        d.field("root", &self.root);
65        if let Some(endpoint) = self.endpoint.clone() {
66            d.field("endpoint", &endpoint);
67        }
68        if let Some(cluster_endpoints) = self.cluster_endpoints.clone() {
69            d.field("cluster_endpoints", &cluster_endpoints);
70        }
71        if let Some(username) = self.username.clone() {
72            d.field("username", &username);
73        }
74        if self.password.is_some() {
75            d.field("password", &"<redacted>");
76        }
77
78        d.finish_non_exhaustive()
79    }
80}
81
82impl crate::Configurator for RedisConfig {
83    type Builder = RedisBuilder;
84    fn into_builder(self) -> Self::Builder {
85        RedisBuilder { config: self }
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn test_redis_builder_interface() {
95        // Test that RedisBuilder still works with the new implementation
96        let builder = RedisBuilder::default()
97            .endpoint("redis://localhost:6379")
98            .username("testuser")
99            .password("testpass")
100            .db(1)
101            .root("/test");
102
103        // The builder should be able to create configuration
104        assert!(builder.config.endpoint.is_some());
105        assert_eq!(
106            builder.config.endpoint.as_ref().unwrap(),
107            "redis://localhost:6379"
108        );
109        assert_eq!(builder.config.username.as_ref().unwrap(), "testuser");
110        assert_eq!(builder.config.password.as_ref().unwrap(), "testpass");
111        assert_eq!(builder.config.db, 1);
112        assert_eq!(builder.config.root.as_ref().unwrap(), "/test");
113    }
114}