address/domain/
conversions.rs

1use crate::{Domain, DomainRef, Endpoint, EndpointRef, Host, HostRef};
2
3impl Domain {
4    //! Conversions
5
6    /// Converts the domain to a domain reference.
7    pub fn to_ref(&self) -> DomainRef<'_> {
8        unsafe { DomainRef::new(self.name()) }
9    }
10
11    /// Converts the domain to an endpoint with the `port`.
12    pub fn to_endpoint(self, port: u16) -> Endpoint {
13        Endpoint::new(self, port)
14    }
15
16    /// Converts the domain to a host.
17    pub fn to_host(self) -> Host {
18        Host::Name(self)
19    }
20}
21
22impl<'a> DomainRef<'a> {
23    //! Conversions
24
25    /// Converts the domain reference to a domain.
26    pub fn to_domain(&self) -> Domain {
27        unsafe { Domain::new(self.name()) }
28    }
29
30    /// Converts the domain reference to an endpoint reference with the `port`.
31    pub const fn to_endpoint(&self, port: u16) -> EndpointRef<'_> {
32        EndpointRef::new(*self, port)
33    }
34
35    /// Converts the domain reference to a host reference.
36    pub const fn to_host(&self) -> HostRef<'_> {
37        HostRef::Name(*self)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use crate::{Domain, DomainRef, Endpoint, EndpointRef, Host, HostRef};
44
45    #[test]
46    fn domain_to_ref() {
47        let domain: Domain = Domain::localhost();
48
49        let result: DomainRef = domain.to_ref();
50        let expected: DomainRef = DomainRef::LOCALHOST;
51        assert_eq!(result, expected);
52    }
53
54    #[test]
55    fn domain_to_endpoint() {
56        let domain: Domain = Domain::localhost();
57
58        let result: Endpoint = domain.to_endpoint(80);
59        let expected: Endpoint = Endpoint::new(Domain::localhost(), 80);
60        assert_eq!(result, expected);
61    }
62
63    #[test]
64    fn domain_to_host() {
65        let domain: Domain = Domain::localhost();
66
67        let result: Host = domain.to_host();
68        let expected: Host = Host::Name(Domain::localhost());
69        assert_eq!(result, expected);
70    }
71
72    #[test]
73    fn ref_to_domain() {
74        let domain: DomainRef = DomainRef::LOCALHOST;
75
76        let result: Domain = domain.to_domain();
77        let expected: Domain = Domain::localhost();
78        assert_eq!(result, expected);
79    }
80
81    #[test]
82    fn ref_to_endpoint() {
83        let domain: DomainRef = DomainRef::LOCALHOST;
84
85        let result: EndpointRef = domain.to_endpoint(80);
86        let expected: EndpointRef = EndpointRef::new(DomainRef::LOCALHOST, 80);
87        assert_eq!(result, expected);
88    }
89
90    #[test]
91    fn ref_to_host() {
92        let domain: DomainRef = DomainRef::LOCALHOST;
93
94        let result: HostRef = domain.to_host();
95        let expected: HostRef = HostRef::Name(DomainRef::LOCALHOST);
96        assert_eq!(result, expected);
97    }
98}