| 1 |
//! Filter module for extracting fields from RDAP responses. |
| 2 |
//! |
| 3 |
//! This module provides a way to extract selected fields from any RDAP response |
| 4 |
//! into a typed array of name/value pairs suitable for CSV serialization. |
| 5 |
//! |
| 6 |
//! # Architecture |
| 7 |
//! |
| 8 |
//! The filter system works through three key abstractions: |
| 9 |
//! |
| 10 |
//! 1. [`Filter`] — An enum listing all extractable fields across all RDAP object types. |
| 11 |
//! 2. [`Filterable`] — A trait implemented by all RDAP response types that allows |
| 12 |
//! extracting a subset of [`Filter`] fields into [`FilterOutput`] values. |
| 13 |
//! 3. [`extract`] — A convenience function that works with any type implementing [`Filterable`]. |
| 14 |
//! |
| 15 |
//! # Filterable RDAP Object Types |
| 16 |
//! |
| 17 |
//! The following RDAP response types implement [`Filterable`]: |
| 18 |
//! |
| 19 |
//! | Object Type | Key Filters | |
| 20 |
//! |---|---| |
| 21 |
//! | [`crate::response::Domain`] | `ldh_name`, `unicode_name`, `handle`, `status`, `nameserver`, `nameserver_ip_address`, `public_id`, entity roles | |
| 22 |
//! | [`crate::response::DomainSearchResults`] | Same as Domain, aggregated across search results | |
| 23 |
//! | [`crate::response::Autnum`] | `start_autnum`, `end_autnum`, `handle`, `status`, `name`, `type`, entity roles | |
| 24 |
//! | [`crate::response::AutnumSearchResults`] | Same as Autnum, aggregated across search results | |
| 25 |
//! | [`crate::response::Entity`] | `role`, `email`, `full_name`, `voice`, `fax`, `contact_uri`, `country_name`, `country_code`, `public_id` | |
| 26 |
//! | [`crate::response::EntitySearchResults`] | Same as Entity, aggregated across search results | |
| 27 |
//! | [`crate::response::Nameserver`] | `ip_address`, `handle`, `status`, entity roles | |
| 28 |
//! | [`crate::response::NameserverSearchResults`] | Same as Nameserver, aggregated across search results | |
| 29 |
//! | [`crate::response::Network`] | `start_ip_address`, `end_ip_address`, `ip_version`, `cidr`, `handle`, `parent_handle`, entity roles | |
| 30 |
//! | [`crate::response::IpSearchResults`] | Same as Network, aggregated across search results | |
| 31 |
//! | [`crate::response::RdapResponse`] | Delegates to the inner object type's filter implementation | |
| 32 |
//! |
| 33 |
//! # Entity Role Filters |
| 34 |
//! |
| 35 |
//! Several filters extract contact information from nested entities by role. |
| 36 |
//! These work recursively through the entity hierarchy: |
| 37 |
//! |
| 38 |
//! - `registrant_*` — Extracts fields from entities with role `registrant` |
| 39 |
//! - `abuse_*` — Extracts fields from entities with role `abuse` |
| 40 |
//! - `technical_*` — Extracts fields from entities with role `technical` |
| 41 |
//! - `registrar_*` — Extracts fields from entities with role `registrar` |
| 42 |
//! |
| 43 |
//! For search results, role filters aggregate values across all search result entries. |
| 44 |
//! |
| 45 |
//! # FilterValue Types |
| 46 |
//! |
| 47 |
//! Each filter extracts a value of one of the following types: |
| 48 |
//! |
| 49 |
//! | Variant | Description | Example | |
| 50 |
//! |---|---|---| |
| 51 |
//! | `StringVal` | Single string | `"example.com"` | |
| 52 |
//! | `StringArray` | Multiple strings | `["active", "clientTransferProhibited"]` | |
| 53 |
//! | `IntVal` | Single integer | `12345` | |
| 54 |
//! | `IntArray` | Multiple integers | `[12345, 12350]` | |
| 55 |
//! | `BoolVal` | Boolean | `true` | |
| 56 |
//! | `HashMapVal` | Key-value pairs | `{"IANA Registrar ID": "1234"}` | |
| 57 |
//! | `Null` | Missing/absent value | — | |
| 58 |
//! |
| 59 |
//! # Example |
| 60 |
//! |
| 61 |
//! ```rust |
| 62 |
//! use icann_rdap_common::prelude::*; |
| 63 |
//! use icann_rdap_common::filter::*; |
| 64 |
//! |
| 65 |
//! // Build a domain object |
| 66 |
//! let domain = Domain::builder() |
| 67 |
//! .ldh_name("example.com") |
| 68 |
//! .handle("EXAMPLE-DOM") |
| 69 |
//! .statuses(vec!["active".to_string(), "client transfer prohibited".to_string()]) |
| 70 |
//! .build(); |
| 71 |
//! |
| 72 |
//! // Define which fields to extract |
| 73 |
//! let filters = vec![ |
| 74 |
//! Filter::LdhName, |
| 75 |
//! Filter::Handle, |
| 76 |
//! Filter::Status, |
| 77 |
//! ]; |
| 78 |
//! |
| 79 |
//! // Extract the selected fields |
| 80 |
//! let results = extract(&domain, &filters); |
| 81 |
//! |
| 82 |
//! // Verify the extracted values |
| 83 |
//! assert_eq!(results.len(), 3); |
| 84 |
//! assert_eq!(results[0].value, FilterValue::StringVal("example.com".to_string())); |
| 85 |
//! assert_eq!(results[1].value, FilterValue::StringVal("EXAMPLE-DOM".to_string())); |
| 86 |
//! match &results[2].value { |
| 87 |
//! FilterValue::StringArray(statuses) => { |
| 88 |
//! assert_eq!(statuses.len(), 2); |
| 89 |
//! } |
| 90 |
//! _ => panic!("Expected StringArray for Status"), |
| 91 |
//! } |
| 92 |
//! ``` |
| 93 |
//! |
| 94 |
//! # Example: Entity Role Filters |
| 95 |
//! |
| 96 |
//! ```rust |
| 97 |
//! use icann_rdap_common::prelude::*; |
| 98 |
//! use icann_rdap_common::filter::*; |
| 99 |
//! |
| 100 |
//! let registrant_email = Email::builder().email("[email protected]").build(); |
| 101 |
//! let registrant_contact = Contact::builder() |
| 102 |
//! .full_name("Admin User") |
| 103 |
//! .emails(vec![registrant_email]) |
| 104 |
//! .build(); |
| 105 |
//! |
| 106 |
//! let domain = Domain::response_obj() |
| 107 |
//! .ldh_name("example.com") |
| 108 |
//! .entity( |
| 109 |
//! Entity::response_obj() |
| 110 |
//! .handle("REG-HANDLE") |
| 111 |
//! .role("registrant") |
| 112 |
//! .contact(registrant_contact) |
| 113 |
//! .build(), |
| 114 |
//! ) |
| 115 |
//! .build(); |
| 116 |
//! |
| 117 |
//! let filters = vec![ |
| 118 |
//! Filter::RegistrantEmail, |
| 119 |
//! Filter::RegistrantFullName, |
| 120 |
//! ]; |
| 121 |
//! |
| 122 |
//! let results = extract(&domain, &filters); |
| 123 |
//! |
| 124 |
//! assert_eq!(results[0].value, FilterValue::StringVal("[email protected]".to_string())); |
| 125 |
//! assert_eq!(results[1].value, FilterValue::StringVal("Admin User".to_string())); |
| 126 |
//! ``` |
| 127 |
//! |
| 128 |
//! # Example: Search Results |
| 129 |
//! |
| 130 |
//! ```rust |
| 131 |
//! use icann_rdap_common::prelude::*; |
| 132 |
//! use icann_rdap_common::filter::*; |
| 133 |
//! |
| 134 |
//! let domain1 = Domain::response_obj() |
| 135 |
//! .ldh_name("example1.com") |
| 136 |
//! .handle("EXAMPLE1-DOM") |
| 137 |
//! .build(); |
| 138 |
//! |
| 139 |
//! let domain2 = Domain::response_obj() |
| 140 |
//! .ldh_name("example2.com") |
| 141 |
//! .handle("EXAMPLE2-DOM") |
| 142 |
//! .build(); |
| 143 |
//! |
| 144 |
//! let search_results = DomainSearchResults::response_obj() |
| 145 |
//! .results(vec![domain1, domain2]) |
| 146 |
//! .build(); |
| 147 |
//! |
| 148 |
//! let filters = vec![Filter::LdhName, Filter::Handle]; |
| 149 |
//! let results = extract(&search_results, &filters); |
| 150 |
//! |
| 151 |
//! assert_eq!(results.len(), 2); |
| 152 |
//! match &results[0].value { |
| 153 |
//! FilterValue::StringArray(names) => { |
| 154 |
//! assert_eq!(names.len(), 2); |
| 155 |
//! assert!(names.contains(&"example1.com".to_string())); |
| 156 |
//! assert!(names.contains(&"example2.com".to_string())); |
| 157 |
//! } |
| 158 |
//! _ => panic!("Expected StringArray"), |
| 159 |
//! } |
| 160 |
//! ``` |
| 161 |
|
| 162 |
pub mod autnum; |
| 163 |
pub mod autnum_search_results; |
| 164 |
pub mod domain; |
| 165 |
pub mod domain_search_results; |
| 166 |
pub mod entity; |
| 167 |
pub mod entity_search_results; |
| 168 |
pub mod ip_search_results; |
| 169 |
pub mod nameserver; |
| 170 |
pub mod nameserver_search_results; |
| 171 |
pub mod network; |
| 172 |
|
| 173 |
use std::{ |
| 174 |
collections::{HashMap, VecDeque}, |
| 175 |
sync::LazyLock, |
| 176 |
}; |
| 177 |
|
| 178 |
use serde::{Deserialize, Serialize}; |
| 179 |
use strum::{Display, EnumString}; |
| 180 |
|
| 181 |
use crate::response::{Entity, EntityRole, ObjectCommonFields}; |
| 182 |
|
| 183 |
/// Represents a filterable field on an RDAP response. |
| 184 |
/// |
| 185 |
/// Each variant corresponds to a specific field that can be extracted from |
| 186 |
/// RDAP objects. The filter system maps each variant to the appropriate |
| 187 |
/// extraction logic based on the RDAP object type. |
| 188 |
/// |
| 189 |
/// # Object Class Common Fields |
| 190 |
/// |
| 191 |
/// These fields are available on all RDAP object types: |
| 192 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Display, EnumString)] |
| 193 |
#[strum(serialize_all = "snake_case")] |
| 194 |
pub enum Filter { |
| 195 |
// Object class common (all types) |
| 196 |
/// The object's handle (e.g., `"EXAMPLE-DOM"`, `"NET-192-0-2-0-1"`). |
| 197 |
Handle, |
| 198 |
/// The object's status list (e.g., `["active", "client transfer prohibited"]`). |
| 199 |
Status, |
| 200 |
/// The object class name (e.g., `"domain"`, `"autnum"`, `"entity"`, `"network"`, `"nameserver"`). |
| 201 |
ObjectClassName, |
| 202 |
/// Event data as key-value pairs of action → date (e.g., `{"registration": "2020-01-01T00:00:00Z"}`). |
| 203 |
Event, |
| 204 |
/// RDAP conformance URIs (e.g., `["rdap_level_0", "icann_rdap_response_profile_1"]`). |
| 205 |
RdapConformance, |
| 206 |
|
| 207 |
// Domain-specific |
| 208 |
/// The LDH (ASCII) domain name (e.g., `"example.com"`). |
| 209 |
LdhName, |
| 210 |
/// The Unicode (internationalized) domain name (e.g., `"例え.com"`). |
| 211 |
UnicodeName, |
| 212 |
/// The list of nameserver domain names associated with this domain. |
| 213 |
Nameserver, |
| 214 |
/// IP addresses from all nameservers associated with this domain as a map of |
| 215 |
/// nameserver ldh_name → IP address list (both IPv4 and IPv6 combined). |
| 216 |
NameserverIpAddress, |
| 217 |
/// Public identifiers as key-value pairs (e.g., `{"IANA Registrar ID": "1234"}`). |
| 218 |
PublicId, |
| 219 |
|
| 220 |
// Nameservers |
| 221 |
/// IP addresses associated with nameservers (both IPv4 and IPv6 combined). |
| 222 |
IpAddress, |
| 223 |
|
| 224 |
// Entity-specific |
| 225 |
/// The entity's role(s) (e.g., `["registrant", "admin"]`). |
| 226 |
Role, |
| 227 |
/// Email addresses associated with the entity's contact information. |
| 228 |
Email, |
| 229 |
/// The full name from the entity's contact information. |
| 230 |
FullName, |
| 231 |
/// The voice/telephone number from the entity's contact information. |
| 232 |
Voice, |
| 233 |
/// The fax number from the entity's contact information. |
| 234 |
Fax, |
| 235 |
/// Contact URIs (URLs) from the entity's contact information. |
| 236 |
ContactUri, |
| 237 |
/// Country name(s) from the entity's postal address(es). |
| 238 |
CountryName, |
| 239 |
/// Country code(s) from the entity's postal address(es). |
| 240 |
CountryCode, |
| 241 |
|
| 242 |
// Autnum-specific |
| 243 |
/// The starting autonomous system number (e.g., `12345`). |
| 244 |
StartAutnum, |
| 245 |
/// The ending autonomous system number (e.g., `12350`). |
| 246 |
EndAutnum, |
| 247 |
|
| 248 |
// Network-specific |
| 249 |
/// The start IP address of the network range (e.g., `"192.0.2.0"`). |
| 250 |
StartIpAddress, |
| 251 |
/// The end IP address of the network range (e.g., `"192.0.2.255"`). |
| 252 |
EndIpAddress, |
| 253 |
/// The IP version (`"v4"` or `"v6"`). |
| 254 |
IpVersion, |
| 255 |
/// CIDR notation entries (e.g., `"192.0.2.0/24"`). |
| 256 |
Cidr, |
| 257 |
|
| 258 |
// Generic |
| 259 |
/// A generic name field (used by Autnum, Network). |
| 260 |
Name, |
| 261 |
/// A generic type field (e.g., autnum type, network type). |
| 262 |
Type, |
| 263 |
/// The parent object's handle (used by Network). |
| 264 |
ParentHandle, |
| 265 |
|
| 266 |
/// Registrant email address (extracted from nested entities with role `registrant`). |
| 267 |
RegistrantEmail, |
| 268 |
/// Registrant full name (extracted from nested entities with role `registrant`). |
| 269 |
RegistrantFullName, |
| 270 |
/// Registrant voice/phone (extracted from nested entities with role `registrant`). |
| 271 |
RegistrantVoice, |
| 272 |
/// Registrant fax (extracted from nested entities with role `registrant`). |
| 273 |
RegistrantFax, |
| 274 |
/// Registrant contact URIs (extracted from nested entities with role `registrant`). |
| 275 |
RegistrantContactUri, |
| 276 |
/// Registrant country name(s) (extracted from nested entities with role `registrant`). |
| 277 |
RegistrantCountryName, |
| 278 |
/// Registrant country code(s) (extracted from nested entities with role `registrant`). |
| 279 |
RegistrantCountryCode, |
| 280 |
/// Registrant public identifiers as key-value pairs (extracted from nested entities with role `registrant`). |
| 281 |
RegistrantPublicId, |
| 282 |
/// Registrant handle (extracted from nested entities with role `registrant`). |
| 283 |
RegistrantHandle, |
| 284 |
|
| 285 |
/// Abuse contact email (extracted from nested entities with role `abuse`). |
| 286 |
AbuseEmail, |
| 287 |
/// Abuse contact full name (extracted from nested entities with role `abuse`). |
| 288 |
AbuseFullName, |
| 289 |
/// Abuse contact voice/phone (extracted from nested entities with role `abuse`). |
| 290 |
AbuseVoice, |
| 291 |
/// Abuse contact fax (extracted from nested entities with role `abuse`). |
| 292 |
AbuseFax, |
| 293 |
/// Abuse contact URIs (extracted from nested entities with role `abuse`). |
| 294 |
AbuseContactUri, |
| 295 |
/// Abuse contact country name(s) (extracted from nested entities with role `abuse`). |
| 296 |
AbuseCountryName, |
| 297 |
/// Abuse contact country code(s) (extracted from nested entities with role `abuse`). |
| 298 |
AbuseCountryCode, |
| 299 |
/// Abuse contact public identifiers as key-value pairs (extracted from nested entities with role `abuse`). |
| 300 |
AbusePublicId, |
| 301 |
/// Abuse contact handle (extracted from nested entities with role `abuse`). |
| 302 |
AbuseHandle, |
| 303 |
|
| 304 |
/// Technical contact email (extracted from nested entities with role `technical`). |
| 305 |
TechnicalEmail, |
| 306 |
/// Technical contact full name (extracted from nested entities with role `technical`). |
| 307 |
TechnicalFullName, |
| 308 |
/// Technical contact voice/phone (extracted from nested entities with role `technical`). |
| 309 |
TechnicalVoice, |
| 310 |
/// Technical contact fax (extracted from nested entities with role `technical`). |
| 311 |
TechnicalFax, |
| 312 |
/// Technical contact URIs (extracted from nested entities with role `technical`). |
| 313 |
TechnicalContactUri, |
| 314 |
/// Technical contact country name(s) (extracted from nested entities with role `technical`). |
| 315 |
TechnicalCountryName, |
| 316 |
/// Technical contact country code(s) (extracted from nested entities with role `technical`). |
| 317 |
TechnicalCountryCode, |
| 318 |
/// Technical contact public identifiers as key-value pairs (extracted from nested entities with role `technical`). |
| 319 |
TechnicalPublicId, |
| 320 |
/// Technical contact handle (extracted from nested entities with role `technical`). |
| 321 |
TechnicalHandle, |
| 322 |
|
| 323 |
/// Registrar email (extracted from nested entities with role `registrar`). |
| 324 |
RegistrarEmail, |
| 325 |
/// Registrar full name (extracted from nested entities with role `registrar`). |
| 326 |
RegistrarFullName, |
| 327 |
/// Registrar voice/phone (extracted from nested entities with role `registrar`). |
| 328 |
RegistrarVoice, |
| 329 |
/// Registrar fax (extracted from nested entities with role `registrar`). |
| 330 |
RegistrarFax, |
| 331 |
/// Registrar contact URIs (extracted from nested entities with role `registrar`). |
| 332 |
RegistrarContactUri, |
| 333 |
/// Registrar country name(s) (extracted from nested entities with role `registrar`). |
| 334 |
RegistrarCountryName, |
| 335 |
/// Registrar country code(s) (extracted from nested entities with role `registrar`). |
| 336 |
RegistrarCountryCode, |
| 337 |
/// Registrar public identifiers as key-value pairs (extracted from nested entities with role `registrar`). |
| 338 |
RegistrarPublicId, |
| 339 |
/// Registrar handle (extracted from nested entities with role `registrar`). |
| 340 |
RegistrarHandle, |
| 341 |
} |
| 342 |
|
| 343 |
/// The extracted value from a filter operation. |
| 344 |
/// |
| 345 |
/// Each variant wraps a different Rust type that corresponds to the kind of data |
| 346 |
/// extracted by a [`Filter`]. The `#[serde(untagged)]` attribute enables flexible |
| 347 |
/// serialization/deserialization. |
| 348 |
/// |
| 349 |
/// # Examples |
| 350 |
/// |
| 351 |
/// ```rust |
| 352 |
/// use icann_rdap_common::filter::{Filter, FilterValue}; |
| 353 |
/// |
| 354 |
/// // Single string value |
| 355 |
/// let val = FilterValue::StringVal("example.com".to_string()); |
| 356 |
/// assert_eq!(val.string_value(), Some("example.com")); |
| 357 |
/// |
| 358 |
/// // Array of strings |
| 359 |
/// let val = FilterValue::StringArray(vec!["active".to_string(), "clientTransferProhibited".to_string()]); |
| 360 |
/// assert_eq!(val.string_values().len(), 2); |
| 361 |
/// |
| 362 |
/// // Integer value |
| 363 |
/// let val = FilterValue::IntVal(12345); |
| 364 |
/// assert_eq!(val.int_value(), Some(12345)); |
| 365 |
/// |
| 366 |
/// // Key-value pairs |
| 367 |
/// let mut map = std::collections::HashMap::new(); |
| 368 |
/// map.insert("IANA Registrar ID".to_string(), FilterValue::StringVal("1234".to_string())); |
| 369 |
/// let val = FilterValue::HashMapVal(map); |
| 370 |
/// assert!(val.hash_map().contains_key("IANA Registrar ID")); |
| 371 |
/// |
| 372 |
/// // Null/missing value |
| 373 |
/// let val = FilterValue::Null; |
| 374 |
/// assert!(val.is_null()); |
| 375 |
/// ``` |
| 376 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 377 |
#[serde(untagged)] |
| 378 |
pub enum FilterValue { |
| 379 |
/// A single string value. |
| 380 |
StringVal(String), |
| 381 |
/// An array of string values. |
| 382 |
StringArray(Vec<String>), |
| 383 |
/// A map of string keys to `FilterValue` values. |
| 384 |
HashMapVal(HashMap<String, FilterValue>), |
| 385 |
/// A single 64-bit integer value. |
| 386 |
IntVal(i64), |
| 387 |
/// An array of 64-bit integer values. |
| 388 |
IntArray(Vec<i64>), |
| 389 |
/// A boolean value. |
| 390 |
BoolVal(bool), |
| 391 |
/// A null/missing value (used when a filter has no data to extract). |
| 392 |
Null, |
| 393 |
} |
| 394 |
|
| 395 |
static EMPTY_HASHMAP: LazyLock<HashMap<String, FilterValue>> = LazyLock::new(HashMap::new); |
| 396 |
|
| 397 |
impl FilterValue { |
| 398 |
/// Returns a reference to the inner string if this is `StringVal`, otherwise `None`. |
| 399 |
/// |
| 400 |
/// # Example |
| 401 |
/// |
| 402 |
/// ```rust |
| 403 |
/// use icann_rdap_common::filter::FilterValue; |
| 404 |
/// |
| 405 |
/// let val = FilterValue::StringVal("hello".to_string()); |
| 406 |
/// assert_eq!(val.string_value(), Some("hello")); |
| 407 |
/// |
| 408 |
/// let val = FilterValue::Null; |
| 409 |
/// assert_eq!(val.string_value(), None); |
| 410 |
/// ``` |
| 411 |
pub fn string_value(&self) -> Option<&str> { |
| 412 |
match self { |
| 413 |
FilterValue::StringVal(s) => Some(s), |
| 414 |
_ => None, |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
/// Consumes `self` and returns the owned string if this is `StringVal`, otherwise `None`. |
| 419 |
/// |
| 420 |
/// # Example |
| 421 |
/// |
| 422 |
/// ```rust |
| 423 |
/// use icann_rdap_common::filter::FilterValue; |
| 424 |
/// |
| 425 |
/// let val = FilterValue::StringVal("hello".to_string()); |
| 426 |
/// assert_eq!(val.into_string_value(), Some("hello".to_string())); |
| 427 |
/// ``` |
| 428 |
pub fn into_string_value(self) -> Option<String> { |
| 429 |
match self { |
| 430 |
FilterValue::StringVal(s) => Some(s), |
| 431 |
_ => None, |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
/// Returns a reference to the inner string array if this is `StringArray`, |
| 436 |
/// otherwise an empty slice. |
| 437 |
/// |
| 438 |
/// # Example |
| 439 |
/// |
| 440 |
/// ```rust |
| 441 |
/// use icann_rdap_common::filter::FilterValue; |
| 442 |
/// |
| 443 |
/// let val = FilterValue::StringArray(vec!["a".to_string(), "b".to_string()]); |
| 444 |
/// assert_eq!(val.string_values().len(), 2); |
| 445 |
/// |
| 446 |
/// let val = FilterValue::Null; |
| 447 |
/// assert!(val.string_values().is_empty()); |
| 448 |
/// ``` |
| 449 |
pub fn string_values(&self) -> &[String] { |
| 450 |
match self { |
| 451 |
FilterValue::StringArray(v) => v, |
| 452 |
_ => &[], |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/// Consumes `self` and returns the owned string array if this is `StringArray`, |
| 457 |
/// otherwise an empty `Vec`. |
| 458 |
pub fn into_string_values(self) -> Vec<String> { |
| 459 |
match self { |
| 460 |
FilterValue::StringArray(v) => v, |
| 461 |
_ => Vec::new(), |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
/// Returns the integer value if this is `IntVal`, otherwise `None`. |
| 466 |
/// |
| 467 |
/// # Example |
| 468 |
/// |
| 469 |
/// ```rust |
| 470 |
/// use icann_rdap_common::filter::FilterValue; |
| 471 |
/// |
| 472 |
/// let val = FilterValue::IntVal(42); |
| 473 |
/// assert_eq!(val.int_value(), Some(42)); |
| 474 |
/// |
| 475 |
/// let val = FilterValue::Null; |
| 476 |
/// assert_eq!(val.int_value(), None); |
| 477 |
/// ``` |
| 478 |
pub fn int_value(&self) -> Option<i64> { |
| 479 |
match self { |
| 480 |
FilterValue::IntVal(v) => Some(*v), |
| 481 |
_ => None, |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
/// Returns a reference to the inner integer array if this is `IntArray`, |
| 486 |
/// otherwise an empty slice. |
| 487 |
pub fn int_values(&self) -> &[i64] { |
| 488 |
match self { |
| 489 |
FilterValue::IntArray(v) => v, |
| 490 |
_ => &[], |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
/// Consumes `self` and returns the owned integer array if this is `IntArray`, |
| 495 |
/// otherwise an empty `Vec`. |
| 496 |
pub fn into_int_values(self) -> Vec<i64> { |
| 497 |
match self { |
| 498 |
FilterValue::IntArray(v) => v, |
| 499 |
_ => Vec::new(), |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
/// Returns the boolean value if this is `BoolVal`, otherwise `None`. |
| 504 |
pub fn bool_value(&self) -> Option<bool> { |
| 505 |
match self { |
| 506 |
FilterValue::BoolVal(v) => Some(*v), |
| 507 |
_ => None, |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
/// Returns a reference to the inner `HashMap` if this is `HashMapVal`, |
| 512 |
/// otherwise an empty static map. |
| 513 |
/// |
| 514 |
/// # Example |
| 515 |
/// |
| 516 |
/// ```rust |
| 517 |
/// use icann_rdap_common::filter::FilterValue; |
| 518 |
/// |
| 519 |
/// let val = FilterValue::Null; |
| 520 |
/// assert!(val.hash_map().is_empty()); |
| 521 |
/// ``` |
| 522 |
pub fn hash_map(&self) -> &HashMap<String, FilterValue> { |
| 523 |
match self { |
| 524 |
FilterValue::HashMapVal(m) => m, |
| 525 |
_ => &EMPTY_HASHMAP, |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
/// Consumes `self` and returns the owned `HashMap` if this is `HashMapVal`, |
| 530 |
/// otherwise an empty map. |
| 531 |
pub fn into_hash_map(self) -> HashMap<String, FilterValue> { |
| 532 |
match self { |
| 533 |
FilterValue::HashMapVal(m) => m, |
| 534 |
_ => HashMap::new(), |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
/// Returns `true` if this is `Null`. |
| 539 |
pub fn is_null(&self) -> bool { |
| 540 |
matches!(self, FilterValue::Null) |
| 541 |
} |
| 542 |
|
| 543 |
/// Returns `true` if this is `Null` or an empty collection. |
| 544 |
/// |
| 545 |
/// # Example |
| 546 |
/// |
| 547 |
/// ```rust |
| 548 |
/// use icann_rdap_common::filter::FilterValue; |
| 549 |
/// |
| 550 |
/// assert!(FilterValue::Null.is_empty()); |
| 551 |
/// assert!(FilterValue::StringArray(vec![]).is_empty()); |
| 552 |
/// assert!(!FilterValue::StringVal("hello".to_string()).is_empty()); |
| 553 |
/// ``` |
| 554 |
pub fn is_empty(&self) -> bool { |
| 555 |
match self { |
| 556 |
FilterValue::Null => true, |
| 557 |
FilterValue::StringArray(v) => v.is_empty(), |
| 558 |
FilterValue::IntArray(v) => v.is_empty(), |
| 559 |
FilterValue::HashMapVal(m) => m.is_empty(), |
| 560 |
_ => false, |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
/// Converts this value to a display-friendly string, suitable for CSV or console output. |
| 565 |
/// |
| 566 |
/// # Formatting Rules |
| 567 |
/// |
| 568 |
/// | Variant | Format | |
| 569 |
/// |---|---| |
| 570 |
/// | `StringVal` | The string itself | |
| 571 |
/// | `StringArray` | Values joined by `\|` | |
| 572 |
/// | `HashMapVal` | `key=value` pairs joined by `\|` | |
| 573 |
/// | `IntVal` | Integer as string | |
| 574 |
/// | `IntArray` | Values joined by `\|` | |
| 575 |
/// | `BoolVal` | `"true"` or `"false"` | |
| 576 |
/// | `Null` | Empty string | |
| 577 |
/// |
| 578 |
/// # Example |
| 579 |
/// |
| 580 |
/// ```rust |
| 581 |
/// use icann_rdap_common::filter::FilterValue; |
| 582 |
/// |
| 583 |
/// let val = FilterValue::StringArray(vec!["a".to_string(), "b".to_string()]); |
| 584 |
/// assert_eq!(val.to_display_string(), "a|b"); |
| 585 |
/// |
| 586 |
/// let val = FilterValue::Null; |
| 587 |
/// assert_eq!(val.to_display_string(), ""); |
| 588 |
/// ``` |
| 589 |
pub fn to_display_string(&self) -> String { |
| 590 |
match self { |
| 591 |
FilterValue::StringVal(s) => s.clone(), |
| 592 |
FilterValue::StringArray(arr) => arr.join("|"), |
| 593 |
FilterValue::HashMapVal(hm) => hm |
| 594 |
.iter() |
| 595 |
.map(|(k, v)| format!("{}={}", k, v.to_display_string())) |
| 596 |
.collect::<Vec<_>>() |
| 597 |
.join("|"), |
| 598 |
FilterValue::IntVal(i) => i.to_string(), |
| 599 |
FilterValue::IntArray(arr) => arr |
| 600 |
.iter() |
| 601 |
.map(|i| i.to_string()) |
| 602 |
.collect::<Vec<_>>() |
| 603 |
.join("|"), |
| 604 |
FilterValue::BoolVal(b) => b.to_string(), |
| 605 |
FilterValue::Null => String::new(), |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
/// A single filter output with a filter identifier and extracted value. |
| 611 |
/// |
| 612 |
/// Each `FilterOutput` pairs a [`Filter`] variant with the [`FilterValue`] |
| 613 |
/// extracted from an RDAP response. The `filter` field identifies which field |
| 614 |
/// was extracted, and `value` contains the actual data. |
| 615 |
/// |
| 616 |
/// # Example |
| 617 |
/// |
| 618 |
/// ```rust |
| 619 |
/// use icann_rdap_common::filter::{Filter, FilterOutput, FilterValue}; |
| 620 |
/// |
| 621 |
/// let output = FilterOutput { |
| 622 |
/// filter: Filter::LdhName, |
| 623 |
/// value: FilterValue::StringVal("example.com".to_string()), |
| 624 |
/// }; |
| 625 |
/// |
| 626 |
/// assert_eq!(output.filter, Filter::LdhName); |
| 627 |
/// assert_eq!(output.string_value(), Some("example.com")); |
| 628 |
/// assert!(!output.is_null()); |
| 629 |
/// ``` |
| 630 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 631 |
pub struct FilterOutput { |
| 632 |
/// The filter that was applied. |
| 633 |
pub filter: Filter, |
| 634 |
/// The extracted value. |
| 635 |
pub value: FilterValue, |
| 636 |
} |
| 637 |
|
| 638 |
impl FilterOutput { |
| 639 |
/// Returns the string value if the inner `FilterValue` is `StringVal`, otherwise `None`. |
| 640 |
/// |
| 641 |
/// # Example |
| 642 |
/// |
| 643 |
/// ```rust |
| 644 |
/// use icann_rdap_common::filter::{Filter, FilterOutput, FilterValue}; |
| 645 |
/// |
| 646 |
/// let output = FilterOutput { |
| 647 |
/// filter: Filter::LdhName, |
| 648 |
/// value: FilterValue::StringVal("example.com".to_string()), |
| 649 |
/// }; |
| 650 |
/// assert_eq!(output.string_value(), Some("example.com")); |
| 651 |
/// |
| 652 |
/// let output = FilterOutput { |
| 653 |
/// filter: Filter::LdhName, |
| 654 |
/// value: FilterValue::Null, |
| 655 |
/// }; |
| 656 |
/// assert_eq!(output.string_value(), None); |
| 657 |
/// ``` |
| 658 |
pub fn string_value(&self) -> Option<&str> { |
| 659 |
self.value.string_value() |
| 660 |
} |
| 661 |
|
| 662 |
/// Returns `true` if the inner `FilterValue` is `Null`. |
| 663 |
/// |
| 664 |
/// # Example |
| 665 |
/// |
| 666 |
/// ```rust |
| 667 |
/// use icann_rdap_common::filter::{Filter, FilterOutput, FilterValue}; |
| 668 |
/// |
| 669 |
/// let output = FilterOutput { |
| 670 |
/// filter: Filter::LdhName, |
| 671 |
/// value: FilterValue::Null, |
| 672 |
/// }; |
| 673 |
/// assert!(output.is_null()); |
| 674 |
/// ``` |
| 675 |
pub fn is_null(&self) -> bool { |
| 676 |
self.value.is_null() |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
/// The result type for filter operations — a vector of filter outputs. |
| 681 |
/// |
| 682 |
/// Each element in the vector corresponds to one of the requested [`Filter`] |
| 683 |
/// values, in the same order as the input filter list. |
| 684 |
/// |
| 685 |
/// # Example |
| 686 |
/// |
| 687 |
/// ```rust |
| 688 |
/// use icann_rdap_common::prelude::*; |
| 689 |
/// use icann_rdap_common::filter::*; |
| 690 |
/// |
| 691 |
/// let domain = Domain::builder() |
| 692 |
/// .ldh_name("example.com") |
| 693 |
/// .handle("EXAMPLE-DOM") |
| 694 |
/// .build(); |
| 695 |
/// |
| 696 |
/// let filters = vec![Filter::LdhName, Filter::Handle]; |
| 697 |
/// let result: FilterResult = extract(&domain, &filters); |
| 698 |
/// |
| 699 |
/// assert_eq!(result.len(), 2); |
| 700 |
/// ``` |
| 701 |
pub type FilterResult = Vec<FilterOutput>; |
| 702 |
|
| 703 |
/// Trait for types that can be filtered. |
| 704 |
/// |
| 705 |
/// Implement this trait for RDAP response types to enable field extraction |
| 706 |
/// via the [`extract`] function. The implementation should return a [`FilterResult`] |
| 707 |
/// containing one output for each requested filter, in the same order. |
| 708 |
/// |
| 709 |
/// Unknown or unsupported filters should return a [`FilterValue::Null`] output. |
| 710 |
/// |
| 711 |
/// # Implementors |
| 712 |
/// |
| 713 |
/// This trait is implemented for all RDAP response types in this crate: |
| 714 |
/// - [`crate::response::Domain`] |
| 715 |
/// - [`crate::response::Autnum`] |
| 716 |
/// - [`crate::response::Entity`] |
| 717 |
/// - [`crate::response::Nameserver`] |
| 718 |
/// - [`crate::response::Network`] |
| 719 |
/// - [`crate::response::DomainSearchResults`] |
| 720 |
/// - [`crate::response::AutnumSearchResults`] |
| 721 |
/// - [`crate::response::EntitySearchResults`] |
| 722 |
/// - [`crate::response::NameserverSearchResults`] |
| 723 |
/// - [`crate::response::IpSearchResults`] |
| 724 |
/// - [`crate::response::RdapResponse`] |
| 725 |
/// |
| 726 |
/// # Example |
| 727 |
/// |
| 728 |
/// ```rust |
| 729 |
/// use icann_rdap_common::filter::{Filter, Filterable, FilterResult, FilterValue, FilterOutput}; |
| 730 |
/// use icann_rdap_common::response::Domain; |
| 731 |
/// |
| 732 |
/// // Domain already implements Filterable |
| 733 |
/// let domain = Domain::builder() |
| 734 |
/// .ldh_name("example.com") |
| 735 |
/// .build(); |
| 736 |
/// |
| 737 |
/// let filters = vec![Filter::LdhName]; |
| 738 |
/// let result: FilterResult = domain.filter(&filters); |
| 739 |
/// |
| 740 |
/// assert_eq!(result.len(), 1); |
| 741 |
/// assert_eq!(result[0].value, FilterValue::StringVal("example.com".to_string())); |
| 742 |
/// ``` |
| 743 |
pub trait Filterable { |
| 744 |
/// Extract the requested filters from this response. |
| 745 |
/// |
| 746 |
/// Returns a [`FilterResult`] with one entry per requested filter, in the same order. |
| 747 |
/// Filters that don't apply to this object type produce a [`FilterValue::Null`] output. |
| 748 |
fn filter(&self, filters: &[Filter]) -> FilterResult; |
| 749 |
} |
| 750 |
|
| 751 |
/// Convenience function to extract filters from any filterable type. |
| 752 |
/// |
| 753 |
/// This is a thin wrapper around [`Filterable::filter`] that provides a |
| 754 |
/// function-style interface. It works with any type implementing [`Filterable`]. |
| 755 |
/// |
| 756 |
/// # Example |
| 757 |
/// |
| 758 |
/// ```rust |
| 759 |
/// use icann_rdap_common::prelude::*; |
| 760 |
/// use icann_rdap_common::filter::*; |
| 761 |
/// |
| 762 |
/// let domain = Domain::builder() |
| 763 |
/// .ldh_name("example.com") |
| 764 |
/// .handle("EXAMPLE-DOM") |
| 765 |
/// .statuses(vec!["active".to_string()]) |
| 766 |
/// .build(); |
| 767 |
/// |
| 768 |
/// let filters = vec![Filter::LdhName, Filter::Handle, Filter::Status]; |
| 769 |
/// let results = extract(&domain, &filters); |
| 770 |
/// |
| 771 |
/// assert_eq!(results.len(), 3); |
| 772 |
/// assert_eq!(results[0].value, FilterValue::StringVal("example.com".to_string())); |
| 773 |
/// assert_eq!(results[1].value, FilterValue::StringVal("EXAMPLE-DOM".to_string())); |
| 774 |
/// ``` |
| 775 |
pub fn extract<T: Filterable>(response: &T, filters: &[Filter]) -> FilterResult { |
| 776 |
response.filter(filters) |
| 777 |
} |
| 778 |
|
| 779 |
/// Convert an `Option<T>` (where T: Display) into a `FilterValue::StringVal` or `Null`. |
| 780 |
/// |
| 781 |
/// This eliminates the common pattern of manually mapping `Option` values: |
| 782 |
/// |
| 783 |
/// ```ignore |
| 784 |
/// self.handle() |
| 785 |
/// .map(|h| FilterValue::StringVal(h.to_string())) |
| 786 |
/// .unwrap_or(FilterValue::Null) |
| 787 |
/// ``` |
| 788 |
/// |
| 789 |
/// in favor of: |
| 790 |
/// |
| 791 |
/// ```ignore |
| 792 |
/// opt_to_string(self.handle()) |
| 793 |
/// ``` |
| 794 |
/// |
| 795 |
/// # Example |
| 796 |
/// |
| 797 |
/// ```rust |
| 798 |
/// use icann_rdap_common::filter::{FilterValue, opt_to_string}; |
| 799 |
/// |
| 800 |
/// let val = opt_to_string(Some("hello")); |
| 801 |
/// assert_eq!(val, FilterValue::StringVal("hello".to_string())); |
| 802 |
/// |
| 803 |
/// let val: FilterValue = opt_to_string::<&str>(None); |
| 804 |
/// assert_eq!(val, FilterValue::Null); |
| 805 |
/// ``` |
| 806 |
pub fn opt_to_string<T: std::fmt::Display>(opt: Option<T>) -> FilterValue { |
| 807 |
opt.map(|v| FilterValue::StringVal(v.to_string())) |
| 808 |
.unwrap_or(FilterValue::Null) |
| 809 |
} |
| 810 |
|
| 811 |
/// Convert an `Option<u32>` into a `FilterValue::IntVal` or `Null`. |
| 812 |
/// |
| 813 |
/// # Example |
| 814 |
/// |
| 815 |
/// ```rust |
| 816 |
/// use icann_rdap_common::filter::{FilterValue, opt_to_i64}; |
| 817 |
/// |
| 818 |
/// let val = opt_to_i64(Some(12345)); |
| 819 |
/// assert_eq!(val, FilterValue::IntVal(12345)); |
| 820 |
/// |
| 821 |
/// let val = opt_to_i64(None); |
| 822 |
/// assert_eq!(val, FilterValue::Null); |
| 823 |
/// ``` |
| 824 |
pub fn opt_to_i64(opt: Option<u32>) -> FilterValue { |
| 825 |
opt.map(|v| FilterValue::IntVal(v as i64)) |
| 826 |
.unwrap_or(FilterValue::Null) |
| 827 |
} |
| 828 |
|
| 829 |
pub(crate) fn find_entity_email_by_role(entities: &[Entity], role: EntityRole) -> Option<String> { |
| 830 |
let mut queue: VecDeque<&Entity> = entities.iter().collect(); |
| 831 |
while let Some(entity) = queue.pop_front() { |
| 832 |
if entity.is_entity_role(&role.to_string()) |
| 833 |
&& let Some(contact) = entity.contact() |
| 834 |
&& let Some(email) = contact.email().map(|e| e.email().to_string()) |
| 835 |
{ |
| 836 |
return Some(email); |
| 837 |
} |
| 838 |
queue.extend(ObjectCommonFields::entities(entity)); |
| 839 |
} |
| 840 |
None |
| 841 |
} |
| 842 |
|
| 843 |
pub(crate) fn find_entity_full_name_by_role( |
| 844 |
entities: &[Entity], |
| 845 |
role: EntityRole, |
| 846 |
) -> Option<String> { |
| 847 |
let mut queue: VecDeque<&Entity> = entities.iter().collect(); |
| 848 |
while let Some(entity) = queue.pop_front() { |
| 849 |
if entity.is_entity_role(&role.to_string()) |
| 850 |
&& let Some(contact) = entity.contact() |
| 851 |
&& let Some(name) = contact.full_name().map(|n| n.to_string()) |
| 852 |
{ |
| 853 |
return Some(name); |
| 854 |
} |
| 855 |
queue.extend(ObjectCommonFields::entities(entity)); |
| 856 |
} |
| 857 |
None |
| 858 |
} |
| 859 |
|
| 860 |
pub(crate) fn find_entity_voice_phone_by_role( |
| 861 |
entities: &[Entity], |
| 862 |
role: EntityRole, |
| 863 |
) -> Option<String> { |
| 864 |
let mut queue: VecDeque<&Entity> = entities.iter().collect(); |
| 865 |
while let Some(entity) = queue.pop_front() { |
| 866 |
if entity.is_entity_role(&role.to_string()) |
| 867 |
&& let Some(contact) = entity.contact() |
| 868 |
&& let Some(phone) = contact.voice_phone().map(|p| p.phone().to_string()) |
| 869 |
{ |
| 870 |
return Some(phone); |
| 871 |
} |
| 872 |
queue.extend(ObjectCommonFields::entities(entity)); |
| 873 |
} |
| 874 |
None |
| 875 |
} |
| 876 |
|
| 877 |
pub(crate) fn find_entity_fax_phone_by_role( |
| 878 |
entities: &[Entity], |
| 879 |
role: EntityRole, |
| 880 |
) -> Option<String> { |
| 881 |
let mut queue: VecDeque<&Entity> = entities.iter().collect(); |
| 882 |
while let Some(entity) = queue.pop_front() { |
| 883 |
if entity.is_entity_role(&role.to_string()) |
| 884 |
&& let Some(contact) = entity.contact() |
| 885 |
&& let Some(phone) = contact.fax_phone().map(|p| p.phone().to_string()) |
| 886 |
{ |
| 887 |
return Some(phone); |
| 888 |
} |
| 889 |
queue.extend(ObjectCommonFields::entities(entity)); |
| 890 |
} |
| 891 |
None |
| 892 |
} |
| 893 |
|
| 894 |
pub(crate) fn find_entity_contact_uris_by_role( |
| 895 |
entities: &[Entity], |
| 896 |
role: EntityRole, |
| 897 |
) -> Vec<String> { |
| 898 |
let mut queue: VecDeque<&Entity> = entities.iter().collect(); |
| 899 |
while let Some(entity) = queue.pop_front() { |
| 900 |
if entity.is_entity_role(&role.to_string()) |
| 901 |
&& let Some(contact) = entity.contact() |
| 902 |
{ |
| 903 |
return contact |
| 904 |
.contact_uris() |
| 905 |
.iter() |
| 906 |
.map(|u| u.to_string()) |
| 907 |
.collect(); |
| 908 |
} |
| 909 |
queue.extend(ObjectCommonFields::entities(entity)); |
| 910 |
} |
| 911 |
Vec::new() |
| 912 |
} |
| 913 |
|
| 914 |
pub(crate) fn find_entity_country_names_by_role( |
| 915 |
entities: &[Entity], |
| 916 |
role: EntityRole, |
| 917 |
) -> Vec<String> { |
| 918 |
let mut queue: VecDeque<&Entity> = entities.iter().collect(); |
| 919 |
while let Some(entity) = queue.pop_front() { |
| 920 |
if entity.is_entity_role(&role.to_string()) |
| 921 |
&& let Some(contact) = entity.contact() |
| 922 |
{ |
| 923 |
return contact |
| 924 |
.postal_addresses() |
| 925 |
.iter() |
| 926 |
.filter_map(|a| a.country_name()) |
| 927 |
.map(|n| n.to_string()) |
| 928 |
.collect(); |
| 929 |
} |
| 930 |
queue.extend(ObjectCommonFields::entities(entity)); |
| 931 |
} |
| 932 |
Vec::new() |
| 933 |
} |
| 934 |
|
| 935 |
pub(crate) fn find_entity_country_codes_by_role( |
| 936 |
entities: &[Entity], |
| 937 |
role: EntityRole, |
| 938 |
) -> Vec<String> { |
| 939 |
let mut queue: VecDeque<&Entity> = entities.iter().collect(); |
| 940 |
while let Some(entity) = queue.pop_front() { |
| 941 |
if entity.is_entity_role(&role.to_string()) |
| 942 |
&& let Some(contact) = entity.contact() |
| 943 |
{ |
| 944 |
return contact |
| 945 |
.postal_addresses() |
| 946 |
.iter() |
| 947 |
.filter_map(|a| a.country_code()) |
| 948 |
.map(|c| c.to_string()) |
| 949 |
.collect(); |
| 950 |
} |
| 951 |
queue.extend(ObjectCommonFields::entities(entity)); |
| 952 |
} |
| 953 |
Vec::new() |
| 954 |
} |
| 955 |
|
| 956 |
pub(crate) fn find_entity_public_ids_by_role( |
| 957 |
entities: &[Entity], |
| 958 |
role: EntityRole, |
| 959 |
) -> Option<HashMap<String, FilterValue>> { |
| 960 |
let mut queue: VecDeque<&Entity> = entities.iter().collect(); |
| 961 |
while let Some(entity) = queue.pop_front() { |
| 962 |
if entity.is_entity_role(&role.to_string()) { |
| 963 |
let public_ids: HashMap<String, FilterValue> = entity |
| 964 |
.public_ids() |
| 965 |
.iter() |
| 966 |
.filter_map(|p| { |
| 967 |
let id_type = p.id_type()?; |
| 968 |
let identifier = p.identifier()?; |
| 969 |
Some(( |
| 970 |
id_type.to_string(), |
| 971 |
FilterValue::StringVal(identifier.to_string()), |
| 972 |
)) |
| 973 |
}) |
| 974 |
.collect(); |
| 975 |
if !public_ids.is_empty() { |
| 976 |
return Some(public_ids); |
| 977 |
} |
| 978 |
} |
| 979 |
queue.extend(ObjectCommonFields::entities(entity)); |
| 980 |
} |
| 981 |
None |
| 982 |
} |
| 983 |
|
| 984 |
pub(crate) fn find_entity_handle_by_role(entities: &[Entity], role: EntityRole) -> Option<String> { |
| 985 |
let mut queue: VecDeque<&Entity> = entities.iter().collect(); |
| 986 |
while let Some(entity) = queue.pop_front() { |
| 987 |
if entity.is_entity_role(&role.to_string()) { |
| 988 |
return entity.handle().map(|h| h.to_string()); |
| 989 |
} |
| 990 |
queue.extend(ObjectCommonFields::entities(entity)); |
| 991 |
} |
| 992 |
None |
| 993 |
} |
| 994 |
|
| 995 |
/// Handles all entity role filter arms (Registrant*, Abuse*, Technical*, Registrar*). |
| 996 |
/// Returns Some(FilterOutput) if the filter is an entity role filter, None otherwise. |
| 997 |
pub(crate) fn entity_role_filter_output(entities: &[Entity], f: Filter) -> Option<FilterOutput> { |
| 998 |
let role = match f { |
| 999 |
Filter::RegistrantEmail |
| 1000 |
| Filter::RegistrantFullName |
| 1001 |
| Filter::RegistrantVoice |
| 1002 |
| Filter::RegistrantFax |
| 1003 |
| Filter::RegistrantContactUri |
| 1004 |
| Filter::RegistrantCountryName |
| 1005 |
| Filter::RegistrantCountryCode |
| 1006 |
| Filter::RegistrantPublicId |
| 1007 |
| Filter::RegistrantHandle => EntityRole::Registrant, |
| 1008 |
Filter::AbuseEmail |
| 1009 |
| Filter::AbuseFullName |
| 1010 |
| Filter::AbuseVoice |
| 1011 |
| Filter::AbuseFax |
| 1012 |
| Filter::AbuseContactUri |
| 1013 |
| Filter::AbuseCountryName |
| 1014 |
| Filter::AbuseCountryCode |
| 1015 |
| Filter::AbusePublicId |
| 1016 |
| Filter::AbuseHandle => EntityRole::Abuse, |
| 1017 |
Filter::TechnicalEmail |
| 1018 |
| Filter::TechnicalFullName |
| 1019 |
| Filter::TechnicalVoice |
| 1020 |
| Filter::TechnicalFax |
| 1021 |
| Filter::TechnicalContactUri |
| 1022 |
| Filter::TechnicalCountryName |
| 1023 |
| Filter::TechnicalCountryCode |
| 1024 |
| Filter::TechnicalPublicId |
| 1025 |
| Filter::TechnicalHandle => EntityRole::Technical, |
| 1026 |
Filter::RegistrarEmail |
| 1027 |
| Filter::RegistrarFullName |
| 1028 |
| Filter::RegistrarVoice |
| 1029 |
| Filter::RegistrarFax |
| 1030 |
| Filter::RegistrarContactUri |
| 1031 |
| Filter::RegistrarCountryName |
| 1032 |
| Filter::RegistrarCountryCode |
| 1033 |
| Filter::RegistrarPublicId |
| 1034 |
| Filter::RegistrarHandle => EntityRole::Registrar, |
| 1035 |
_ => return None, |
| 1036 |
}; |
| 1037 |
|
| 1038 |
let value = match f { |
| 1039 |
Filter::RegistrantEmail |
| 1040 |
| Filter::AbuseEmail |
| 1041 |
| Filter::TechnicalEmail |
| 1042 |
| Filter::RegistrarEmail => find_entity_email_by_role(entities, role) |
| 1043 |
.map(|e| FilterValue::StringVal(e.to_string())) |
| 1044 |
.unwrap_or(FilterValue::Null), |
| 1045 |
Filter::RegistrantFullName |
| 1046 |
| Filter::AbuseFullName |
| 1047 |
| Filter::TechnicalFullName |
| 1048 |
| Filter::RegistrarFullName => find_entity_full_name_by_role(entities, role) |
| 1049 |
.map(|e| FilterValue::StringVal(e.to_string())) |
| 1050 |
.unwrap_or(FilterValue::Null), |
| 1051 |
Filter::RegistrantVoice |
| 1052 |
| Filter::AbuseVoice |
| 1053 |
| Filter::TechnicalVoice |
| 1054 |
| Filter::RegistrarVoice => find_entity_voice_phone_by_role(entities, role) |
| 1055 |
.map(|e| FilterValue::StringVal(e.to_string())) |
| 1056 |
.unwrap_or(FilterValue::Null), |
| 1057 |
Filter::RegistrantFax | Filter::AbuseFax | Filter::TechnicalFax | Filter::RegistrarFax => { |
| 1058 |
find_entity_fax_phone_by_role(entities, role) |
| 1059 |
.map(|e| FilterValue::StringVal(e.to_string())) |
| 1060 |
.unwrap_or(FilterValue::Null) |
| 1061 |
} |
| 1062 |
Filter::RegistrantContactUri |
| 1063 |
| Filter::AbuseContactUri |
| 1064 |
| Filter::TechnicalContactUri |
| 1065 |
| Filter::RegistrarContactUri => { |
| 1066 |
FilterValue::StringArray(find_entity_contact_uris_by_role(entities, role)) |
| 1067 |
} |
| 1068 |
Filter::RegistrantCountryName |
| 1069 |
| Filter::AbuseCountryName |
| 1070 |
| Filter::TechnicalCountryName |
| 1071 |
| Filter::RegistrarCountryName => { |
| 1072 |
FilterValue::StringArray(find_entity_country_names_by_role(entities, role)) |
| 1073 |
} |
| 1074 |
Filter::RegistrantCountryCode |
| 1075 |
| Filter::AbuseCountryCode |
| 1076 |
| Filter::TechnicalCountryCode |
| 1077 |
| Filter::RegistrarCountryCode => { |
| 1078 |
FilterValue::StringArray(find_entity_country_codes_by_role(entities, role)) |
| 1079 |
} |
| 1080 |
Filter::RegistrantPublicId |
| 1081 |
| Filter::AbusePublicId |
| 1082 |
| Filter::TechnicalPublicId |
| 1083 |
| Filter::RegistrarPublicId => FilterValue::HashMapVal( |
| 1084 |
find_entity_public_ids_by_role(entities, role).unwrap_or_default(), |
| 1085 |
), |
| 1086 |
Filter::RegistrantHandle |
| 1087 |
| Filter::AbuseHandle |
| 1088 |
| Filter::TechnicalHandle |
| 1089 |
| Filter::RegistrarHandle => find_entity_handle_by_role(entities, role) |
| 1090 |
.map(|e| FilterValue::StringVal(e.to_string())) |
| 1091 |
.unwrap_or(FilterValue::Null), |
| 1092 |
_ => return None, |
| 1093 |
}; |
| 1094 |
|
| 1095 |
Some(FilterOutput { filter: f, value }) |
| 1096 |
} |
| 1097 |
|
| 1098 |
/// Handles entity role filters for search results (returns arrays). |
| 1099 |
pub(crate) fn entity_role_filter_output_search<'a, T, I>(results: I, f: Filter) -> FilterOutput |
| 1100 |
where |
| 1101 |
T: EntityRoleProvider + 'a, |
| 1102 |
I: Iterator<Item = &'a T> + 'a, |
| 1103 |
{ |
| 1104 |
let role = match f { |
| 1105 |
Filter::RegistrantEmail |
| 1106 |
| Filter::RegistrantFullName |
| 1107 |
| Filter::RegistrantVoice |
| 1108 |
| Filter::RegistrantFax |
| 1109 |
| Filter::RegistrantContactUri |
| 1110 |
| Filter::RegistrantCountryName |
| 1111 |
| Filter::RegistrantCountryCode |
| 1112 |
| Filter::RegistrantPublicId |
| 1113 |
| Filter::RegistrantHandle => EntityRole::Registrant, |
| 1114 |
Filter::AbuseEmail |
| 1115 |
| Filter::AbuseFullName |
| 1116 |
| Filter::AbuseVoice |
| 1117 |
| Filter::AbuseFax |
| 1118 |
| Filter::AbuseContactUri |
| 1119 |
| Filter::AbuseCountryName |
| 1120 |
| Filter::AbuseCountryCode |
| 1121 |
| Filter::AbusePublicId |
| 1122 |
| Filter::AbuseHandle => EntityRole::Abuse, |
| 1123 |
Filter::TechnicalEmail |
| 1124 |
| Filter::TechnicalFullName |
| 1125 |
| Filter::TechnicalVoice |
| 1126 |
| Filter::TechnicalFax |
| 1127 |
| Filter::TechnicalContactUri |
| 1128 |
| Filter::TechnicalCountryName |
| 1129 |
| Filter::TechnicalCountryCode |
| 1130 |
| Filter::TechnicalPublicId |
| 1131 |
| Filter::TechnicalHandle => EntityRole::Technical, |
| 1132 |
Filter::RegistrarEmail |
| 1133 |
| Filter::RegistrarFullName |
| 1134 |
| Filter::RegistrarVoice |
| 1135 |
| Filter::RegistrarFax |
| 1136 |
| Filter::RegistrarContactUri |
| 1137 |
| Filter::RegistrarCountryName |
| 1138 |
| Filter::RegistrarCountryCode |
| 1139 |
| Filter::RegistrarPublicId |
| 1140 |
| Filter::RegistrarHandle => EntityRole::Registrar, |
| 1141 |
_ => { |
| 1142 |
return FilterOutput { |
| 1143 |
filter: f, |
| 1144 |
value: FilterValue::Null, |
| 1145 |
}; |
| 1146 |
} |
| 1147 |
}; |
| 1148 |
|
| 1149 |
let value = match f { |
| 1150 |
Filter::RegistrantEmail |
| 1151 |
| Filter::AbuseEmail |
| 1152 |
| Filter::TechnicalEmail |
| 1153 |
| Filter::RegistrarEmail => FilterValue::StringArray( |
| 1154 |
results |
| 1155 |
.filter_map(|r| find_entity_email_by_role(r.entities(), role)) |
| 1156 |
.collect(), |
| 1157 |
), |
| 1158 |
Filter::RegistrantFullName |
| 1159 |
| Filter::AbuseFullName |
| 1160 |
| Filter::TechnicalFullName |
| 1161 |
| Filter::RegistrarFullName => FilterValue::StringArray( |
| 1162 |
results |
| 1163 |
.filter_map(|r| find_entity_full_name_by_role(r.entities(), role)) |
| 1164 |
.collect(), |
| 1165 |
), |
| 1166 |
Filter::RegistrantVoice |
| 1167 |
| Filter::AbuseVoice |
| 1168 |
| Filter::TechnicalVoice |
| 1169 |
| Filter::RegistrarVoice => FilterValue::StringArray( |
| 1170 |
results |
| 1171 |
.filter_map(|r| find_entity_voice_phone_by_role(r.entities(), role)) |
| 1172 |
.collect(), |
| 1173 |
), |
| 1174 |
Filter::RegistrantFax | Filter::AbuseFax | Filter::TechnicalFax | Filter::RegistrarFax => { |
| 1175 |
FilterValue::StringArray( |
| 1176 |
results |
| 1177 |
.filter_map(|r| find_entity_fax_phone_by_role(r.entities(), role)) |
| 1178 |
.collect(), |
| 1179 |
) |
| 1180 |
} |
| 1181 |
Filter::RegistrantContactUri |
| 1182 |
| Filter::AbuseContactUri |
| 1183 |
| Filter::TechnicalContactUri |
| 1184 |
| Filter::RegistrarContactUri => FilterValue::StringArray( |
| 1185 |
results |
| 1186 |
.flat_map(|r| find_entity_contact_uris_by_role(r.entities(), role)) |
| 1187 |
.collect(), |
| 1188 |
), |
| 1189 |
Filter::RegistrantCountryName |
| 1190 |
| Filter::AbuseCountryName |
| 1191 |
| Filter::TechnicalCountryName |
| 1192 |
| Filter::RegistrarCountryName => FilterValue::StringArray( |
| 1193 |
results |
| 1194 |
.flat_map(|r| find_entity_country_names_by_role(r.entities(), role)) |
| 1195 |
.collect(), |
| 1196 |
), |
| 1197 |
Filter::RegistrantCountryCode |
| 1198 |
| Filter::AbuseCountryCode |
| 1199 |
| Filter::TechnicalCountryCode |
| 1200 |
| Filter::RegistrarCountryCode => FilterValue::StringArray( |
| 1201 |
results |
| 1202 |
.flat_map(|r| find_entity_country_codes_by_role(r.entities(), role)) |
| 1203 |
.collect(), |
| 1204 |
), |
| 1205 |
Filter::RegistrantPublicId |
| 1206 |
| Filter::AbusePublicId |
| 1207 |
| Filter::TechnicalPublicId |
| 1208 |
| Filter::RegistrarPublicId => { |
| 1209 |
let mut public_ids: HashMap<String, FilterValue> = HashMap::new(); |
| 1210 |
for r in results { |
| 1211 |
if let Some(pid) = find_entity_public_ids_by_role(r.entities(), role) { |
| 1212 |
public_ids.extend(pid); |
| 1213 |
} |
| 1214 |
} |
| 1215 |
FilterValue::HashMapVal(public_ids) |
| 1216 |
} |
| 1217 |
Filter::RegistrantHandle |
| 1218 |
| Filter::AbuseHandle |
| 1219 |
| Filter::TechnicalHandle |
| 1220 |
| Filter::RegistrarHandle => FilterValue::StringArray( |
| 1221 |
results |
| 1222 |
.filter_map(|r| find_entity_handle_by_role(r.entities(), role)) |
| 1223 |
.collect(), |
| 1224 |
), |
| 1225 |
_ => FilterValue::Null, |
| 1226 |
}; |
| 1227 |
|
| 1228 |
FilterOutput { filter: f, value } |
| 1229 |
} |
| 1230 |
|
| 1231 |
/// Trait for types that can provide entity lists for role-based filtering. |
| 1232 |
/// |
| 1233 |
/// Implemented by all RDAP response types that may contain nested entities |
| 1234 |
/// with roles (registrant, abuse, technical, registrar). |
| 1235 |
pub(crate) trait EntityRoleProvider { |
| 1236 |
/// Returns the list of entities associated with this object. |
| 1237 |
fn entities(&self) -> &[Entity]; |
| 1238 |
} |
| 1239 |
|
| 1240 |
impl EntityRoleProvider for crate::response::Autnum { |
| 1241 |
fn entities(&self) -> &[Entity] { |
| 1242 |
ObjectCommonFields::entities(self) |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
impl EntityRoleProvider for crate::response::Domain { |
| 1247 |
fn entities(&self) -> &[Entity] { |
| 1248 |
ObjectCommonFields::entities(self) |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
impl EntityRoleProvider for crate::response::Entity { |
| 1253 |
fn entities(&self) -> &[Entity] { |
| 1254 |
ObjectCommonFields::entities(self) |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
impl EntityRoleProvider for crate::response::Network { |
| 1259 |
fn entities(&self) -> &[Entity] { |
| 1260 |
ObjectCommonFields::entities(self) |
| 1261 |
} |
| 1262 |
} |
| 1263 |
|
| 1264 |
impl EntityRoleProvider for crate::response::Nameserver { |
| 1265 |
fn entities(&self) -> &[Entity] { |
| 1266 |
ObjectCommonFields::entities(self) |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
impl Filterable for crate::response::RdapResponse { |
| 1271 |
fn filter(&self, filters: &[Filter]) -> FilterResult { |
| 1272 |
match self { |
| 1273 |
crate::response::RdapResponse::Domain(d) => d.filter(filters), |
| 1274 |
crate::response::RdapResponse::Autnum(a) => a.filter(filters), |
| 1275 |
crate::response::RdapResponse::Entity(e) => e.filter(filters), |
| 1276 |
crate::response::RdapResponse::Nameserver(n) => n.filter(filters), |
| 1277 |
crate::response::RdapResponse::Network(n) => n.filter(filters), |
| 1278 |
crate::response::RdapResponse::DomainSearchResults(d) => d.filter(filters), |
| 1279 |
crate::response::RdapResponse::AutnumSearchResults(a) => a.filter(filters), |
| 1280 |
crate::response::RdapResponse::EntitySearchResults(e) => e.filter(filters), |
| 1281 |
crate::response::RdapResponse::NameserverSearchResults(n) => n.filter(filters), |
| 1282 |
crate::response::RdapResponse::IpSearchResults(i) => i.filter(filters), |
| 1283 |
_ => filters |
| 1284 |
.iter() |
| 1285 |
.map(|f| FilterOutput { |
| 1286 |
filter: *f, |
| 1287 |
value: FilterValue::Null, |
| 1288 |
}) |
| 1289 |
.collect(), |
| 1290 |
} |
| 1291 |
} |
| 1292 |
} |