Skip to main content

whois_rust/
target.rs

1use validators::prelude::*;
2use validators_prelude::Host;
3
4use crate::WhoIsError;
5
6/// The target of a lookup: a domain or an IP address, without a port.
7#[derive(Debug, Clone, PartialEq, Eq, Hash, Validator)]
8#[validator(host(port(Disallow)))]
9pub struct Target(pub(crate) Host);
10
11impl Target {
12    /// Create a `Target` from a `Host`. A `Host` can be built by hand, so a domain is validated again, which also turns a Unicode domain into its ASCII (Punycode) form.
13    #[inline]
14    pub fn from_host(host: Host) -> Result<Target, WhoIsError> {
15        match host {
16            Host::Domain(domain) => Ok(Target::parse_string(domain)?),
17            host => Ok(Target(host)),
18        }
19    }
20
21    /// Get the host of this target.
22    #[inline]
23    pub const fn host(&self) -> &Host {
24        &self.0
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn from_host_rejects_a_domain_which_is_not_one_line() {
34        // A second line would be a second request, which RFC 3912 does not allow.
35        assert!(
36            Target::from_host(Host::Domain(String::from("magiclen.org\r\nexample.com"))).is_err()
37        );
38
39        assert!(Target::from_host(Host::Domain(String::from("magiclen.org"))).is_ok());
40    }
41}