use idna;
use url::Url;
use crate::common::{BASE_WHOIS_URL, Config, execute_query};
use crate::errors::Ip2LocationError;
use crate::models::WhoisResult;
pub struct Whois<'a> {
pub(crate) config: &'a Config,
}
impl<'a> Whois<'a> {
pub async fn lookup(&self, domain: &str) -> Result<WhoisResult, Ip2LocationError> {
if domain.trim().is_empty() {
return Err(Ip2LocationError::InvalidInput("Domain cannot be empty.".into()));
}
let params = vec![("domain", domain)];
execute_query(BASE_WHOIS_URL, self.config, ¶ms).await
}
}
pub fn get_punycode(domain: &str) -> Result<String, String> {
idna::domain_to_ascii(domain).map_err(|e| format!("IDNA encoding error: {:?}", e))
}
pub fn get_normaltext(domain: &str) -> Result<String, String> {
let (unicode, res) = idna::domain_to_unicode(domain);
match res {
Ok(()) => Ok(unicode),
Err(e) => Err(format!("IDNA decoding error: {:?}", e)),
}
}
pub fn get_domain_name(url_str: &str) -> Option<String> {
if url_str.is_empty() {
return None;
}
let candidate = if url_str.starts_with("http") {
url_str.to_owned()
} else {
format!("https://{}", url_str)
};
if let Ok(parsed_url) = Url::parse(&candidate) {
parsed_url.host_str().map(|host| {
host.strip_prefix("www.").unwrap_or(host).to_string()
})
} else {
None
}
}
pub fn get_domain_extension(url_str: &str) -> Option<String> {
get_domain_name(url_str).and_then(|domain| domain.find('.').map(|i| domain[i..].to_string()))
}