use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::time::SystemTime;
#[derive(Debug, Clone, Default)]
pub struct WhoisResult {
pub creation_date: Option<DateTime<Utc>>,
pub expiration_date: Option<DateTime<Utc>>,
pub updated_date: Option<DateTime<Utc>>,
pub registrar: Option<String>,
pub registrant_country: Option<String>,
pub registrant_org: Option<String>,
pub status: Vec<String>,
pub nameservers: Vec<String>,
pub raw_text: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct WhoisCacheEntry {
pub(crate) result: WhoisCacheResult,
pub(crate) cached_at: SystemTime,
pub(crate) domain: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct WhoisCacheResult {
creation_date: Option<i64>,
expiration_date: Option<i64>,
updated_date: Option<i64>,
registrar: Option<String>,
registrant_country: Option<String>,
registrant_org: Option<String>,
status: Option<Vec<String>>,
nameservers: Option<Vec<String>>,
raw_text: Option<String>,
}
impl From<&WhoisResult> for WhoisCacheResult {
fn from(result: &WhoisResult) -> Self {
let vec_to_opt = |v: &Vec<String>| -> Option<Vec<String>> {
if v.is_empty() {
None
} else {
Some(v.clone())
}
};
WhoisCacheResult {
creation_date: result.creation_date.map(|dt| dt.timestamp_millis()),
expiration_date: result.expiration_date.map(|dt| dt.timestamp_millis()),
updated_date: result.updated_date.map(|dt| dt.timestamp_millis()),
registrar: result.registrar.clone(),
registrant_country: result.registrant_country.clone(),
registrant_org: result.registrant_org.clone(),
status: vec_to_opt(&result.status),
nameservers: vec_to_opt(&result.nameservers),
raw_text: result.raw_text.clone(),
}
}
}
impl From<WhoisCacheResult> for WhoisResult {
fn from(cache: WhoisCacheResult) -> Self {
#[allow(clippy::cast_possible_truncation)] let ms_to_dt = |ms: i64| -> Option<DateTime<Utc>> {
let nanos = (ms.rem_euclid(1000) * 1_000_000) as u32;
DateTime::from_timestamp(ms.div_euclid(1000), nanos)
};
WhoisResult {
creation_date: cache.creation_date.and_then(ms_to_dt),
expiration_date: cache.expiration_date.and_then(ms_to_dt),
updated_date: cache.updated_date.and_then(ms_to_dt),
registrar: cache.registrar,
registrant_country: cache.registrant_country,
registrant_org: cache.registrant_org,
status: cache.status.unwrap_or_default(),
nameservers: cache.nameservers.unwrap_or_default(),
raw_text: cache.raw_text,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_whois_cache_result_from_whois_result() {
let whois_result = WhoisResult {
creation_date: Some(chrono::Utc::now()),
expiration_date: Some(chrono::Utc::now() + chrono::Duration::days(365)),
updated_date: Some(chrono::Utc::now()),
registrar: Some("Test Registrar".to_string()),
registrant_country: Some("US".to_string()),
registrant_org: Some("Test Org".to_string()),
status: vec!["active".to_string()],
nameservers: vec!["ns1.example.com".to_string()],
raw_text: Some("Raw text".to_string()),
};
let cache_result: WhoisCacheResult = (&whois_result).into();
let converted_back: WhoisResult = cache_result.into();
assert!(converted_back.creation_date.is_some());
assert!(converted_back.expiration_date.is_some());
assert_eq!(converted_back.registrar, Some("Test Registrar".to_string()));
assert_eq!(converted_back.registrant_country, Some("US".to_string()));
assert_eq!(converted_back.status, vec!["active".to_string()]);
}
#[test]
fn test_whois_cache_result_from_whois_result_none_fields() {
let whois_result = WhoisResult::default();
let cache_result: WhoisCacheResult = (&whois_result).into();
let converted_back: WhoisResult = cache_result.into();
assert!(converted_back.creation_date.is_none());
assert!(converted_back.registrar.is_none());
}
#[test]
fn test_whois_cache_json_compat_old_null_status_round_trips() {
let raw = r#"{
"creation_date": null,
"expiration_date": null,
"updated_date": null,
"registrar": "Some Registrar",
"registrant_country": null,
"registrant_org": null,
"status": null,
"nameservers": ["ns1.example.com", "ns2.example.com"],
"raw_text": null
}"#;
let cache: WhoisCacheResult =
serde_json::from_str(raw).expect("legacy cache JSON must still deserialize");
let result: WhoisResult = cache.into();
assert_eq!(result.registrar.as_deref(), Some("Some Registrar"));
assert!(result.status.is_empty(), "null on disk -> empty Vec");
assert_eq!(
result.nameservers,
vec!["ns1.example.com".to_string(), "ns2.example.com".to_string()]
);
}
#[test]
fn test_whois_cache_json_compat_empty_vec_serialises_as_null() {
let result = WhoisResult {
registrar: Some("R".to_string()),
status: vec![], nameservers: vec!["ns1.example.com".to_string()],
..WhoisResult::default()
};
let cache: WhoisCacheResult = (&result).into();
let json: serde_json::Value = serde_json::to_value(&cache).expect("cache must serialise");
assert_eq!(
json["status"],
serde_json::Value::Null,
"empty in-memory Vec must persist as JSON null (preserves absent-vs-empty distinction)"
);
assert_eq!(
json["nameservers"],
serde_json::json!(["ns1.example.com"]),
"non-empty Vec must persist as a JSON array"
);
}
#[test]
fn test_whois_cache_result_unrepresentable_timestamp_is_none() {
let cache = WhoisCacheResult {
creation_date: Some(i64::MAX),
expiration_date: Some(i64::MIN),
updated_date: Some(0),
registrar: None,
registrant_country: None,
registrant_org: None,
status: None,
nameservers: None,
raw_text: None,
};
let result: WhoisResult = cache.into();
assert!(
result.creation_date.is_none(),
"i64::MAX ms must round-trip to None, not 1970-01-01"
);
assert!(
result.expiration_date.is_none(),
"i64::MIN ms must round-trip to None, not 1970-01-01"
);
assert!(
result.updated_date.is_some(),
"0 ms is a valid timestamp and must round-trip to Some(epoch)"
);
}
#[test]
fn test_whois_cache_result_round_trip() {
let original = WhoisResult {
creation_date: Some(chrono::Utc::now()),
expiration_date: Some(chrono::Utc::now() + chrono::Duration::days(365)),
updated_date: Some(chrono::Utc::now()),
registrar: Some("Test Registrar".to_string()),
registrant_country: Some("US".to_string()),
registrant_org: Some("Test Org".to_string()),
status: vec!["active".to_string()],
nameservers: vec!["ns1.example.com".to_string()],
raw_text: Some("Raw text".to_string()),
};
let cache_result: WhoisCacheResult = (&original).into();
let converted: WhoisResult = cache_result.into();
assert_eq!(converted.registrar, original.registrar);
assert_eq!(converted.registrant_country, original.registrant_country);
assert_eq!(converted.registrant_org, original.registrant_org);
assert_eq!(converted.status, original.status);
assert_eq!(converted.nameservers, original.nameservers);
assert_eq!(converted.raw_text, original.raw_text);
assert!(converted.creation_date.is_some());
assert!(converted.expiration_date.is_some());
assert!(converted.updated_date.is_some());
}
}