use serde::{Deserialize, Serialize};
use crate::rdap::RdapClient;
use crate::whois::{get_registry_url, get_whois_server};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TldInfo {
pub tld: String,
pub whois_server: Option<String>,
pub rdap_url: Option<String>,
pub registry_url: Option<String>,
pub tld_type: String,
}
pub async fn lookup_tld(tld: &str) -> TldInfo {
let tld = tld.trim_start_matches('.').to_lowercase();
let whois_server = get_whois_server(&tld).map(|s| s.to_string());
let registry_url = get_registry_url(&tld);
let tld_type = classify_tld(&tld);
let rdap_client = RdapClient::new();
let rdap_url = rdap_client.get_rdap_base_url_for_tld(&tld).await;
TldInfo {
tld,
whois_server,
rdap_url,
registry_url,
tld_type,
}
}
fn classify_tld(tld: &str) -> String {
if tld.len() == 2 && tld.chars().all(|c| c.is_ascii_alphabetic()) {
"country-code".to_string()
} else if ["arpa", "root"].contains(&tld) {
"infrastructure".to_string()
} else if [
"aero", "asia", "cat", "coop", "edu", "gov", "int", "jobs", "mil", "museum", "post", "tel",
"travel", "xxx",
]
.contains(&tld)
{
"sponsored".to_string()
} else {
"generic".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_classify_tld_country_code() {
assert_eq!(classify_tld("uk"), "country-code");
assert_eq!(classify_tld("de"), "country-code");
assert_eq!(classify_tld("jp"), "country-code");
}
#[test]
fn test_classify_tld_infrastructure() {
assert_eq!(classify_tld("arpa"), "infrastructure");
assert_eq!(classify_tld("root"), "infrastructure");
}
#[test]
fn test_classify_tld_sponsored() {
assert_eq!(classify_tld("edu"), "sponsored");
assert_eq!(classify_tld("gov"), "sponsored");
assert_eq!(classify_tld("museum"), "sponsored");
}
#[test]
fn test_classify_tld_generic() {
assert_eq!(classify_tld("com"), "generic");
assert_eq!(classify_tld("net"), "generic");
assert_eq!(classify_tld("org"), "generic");
assert_eq!(classify_tld("app"), "generic");
}
#[test]
fn test_classify_tld_numeric_not_country_code() {
assert_eq!(classify_tld("a1"), "generic");
}
}