1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use redis::ConnectionInfo;
use serde::{Deserializer, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
pub struct AsConnectionInfo;
impl SerializeAs<ConnectionInfo> for AsConnectionInfo {
fn serialize_as<S>(value: &ConnectionInfo, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_redis_connection::serialize(value, serializer)
}
}
impl<'de> DeserializeAs<'de, ConnectionInfo> for AsConnectionInfo {
fn deserialize_as<D>(deserializer: D) -> Result<ConnectionInfo, D::Error>
where
D: Deserializer<'de>,
{
serde_redis_connection::deserialize(deserializer)
}
}
pub mod serde_redis_connection {
use redis::{ConnectionInfo, IntoConnectionInfo};
use serde::{
de::{Unexpected, Visitor},
Deserializer, Serializer,
};
pub fn serialize<S>(connection_info: &ConnectionInfo, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(
match connection_info {
ConnectionInfo {
addr,
db,
username: Some(username),
passwd: Some(passwd),
} => format!("redis://{}:{}@{}/{}", username, passwd, addr, db),
ConnectionInfo {
addr,
db,
username: Some(username),
passwd: None,
} => format!("redis://{}@{}/{}", username, addr, db),
ConnectionInfo {
addr,
db,
username: None,
passwd: Some(passwd),
} => format!("redis://:{}@{}/{}", passwd, addr, db),
ConnectionInfo {
addr,
db,
username: None,
passwd: None,
} => format!("redis://{}/{}", addr, db),
}
.as_str(),
)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<redis::ConnectionInfo, D::Error>
where
D: Deserializer<'de>,
{
struct V;
impl<'de> Visitor<'de> for V {
type Value = ConnectionInfo;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "a valid Redis connection info")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
v.into_connection_info()
.map_err(|_err| E::invalid_value(Unexpected::Str(v), &self))
}
}
deserializer.deserialize_string(V)
}
}