Skip to main content

whois_rust/
who_is_host.rs

1use validators::prelude::*;
2use validators_prelude::Host;
3
4/// The host of a WHOIS server, with an optional port.
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Validator)]
6#[validator(host)]
7pub struct WhoIsHost {
8    pub(crate) host: Host,
9    pub(crate) port: Option<u16>,
10}
11
12impl WhoIsHost {
13    /// Get the host part.
14    #[inline]
15    pub const fn host(&self) -> &Host {
16        &self.host
17    }
18
19    /// Get the port part. `None` means the default WHOIS port (43) is used.
20    #[inline]
21    pub const fn port(&self) -> Option<u16> {
22        self.port
23    }
24
25    pub(crate) fn to_addr_string(&self, default_port: u16) -> String {
26        let port = self.port.unwrap_or(default_port);
27
28        match &self.host {
29            Host::IPv4(ip) => format!("{}:{}", ip, port),
30            Host::IPv6(ip) => format!("[{}]:{}", ip, port),
31            Host::Domain(domain) => format!("{}:{}", domain, port),
32        }
33    }
34}