@@ -24,9 +24,52 @@ pub(crate) enum HostInternal {
24
24
Ipv6 ( Ipv6Addr ) ,
25
25
}
26
26
27
- impl < S > From < Host < S > > for HostInternal {
27
+ #[ cfg( feature = "serde" ) ]
28
+ impl :: serde:: Serialize for HostInternal {
29
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
30
+ where
31
+ S : :: serde:: Serializer ,
32
+ {
33
+ // This doesn’t use `derive` because that involves
34
+ // large dependencies (that take a long time to build), and
35
+ // either Macros 1.1 which are not stable yet or a cumbersome build script.
36
+ //
37
+ // Implementing `Serializer` correctly for an enum is tricky,
38
+ // so let’s use existing enums that already do.
39
+ use std:: net:: IpAddr ;
40
+ match * self {
41
+ HostInternal :: None => None ,
42
+ HostInternal :: Domain => Some ( None ) ,
43
+ HostInternal :: Ipv4 ( addr) => Some ( Some ( IpAddr :: V4 ( addr) ) ) ,
44
+ HostInternal :: Ipv6 ( addr) => Some ( Some ( IpAddr :: V6 ( addr) ) ) ,
45
+ }
46
+ . serialize ( serializer)
47
+ }
48
+ }
49
+
50
+ #[ cfg( feature = "serde" ) ]
51
+ impl < ' de > :: serde:: Deserialize < ' de > for HostInternal {
52
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
53
+ where
54
+ D : :: serde:: Deserializer < ' de > ,
55
+ {
56
+ use std:: net:: IpAddr ;
57
+ Ok ( match :: serde:: Deserialize :: deserialize ( deserializer) ? {
58
+ None => HostInternal :: None ,
59
+ Some ( None ) => HostInternal :: Domain ,
60
+ Some ( Some ( IpAddr :: V4 ( addr) ) ) => HostInternal :: Ipv4 ( addr) ,
61
+ Some ( Some ( IpAddr :: V6 ( addr) ) ) => HostInternal :: Ipv6 ( addr) ,
62
+ } )
63
+ }
64
+ }
65
+
66
+ impl < S > From < Host < S > > for HostInternal
67
+ where
68
+ S : ToString ,
69
+ {
28
70
fn from ( host : Host < S > ) -> HostInternal {
29
71
match host {
72
+ Host :: Domain ( ref s) if s. to_string ( ) . is_empty ( ) => HostInternal :: None ,
30
73
Host :: Domain ( _) => HostInternal :: Domain ,
31
74
Host :: Ipv4 ( address) => HostInternal :: Ipv4 ( address) ,
32
75
Host :: Ipv6 ( address) => HostInternal :: Ipv6 ( address) ,
0 commit comments