| 1 |
//! Entity filter implementation. |
| 2 |
//! |
| 3 |
//! Extracts fields from [`crate::response::Entity`] RDAP objects. |
| 4 |
//! |
| 5 |
//! # Supported Filters |
| 6 |
//! |
| 7 |
//! | Filter | Value Type | Description | |
| 8 |
//! |---|---|---| |
| 9 |
//! | `Handle` | `StringVal` | Entity handle | |
| 10 |
//! | `Status` | `StringArray` | Status list | |
| 11 |
//! | `ObjectClassName` | `StringVal` | Always `"entity"` | |
| 12 |
//! | `Event` | `HashMapVal` | Event actions mapped to dates | |
| 13 |
//! | `RdapConformance` | `StringArray` | RDAP conformance URIs | |
| 14 |
//! | `Role` | `StringArray` | Entity role(s) (e.g., `"registrant"`, `"admin"`) | |
| 15 |
//! | `Email` | `StringArray` | Email addresses from contact | |
| 16 |
//! | `FullName` | `StringVal` | Full name from contact | |
| 17 |
//! | `PublicId` | `HashMapVal` | Public identifiers | |
| 18 |
//! | `Voice` | `StringVal` | Phone number from contact | |
| 19 |
//! | `Fax` | `StringVal` | Fax number from contact | |
| 20 |
//! | `ContactUri` | `StringArray` | Contact URIs from contact | |
| 21 |
//! | `CountryName` | `StringArray` | Country names from postal addresses | |
| 22 |
//! | `CountryCode` | `StringArray` | Country codes from postal addresses | |
| 23 |
//! | Entity roles | Various | Extract from nested child entities | |
| 24 |
|
| 25 |
use super::*; |
| 26 |
use crate::response::{CommonFields, Entity, ObjectCommonFields}; |
| 27 |
|
| 28 |
impl Filterable for Entity { |
| 29 |
fn filter(&self, filters: &[Filter]) -> FilterResult { |
| 30 |
filters |
| 31 |
.iter() |
| 32 |
.map(|f| match f { |
| 33 |
Filter::Handle => FilterOutput { |
| 34 |
filter: *f, |
| 35 |
value: opt_to_string(self.handle()), |
| 36 |
}, |
| 37 |
Filter::Status => FilterOutput { |
| 38 |
filter: *f, |
| 39 |
value: FilterValue::StringArray( |
| 40 |
self.status().iter().map(|s| s.to_string()).collect(), |
| 41 |
), |
| 42 |
}, |
| 43 |
Filter::ObjectClassName => FilterOutput { |
| 44 |
filter: *f, |
| 45 |
value: FilterValue::StringVal(self.object_class_name().to_string()), |
| 46 |
}, |
| 47 |
Filter::Event => FilterOutput { |
| 48 |
filter: *f, |
| 49 |
value: FilterValue::HashMapVal( |
| 50 |
self.events() |
| 51 |
.iter() |
| 52 |
.filter_map(|e| { |
| 53 |
let action = e.event_action()?; |
| 54 |
let date = e.event_date()?; |
| 55 |
Some((action.to_string(), FilterValue::StringVal(date.to_string()))) |
| 56 |
}) |
| 57 |
.collect(), |
| 58 |
), |
| 59 |
}, |
| 60 |
Filter::RdapConformance => FilterOutput { |
| 61 |
filter: *f, |
| 62 |
value: FilterValue::StringArray( |
| 63 |
self.common() |
| 64 |
.rdap_conformance |
| 65 |
.as_deref() |
| 66 |
.unwrap_or_default() |
| 67 |
.iter() |
| 68 |
.map(|ext| ext.0.clone()) |
| 69 |
.collect(), |
| 70 |
), |
| 71 |
}, |
| 72 |
Filter::Role => FilterOutput { |
| 73 |
filter: *f, |
| 74 |
value: FilterValue::StringArray( |
| 75 |
self.roles().iter().map(|r| r.to_string()).collect(), |
| 76 |
), |
| 77 |
}, |
| 78 |
Filter::Email => FilterOutput { |
| 79 |
filter: *f, |
| 80 |
value: self |
| 81 |
.contact() |
| 82 |
.and_then(|c| { |
| 83 |
let emails: Vec<String> = |
| 84 |
c.emails().iter().map(|e| e.email().to_string()).collect(); |
| 85 |
if emails.is_empty() { |
| 86 |
None |
| 87 |
} else { |
| 88 |
Some(FilterValue::StringArray(emails)) |
| 89 |
} |
| 90 |
}) |
| 91 |
.unwrap_or(FilterValue::Null), |
| 92 |
}, |
| 93 |
Filter::FullName => FilterOutput { |
| 94 |
filter: *f, |
| 95 |
value: self |
| 96 |
.contact() |
| 97 |
.and_then(|c| c.full_name().map(|n| FilterValue::StringVal(n.to_string()))) |
| 98 |
.unwrap_or(FilterValue::Null), |
| 99 |
}, |
| 100 |
Filter::PublicId => FilterOutput { |
| 101 |
filter: *f, |
| 102 |
value: FilterValue::HashMapVal( |
| 103 |
self.public_ids() |
| 104 |
.iter() |
| 105 |
.filter_map(|p| { |
| 106 |
let id_type = p.id_type()?; |
| 107 |
let identifier = p.identifier()?; |
| 108 |
Some(( |
| 109 |
id_type.to_string(), |
| 110 |
FilterValue::StringVal(identifier.to_string()), |
| 111 |
)) |
| 112 |
}) |
| 113 |
.collect(), |
| 114 |
), |
| 115 |
}, |
| 116 |
Filter::Voice => FilterOutput { |
| 117 |
filter: *f, |
| 118 |
value: self |
| 119 |
.contact() |
| 120 |
.and_then(|c| { |
| 121 |
c.voice_phone() |
| 122 |
.map(|p| FilterValue::StringVal(p.phone().to_string())) |
| 123 |
}) |
| 124 |
.unwrap_or(FilterValue::Null), |
| 125 |
}, |
| 126 |
Filter::Fax => FilterOutput { |
| 127 |
filter: *f, |
| 128 |
value: self |
| 129 |
.contact() |
| 130 |
.and_then(|c| { |
| 131 |
c.fax_phone() |
| 132 |
.map(|p| FilterValue::StringVal(p.phone().to_string())) |
| 133 |
}) |
| 134 |
.unwrap_or(FilterValue::Null), |
| 135 |
}, |
| 136 |
Filter::ContactUri => FilterOutput { |
| 137 |
filter: *f, |
| 138 |
value: FilterValue::StringArray( |
| 139 |
self.contact() |
| 140 |
.map(|c| c.contact_uris().iter().map(|u| u.to_string()).collect()) |
| 141 |
.unwrap_or_default(), |
| 142 |
), |
| 143 |
}, |
| 144 |
Filter::CountryName => FilterOutput { |
| 145 |
filter: *f, |
| 146 |
value: FilterValue::StringArray( |
| 147 |
self.contact() |
| 148 |
.map(|c| { |
| 149 |
c.postal_addresses() |
| 150 |
.iter() |
| 151 |
.filter_map(|a| a.country_name()) |
| 152 |
.map(|n| n.to_string()) |
| 153 |
.collect() |
| 154 |
}) |
| 155 |
.unwrap_or_default(), |
| 156 |
), |
| 157 |
}, |
| 158 |
Filter::CountryCode => FilterOutput { |
| 159 |
filter: *f, |
| 160 |
value: FilterValue::StringArray( |
| 161 |
self.contact() |
| 162 |
.map(|c| { |
| 163 |
c.postal_addresses() |
| 164 |
.iter() |
| 165 |
.filter_map(|a| a.country_code()) |
| 166 |
.map(|c| c.to_string()) |
| 167 |
.collect() |
| 168 |
}) |
| 169 |
.unwrap_or_default(), |
| 170 |
), |
| 171 |
}, |
| 172 |
_ => entity_role_filter_output(ObjectCommonFields::entities(self), *f).unwrap_or( |
| 173 |
FilterOutput { |
| 174 |
filter: *f, |
| 175 |
value: FilterValue::Null, |
| 176 |
}, |
| 177 |
), |
| 178 |
}) |
| 179 |
.collect() |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
#[cfg(test)] |
| 184 |
mod tests { |
| 185 |
use super::*; |
| 186 |
use crate::{ |
| 187 |
contact::Contact, |
| 188 |
prelude::{Email, Event}, |
| 189 |
}; |
| 190 |
|
| 191 |
fn make_test_entity() -> Entity { |
| 192 |
let contact = Contact::builder().full_name("John User").build(); |
| 193 |
|
| 194 |
Entity::builder() |
| 195 |
.handle("EntityHandle-1") |
| 196 |
.role("registrant") |
| 197 |
.contact(contact) |
| 198 |
.statuses(vec!["active".to_string()]) |
| 199 |
.event( |
| 200 |
Event::builder() |
| 201 |
.event_action("last changed") |
| 202 |
.event_date("2021-06-15T00:00:00Z") |
| 203 |
.build(), |
| 204 |
) |
| 205 |
.build() |
| 206 |
} |
| 207 |
|
| 208 |
#[test] |
| 209 |
fn filter_handle() { |
| 210 |
// GIVEN |
| 211 |
let entity = make_test_entity(); |
| 212 |
let filters = vec![Filter::Handle]; |
| 213 |
|
| 214 |
// WHEN |
| 215 |
let results = extract(&entity, &filters); |
| 216 |
|
| 217 |
// THEN |
| 218 |
assert_eq!( |
| 219 |
results[0].value, |
| 220 |
FilterValue::StringVal("EntityHandle-1".to_string()) |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn filter_object_class_name() { |
| 226 |
// GIVEN |
| 227 |
let entity = make_test_entity(); |
| 228 |
let filters = vec![Filter::ObjectClassName]; |
| 229 |
|
| 230 |
// WHEN |
| 231 |
let results = extract(&entity, &filters); |
| 232 |
|
| 233 |
// THEN |
| 234 |
assert_eq!( |
| 235 |
results[0].value, |
| 236 |
FilterValue::StringVal("entity".to_string()) |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn filter_status() { |
| 242 |
// GIVEN |
| 243 |
let entity = make_test_entity(); |
| 244 |
let filters = vec![Filter::Status]; |
| 245 |
|
| 246 |
// WHEN |
| 247 |
let results = extract(&entity, &filters); |
| 248 |
|
| 249 |
// THEN |
| 250 |
match &results[0].value { |
| 251 |
FilterValue::StringArray(s) => { |
| 252 |
assert_eq!(s.len(), 1); |
| 253 |
assert!(s.contains(&"active".to_string())); |
| 254 |
} |
| 255 |
_ => panic!("Expected StringArray"), |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
#[test] |
| 260 |
fn filter_role() { |
| 261 |
// GIVEN |
| 262 |
let entity = make_test_entity(); |
| 263 |
let filters = vec![Filter::Role]; |
| 264 |
|
| 265 |
// WHEN |
| 266 |
let results = extract(&entity, &filters); |
| 267 |
|
| 268 |
// THEN |
| 269 |
match &results[0].value { |
| 270 |
FilterValue::StringArray(s) => { |
| 271 |
assert_eq!(s.len(), 1); |
| 272 |
assert!(s.contains(&"registrant".to_string())); |
| 273 |
} |
| 274 |
_ => panic!("Expected StringArray"), |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn filter_full_name() { |
| 280 |
// GIVEN |
| 281 |
let entity = make_test_entity(); |
| 282 |
let filters = vec![Filter::FullName]; |
| 283 |
|
| 284 |
// WHEN |
| 285 |
let results = extract(&entity, &filters); |
| 286 |
|
| 287 |
// THEN |
| 288 |
assert_eq!( |
| 289 |
results[0].value, |
| 290 |
FilterValue::StringVal("John User".to_string()) |
| 291 |
); |
| 292 |
} |
| 293 |
|
| 294 |
#[test] |
| 295 |
fn filter_email() { |
| 296 |
// GIVEN |
| 297 |
let contact = Contact::builder() |
| 298 |
.full_name("John User") |
| 299 |
.emails(vec![Email::builder().email("[email protected]").build()]) |
| 300 |
.build(); |
| 301 |
let entity = Entity::builder() |
| 302 |
.handle("EntityHandle-1") |
| 303 |
.role("registrant") |
| 304 |
.contact(contact) |
| 305 |
.statuses(vec!["active".to_string()]) |
| 306 |
.event( |
| 307 |
Event::builder() |
| 308 |
.event_action("last changed") |
| 309 |
.event_date("2021-06-15T00:00:00Z") |
| 310 |
.build(), |
| 311 |
) |
| 312 |
.build(); |
| 313 |
let filters = vec![Filter::Email]; |
| 314 |
|
| 315 |
// WHEN |
| 316 |
let results = extract(&entity, &filters); |
| 317 |
|
| 318 |
// THEN |
| 319 |
assert_eq!( |
| 320 |
results[0].value, |
| 321 |
FilterValue::StringArray(vec!["[email protected]".to_string()]) |
| 322 |
); |
| 323 |
} |
| 324 |
|
| 325 |
#[test] |
| 326 |
fn filter_registrant_full_name() { |
| 327 |
// GIVEN |
| 328 |
let child_contact = Contact::builder().full_name("Child Registrant").build(); |
| 329 |
let child_entity = Entity::response_obj() |
| 330 |
.handle("CHILD-HANDLE") |
| 331 |
.role("registrant") |
| 332 |
.contact(child_contact) |
| 333 |
.build(); |
| 334 |
|
| 335 |
let parent_entity = Entity::response_obj() |
| 336 |
.handle("PARENT-HANDLE") |
| 337 |
.role("admin") |
| 338 |
.entity(child_entity) |
| 339 |
.build(); |
| 340 |
|
| 341 |
let filters = vec![Filter::RegistrantFullName]; |
| 342 |
|
| 343 |
// WHEN |
| 344 |
let results = extract(&parent_entity, &filters); |
| 345 |
|
| 346 |
// THEN |
| 347 |
assert_eq!( |
| 348 |
results[0].value, |
| 349 |
FilterValue::StringVal("Child Registrant".to_string()) |
| 350 |
); |
| 351 |
} |
| 352 |
|
| 353 |
#[test] |
| 354 |
fn filter_event() { |
| 355 |
// GIVEN |
| 356 |
let entity = make_test_entity(); |
| 357 |
let filters = vec![Filter::Event]; |
| 358 |
|
| 359 |
// WHEN |
| 360 |
let results = extract(&entity, &filters); |
| 361 |
|
| 362 |
// THEN |
| 363 |
match &results[0].value { |
| 364 |
FilterValue::HashMapVal(hm) => { |
| 365 |
assert_eq!(hm.len(), 1); |
| 366 |
assert_eq!( |
| 367 |
hm.get("last changed"), |
| 368 |
Some(&FilterValue::StringVal("2021-06-15T00:00:00Z".to_string())) |
| 369 |
); |
| 370 |
} |
| 371 |
_ => panic!("Expected HashMapVal"), |
| 372 |
} |
| 373 |
} |
| 374 |
} |