use validators::prelude::*;
use validators_prelude::Host;
use crate::WhoIsError;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Validator)]
#[validator(host(port(Disallow)))]
pub struct Target(pub(crate) Host);
impl Target {
#[inline]
pub fn from_host(host: Host) -> Result<Target, WhoIsError> {
match host {
Host::Domain(domain) => Ok(Target::parse_string(domain)?),
host => Ok(Target(host)),
}
}
#[inline]
pub const fn host(&self) -> &Host {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_host_rejects_a_domain_which_is_not_one_line() {
assert!(
Target::from_host(Host::Domain(String::from("magiclen.org\r\nexample.com"))).is_err()
);
assert!(Target::from_host(Host::Domain(String::from("magiclen.org"))).is_ok());
}
}