Skip to main content

address/host/
conversions_ref.rs

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