address/host/
conversions_ref.rs1use crate::{Authority, AuthorityRef, Domain, DomainRef, Host, HostRef, IPAddress};
2
3impl<'a> HostRef<'a> {
4 pub fn to_host(self) -> Host {
8 match self {
9 Self::Domain(domain) => Host::Domain(domain.to_domain()),
10 Self::IPAddress(ip) => Host::IPAddress(ip),
11 }
12 }
13
14 pub fn to_domain(self) -> Result<Domain, Self> {
16 if let Self::Domain(domain) = self {
17 Ok(domain.to_domain())
18 } else {
19 Err(self)
20 }
21 }
22
23 pub const fn to_domain_ref(self) -> Result<DomainRef<'a>, Self> {
25 if let Self::Domain(domain) = self {
26 Ok(domain)
27 } else {
28 Err(self)
29 }
30 }
31
32 pub const fn to_ip(self) -> Result<IPAddress, Self> {
34 if let Self::IPAddress(ip) = self {
35 Ok(ip)
36 } else {
37 Err(self)
38 }
39 }
40
41 pub fn to_authority(self, port: u16) -> Authority {
43 Authority::new(self.to_host(), port)
44 }
45
46 pub const fn to_authority_ref(self, port: u16) -> AuthorityRef<'a> {
48 AuthorityRef::new(self, port)
49 }
50}
51
52impl<'a> From<&'a Host> for HostRef<'a> {
53 fn from(host: &'a Host) -> Self {
54 host.to_ref()
55 }
56}
57
58impl<'a> From<DomainRef<'a>> for HostRef<'a> {
59 fn from(domain: DomainRef<'a>) -> Self {
60 domain.to_host_ref()
61 }
62}
63
64impl<'a, A: Into<IPAddress>> From<A> for HostRef<'a> {
65 fn from(ip: A) -> Self {
66 ip.into().to_host_ref()
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use crate::{
73 Authority, AuthorityRef, Domain, DomainRef, Host, HostRef, IPAddress, IPv4Address,
74 };
75
76 #[test]
77 fn ref_to_host() {
78 let host: HostRef = HostRef::Domain(DomainRef::LOCALHOST);
79 let result: Host = host.to_host();
80 let expected: Host = Host::Domain(Domain::localhost());
81 assert_eq!(result, expected);
82
83 let host: HostRef = HostRef::IPAddress(IPAddress::V4(IPv4Address::LOCALHOST));
84 let result: Host = host.to_host();
85 let expected: Host = Host::IPAddress(IPAddress::V4(IPv4Address::LOCALHOST));
86 assert_eq!(result, expected);
87 }
88
89 #[test]
90 fn ref_to_domain() {
91 let host: HostRef = DomainRef::LOCALHOST.to_host_ref();
92 let result: Result<Domain, HostRef> = host.to_domain();
93 let expected: Result<Domain, HostRef> = Ok(Domain::localhost());
94 assert_eq!(result, expected);
95
96 let result: Result<DomainRef, HostRef> = host.to_domain_ref();
97 let expected: Result<DomainRef, HostRef> = Ok(DomainRef::LOCALHOST);
98 assert_eq!(result, expected);
99
100 let host: HostRef = IPv4Address::LOCALHOST.to_host_ref();
101 let result: Result<Domain, HostRef> = host.to_domain();
102 let expected: Result<Domain, HostRef> = Err(host);
103 assert_eq!(result, expected);
104
105 let result: Result<DomainRef, HostRef> = host.to_domain_ref();
106 let expected: Result<DomainRef, HostRef> = Err(host);
107 assert_eq!(result, expected);
108 }
109
110 #[test]
111 fn ref_to_ip() {
112 let host: HostRef = IPv4Address::LOCALHOST.to_host_ref();
113 let result: Result<IPAddress, HostRef> = host.to_ip();
114 let expected: Result<IPAddress, HostRef> = Ok(IPv4Address::LOCALHOST.to_ip());
115 assert_eq!(result, expected);
116
117 let host: HostRef = DomainRef::LOCALHOST.to_host_ref();
118 let result: Result<IPAddress, HostRef> = host.to_ip();
119 let expected: Result<IPAddress, HostRef> = Err(host);
120 assert_eq!(result, expected);
121 }
122
123 #[test]
124 fn ref_to_authority() {
125 let host: HostRef = DomainRef::LOCALHOST.to_host_ref();
126 let result: Authority = host.to_authority(80);
127 let expected: Authority = Authority::new(Host::Domain(Domain::localhost()), 80);
128 assert_eq!(result, expected);
129
130 let result: AuthorityRef = host.to_authority_ref(80);
131 let expected: AuthorityRef = AuthorityRef::new(HostRef::Domain(DomainRef::LOCALHOST), 80);
132 assert_eq!(result, expected);
133
134 let host: HostRef = IPv4Address::LOCALHOST.to_host_ref();
135 let result: Authority = host.to_authority(80);
136 let expected: Authority =
137 Authority::new(Host::IPAddress(IPAddress::V4(IPv4Address::LOCALHOST)), 80);
138 assert_eq!(result, expected);
139
140 let result: AuthorityRef = host.to_authority_ref(80);
141 let expected: AuthorityRef = AuthorityRef::new(
142 HostRef::IPAddress(IPAddress::V4(IPv4Address::LOCALHOST)),
143 80,
144 );
145 assert_eq!(result, expected);
146 }
147
148 #[test]
149 fn ref_from() {
150 let expected: HostRef = HostRef::Domain(DomainRef::LOCALHOST);
151
152 let owned: Host = Host::Domain(Domain::localhost());
153 let result: HostRef = (&owned).into();
154 assert_eq!(result, expected);
155
156 let result: HostRef = DomainRef::LOCALHOST.into();
157 assert_eq!(result, expected);
158
159 let expected: HostRef = HostRef::IPAddress(IPAddress::V4(IPv4Address::LOCALHOST));
160 let result: HostRef = IPv4Address::LOCALHOST.into();
161 assert_eq!(result, expected);
162 }
163}