1use validators::prelude::*;
2use validators_prelude::Host;
3
4use crate::WhoIsError;
5
6#[derive(Debug, Clone, PartialEq, Eq, Hash, Validator)]
8#[validator(host(port(Disallow)))]
9pub struct Target(pub(crate) Host);
10
11impl Target {
12 #[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 #[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 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}