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
use crate::domain::Domain;
use crate::ip::IPAddress;

/// Represents an IP address or a domain name.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Host {

    /// An IP Address
    Address(IPAddress),

    /// A Domain Name
    Name(Domain),
}

impl ToString for Host {

    /// ```
    /// use inet::domain::Domain;
    /// use std::convert::TryFrom;
    /// use inet::host::Host;
    /// use inet::ip::{IPv4Address, IPAddress};
    ///
    /// let ip: Host = Host::Address(IPAddress::V4(IPv4Address::LOCALHOST));
    /// assert_eq!(ip.to_string(), "127.0.0.1");
    ///
    /// let domain: Host = Host::Name(Domain::try_from("localhost".as_bytes()).ok().unwrap());
    /// assert_eq!(domain.to_string(), "localhost");
    /// ```
    fn to_string(&self) -> String {
        match self {
            Host::Address(ip) => ip.to_string(),
            Host::Name(domain) => domain.to_string(),
        }
    }
}