Debian Codegraph @

src/filter/ip_search_results.rs

raw
1 //! IP search results filter implementation.
2 //!
3 //! Extracts fields from [`crate::response::IpSearchResults`] RDAP objects.
4 //! Values are aggregated across all search results into arrays.
5 //!
6 //! # Supported Filters
7 //!
8 //! | Filter | Value Type | Description |
9 //! |---|---|---|
10 //! | `Handle` | `StringArray` | Handles from all results |
11 //! | `Status` | `StringArray` | Statuses from all results |
12 //! | `ObjectClassName` | `StringArray` | Object class names from all results |
13 //! | `Event` | `HashMapVal` | Events aggregated from all results |
14 //! | `RdapConformance` | `StringArray` | Conformance URIs from all results |
15 //! | `Cidr` | `StringArray` | CIDR entries from all results |
16 //! | `StartIpAddress` | `StringArray` | Start IP addresses from all results |
17 //! | `EndIpAddress` | `StringArray` | End IP addresses from all results |
18 //! | `IpVersion` | `StringArray` | IP versions from all results |
19 //! | `Name` | `StringArray` | Network names from all results |
20 //! | `Type` | `StringArray` | Network types from all results |
21 //! | `ParentHandle` | `StringArray` | Parent handles from all results |
22 //! | Entity roles | `StringArray` | Role-based fields aggregated from all results |
23
24 use super::*;
25 use crate::response::{CommonFields, IpSearchResults, ObjectCommonFields};
26
27 impl Filterable for IpSearchResults {
28 fn filter(&self, filters: &[Filter]) -> FilterResult {
29 filters
30 .iter()
31 .map(|f| match f {
32 Filter::Handle => FilterOutput {
33 filter: *f,
34 value: FilterValue::StringArray(
35 self.results()
36 .iter()
37 .filter_map(|n| n.handle())
38 .map(|h| h.to_string())
39 .collect(),
40 ),
41 },
42 Filter::Status => FilterOutput {
43 filter: *f,
44 value: FilterValue::StringArray(
45 self.results()
46 .iter()
47 .flat_map(|n| n.status())
48 .map(|s| s.to_string())
49 .collect(),
50 ),
51 },
52 Filter::ObjectClassName => FilterOutput {
53 filter: *f,
54 value: FilterValue::StringArray(
55 self.results()
56 .iter()
57 .map(|n| n.object_class_name().to_string())
58 .collect(),
59 ),
60 },
61 Filter::Event => FilterOutput {
62 filter: *f,
63 value: FilterValue::HashMapVal(
64 self.results()
65 .iter()
66 .flat_map(|n| n.events())
67 .filter_map(|e| {
68 let action = e.event_action()?;
69 let date = e.event_date()?;
70 Some((action.to_string(), FilterValue::StringVal(date.to_string())))
71 })
72 .collect(),
73 ),
74 },
75 Filter::RdapConformance => FilterOutput {
76 filter: *f,
77 value: FilterValue::StringArray(
78 self.results()
79 .iter()
80 .filter_map(|n| n.common().rdap_conformance.as_ref())
81 .flatten()
82 .map(|ext| ext.0.clone())
83 .collect(),
84 ),
85 },
86 Filter::Cidr => FilterOutput {
87 filter: *f,
88 value: FilterValue::StringArray(
89 self.results()
90 .iter()
91 .flat_map(|n| {
92 n.cidr0_cidrs().iter().filter_map(|c| {
93 let prefix = c.prefix()?;
94 let length = c.length()?;
95 Some(format!("{}/{}", prefix, length))
96 })
97 })
98 .collect(),
99 ),
100 },
101 Filter::StartIpAddress => FilterOutput {
102 filter: *f,
103 value: FilterValue::StringArray(
104 self.results()
105 .iter()
106 .filter_map(|n| n.start_address())
107 .map(|a| a.to_string())
108 .collect(),
109 ),
110 },
111 Filter::EndIpAddress => FilterOutput {
112 filter: *f,
113 value: FilterValue::StringArray(
114 self.results()
115 .iter()
116 .filter_map(|n| n.end_address())
117 .map(|a| a.to_string())
118 .collect(),
119 ),
120 },
121 Filter::IpVersion => FilterOutput {
122 filter: *f,
123 value: FilterValue::StringArray(
124 self.results()
125 .iter()
126 .filter_map(|n| n.ip_version())
127 .map(|v| v.to_string())
128 .collect(),
129 ),
130 },
131 Filter::Name => FilterOutput {
132 filter: *f,
133 value: FilterValue::StringArray(
134 self.results()
135 .iter()
136 .filter_map(|n| n.name())
137 .map(|n| n.to_string())
138 .collect(),
139 ),
140 },
141 Filter::Type => FilterOutput {
142 filter: *f,
143 value: FilterValue::StringArray(
144 self.results()
145 .iter()
146 .filter_map(|n| n.network_type())
147 .map(|t| t.to_string())
148 .collect(),
149 ),
150 },
151 Filter::ParentHandle => FilterOutput {
152 filter: *f,
153 value: FilterValue::StringArray(
154 self.results()
155 .iter()
156 .filter_map(|n| n.parent_handle())
157 .map(|h| h.to_string())
158 .collect(),
159 ),
160 },
161 _ => entity_role_filter_output_search(self.results().iter(), *f),
162 })
163 .collect()
164 }
165 }
166
167 #[cfg(test)]
168 mod tests {
169 use super::*;
170 use crate::contact::Contact;
171 use crate::prelude::{Email, Event, ExtensionId, Network};
172 use crate::response::Entity as EntityType;
173
174 fn make_test_network_1() -> Network {
175 let registrant_contact = Contact::builder()
176 .full_name("Network Registrant One")
177 .emails(vec![
178 Email::builder().email("[email protected]").build(),
179 ])
180 .build();
181 let abuse_contact = Contact::builder()
182 .full_name("Network Abuse One")
183 .emails(vec![Email::builder().email("[email protected]").build()])
184 .build();
185 let tech_contact = Contact::builder()
186 .full_name("Network Tech One")
187 .emails(vec![Email::builder().email("[email protected]").build()])
188 .build();
189 let registrar_contact = Contact::builder()
190 .full_name("Network Registrar One")
191 .emails(vec![
192 Email::builder().email("[email protected]").build(),
193 ])
194 .build();
195
196 Network::builder()
197 .cidr("192.0.2.0/24")
198 .handle("NET-192-0-2-0-1")
199 .name("EXAMPLE-NET-1")
200 .network_type("ASSIGNED PORTABLE PREFIX")
201 .parent_handle("TOP-LEVEL-NET")
202 .status("active")
203 .entity(
204 EntityType::response_obj()
205 .handle("REGISTRANT-HANDLE-1")
206 .role("registrant")
207 .contact(registrant_contact)
208 .build(),
209 )
210 .entity(
211 EntityType::response_obj()
212 .handle("ABUSE-HANDLE-1")
213 .role("abuse")
214 .contact(abuse_contact)
215 .build(),
216 )
217 .entity(
218 EntityType::response_obj()
219 .handle("TECH-HANDLE-1")
220 .role("technical")
221 .contact(tech_contact)
222 .build(),
223 )
224 .entity(
225 EntityType::response_obj()
226 .handle("REGISTRAR-HANDLE-1")
227 .role("registrar")
228 .contact(registrar_contact)
229 .build(),
230 )
231 .event(
232 Event::builder()
233 .event_action("registration")
234 .event_date("2020-01-01T00:00:00Z")
235 .build(),
236 )
237 .build()
238 .unwrap()
239 }
240
241 fn make_test_network_2() -> Network {
242 let registrant_contact = Contact::builder()
243 .full_name("Network Registrant Two")
244 .emails(vec![
245 Email::builder().email("[email protected]").build(),
246 ])
247 .build();
248 let abuse_contact = Contact::builder()
249 .full_name("Network Abuse Two")
250 .emails(vec![Email::builder().email("[email protected]").build()])
251 .build();
252 let tech_contact = Contact::builder()
253 .full_name("Network Tech Two")
254 .emails(vec![Email::builder().email("[email protected]").build()])
255 .build();
256 let registrar_contact = Contact::builder()
257 .full_name("Network Registrar Two")
258 .emails(vec![
259 Email::builder().email("[email protected]").build(),
260 ])
261 .build();
262
263 Network::builder()
264 .cidr("198.51.100.0/24")
265 .handle("NET-198-51-100-1")
266 .name("EXAMPLE-NET-2")
267 .network_type("ASSIGNED PORTABLE PREFIX")
268 .parent_handle("TOP-LEVEL-NET")
269 .status("active")
270 .entity(
271 EntityType::response_obj()
272 .handle("REGISTRANT-HANDLE-2")
273 .role("registrant")
274 .contact(registrant_contact)
275 .build(),
276 )
277 .entity(
278 EntityType::response_obj()
279 .handle("ABUSE-HANDLE-2")
280 .role("abuse")
281 .contact(abuse_contact)
282 .build(),
283 )
284 .entity(
285 EntityType::response_obj()
286 .handle("TECH-HANDLE-2")
287 .role("technical")
288 .contact(tech_contact)
289 .build(),
290 )
291 .entity(
292 EntityType::response_obj()
293 .handle("REGISTRAR-HANDLE-2")
294 .role("registrar")
295 .contact(registrar_contact)
296 .build(),
297 )
298 .event(
299 Event::builder()
300 .event_action("last changed")
301 .event_date("2023-06-15T12:00:00Z")
302 .build(),
303 )
304 .build()
305 .unwrap()
306 }
307
308 fn make_test_ip_search_results() -> IpSearchResults {
309 let extensions = vec![
310 ExtensionId::RdapLevel0.to_extension(),
311 ExtensionId::IpSearchResults.to_extension(),
312 ];
313 IpSearchResults::response_obj()
314 .results(vec![make_test_network_1(), make_test_network_2()])
315 .extensions(extensions)
316 .build()
317 }
318
319 #[test]
320 fn filter_handle() {
321 // GIVEN
322 let search_results = make_test_ip_search_results();
323 let filters = vec![Filter::Handle];
324
325 // WHEN
326 let results = extract(&search_results, &filters);
327
328 // THEN
329 assert_eq!(results.len(), 1);
330 assert_eq!(results[0].filter, Filter::Handle);
331 match &results[0].value {
332 FilterValue::StringArray(s) => {
333 assert_eq!(s.len(), 2);
334 assert!(s.contains(&"NET-192-0-2-0-1".to_string()));
335 assert!(s.contains(&"NET-198-51-100-1".to_string()));
336 }
337 _ => panic!("Expected StringArray"),
338 }
339 }
340
341 #[test]
342 fn filter_status() {
343 // GIVEN
344 let search_results = make_test_ip_search_results();
345 let filters = vec![Filter::Status];
346
347 // WHEN
348 let results = extract(&search_results, &filters);
349
350 // THEN
351 assert_eq!(results.len(), 1);
352 assert_eq!(results[0].filter, Filter::Status);
353 match &results[0].value {
354 FilterValue::StringArray(s) => {
355 assert_eq!(s.len(), 2);
356 assert!(s.contains(&"active".to_string()));
357 }
358 _ => panic!("Expected StringArray"),
359 }
360 }
361
362 #[test]
363 fn filter_object_class_name() {
364 // GIVEN
365 let search_results = make_test_ip_search_results();
366 let filters = vec![Filter::ObjectClassName];
367
368 // WHEN
369 let results = extract(&search_results, &filters);
370
371 // THEN
372 assert_eq!(results.len(), 1);
373 assert_eq!(results[0].filter, Filter::ObjectClassName);
374 match &results[0].value {
375 FilterValue::StringArray(s) => {
376 assert_eq!(s.len(), 2);
377 assert!(s.contains(&"ip network".to_string()));
378 }
379 _ => panic!("Expected StringArray"),
380 }
381 }
382
383 #[test]
384 fn filter_event() {
385 // GIVEN
386 let search_results = make_test_ip_search_results();
387 let filters = vec![Filter::Event];
388
389 // WHEN
390 let results = extract(&search_results, &filters);
391
392 // THEN
393 assert_eq!(results.len(), 1);
394 assert_eq!(results[0].filter, Filter::Event);
395 match &results[0].value {
396 FilterValue::HashMapVal(hm) => {
397 assert_eq!(hm.len(), 2);
398 assert!(hm.contains_key("registration"));
399 assert!(hm.contains_key("last changed"));
400 }
401 _ => panic!("Expected HashMapVal"),
402 }
403 }
404
405 #[test]
406 fn filter_rdap_conformance() {
407 // GIVEN
408 let search_results = make_test_ip_search_results();
409 let filters = vec![Filter::RdapConformance];
410
411 // WHEN
412 let results = extract(&search_results, &filters);
413
414 // THEN
415 assert_eq!(results.len(), 1);
416 assert_eq!(results[0].filter, Filter::RdapConformance);
417 // Network::builder() uses Common::builder() which sets rdap_conformance to None,
418 // so the result is an empty array (no rdap_conformance on individual Network objects)
419 match &results[0].value {
420 FilterValue::StringArray(s) => {
421 // Empty because Network::builder() doesn't set rdap_conformance
422 assert_eq!(s.len(), 0);
423 }
424 _ => panic!("Expected StringArray"),
425 }
426 }
427
428 #[test]
429 fn filter_cidr() {
430 // GIVEN
431 let search_results = make_test_ip_search_results();
432 let filters = vec![Filter::Cidr];
433
434 // WHEN
435 let results = extract(&search_results, &filters);
436
437 // THEN
438 assert_eq!(results.len(), 1);
439 assert_eq!(results[0].filter, Filter::Cidr);
440 match &results[0].value {
441 FilterValue::StringArray(s) => {
442 assert_eq!(s.len(), 2);
443 assert!(s.contains(&"192.0.2.0/24".to_string()));
444 assert!(s.contains(&"198.51.100.0/24".to_string()));
445 }
446 _ => panic!("Expected StringArray"),
447 }
448 }
449
450 #[test]
451 fn filter_start_ip_address() {
452 // GIVEN
453 let search_results = make_test_ip_search_results();
454 let filters = vec![Filter::StartIpAddress];
455
456 // WHEN
457 let results = extract(&search_results, &filters);
458
459 // THEN
460 assert_eq!(results.len(), 1);
461 assert_eq!(results[0].filter, Filter::StartIpAddress);
462 match &results[0].value {
463 FilterValue::StringArray(s) => {
464 assert_eq!(s.len(), 2);
465 assert!(s.contains(&"192.0.2.0".to_string()));
466 assert!(s.contains(&"198.51.100.0".to_string()));
467 }
468 _ => panic!("Expected StringArray"),
469 }
470 }
471
472 #[test]
473 fn filter_end_ip_address() {
474 // GIVEN
475 let search_results = make_test_ip_search_results();
476 let filters = vec![Filter::EndIpAddress];
477
478 // WHEN
479 let results = extract(&search_results, &filters);
480
481 // THEN
482 assert_eq!(results.len(), 1);
483 assert_eq!(results[0].filter, Filter::EndIpAddress);
484 match &results[0].value {
485 FilterValue::StringArray(s) => {
486 assert_eq!(s.len(), 2);
487 assert!(s.contains(&"192.0.2.255".to_string()));
488 assert!(s.contains(&"198.51.100.255".to_string()));
489 }
490 _ => panic!("Expected StringArray"),
491 }
492 }
493
494 #[test]
495 fn filter_ip_version() {
496 // GIVEN
497 let search_results = make_test_ip_search_results();
498 let filters = vec![Filter::IpVersion];
499
500 // WHEN
501 let results = extract(&search_results, &filters);
502
503 // THEN
504 assert_eq!(results.len(), 1);
505 assert_eq!(results[0].filter, Filter::IpVersion);
506 match &results[0].value {
507 FilterValue::StringArray(s) => {
508 assert_eq!(s, &vec!["v4".to_string(), "v4".to_string()]);
509 }
510 _ => panic!("Expected StringArray"),
511 }
512 }
513
514 #[test]
515 fn filter_name() {
516 // GIVEN
517 let search_results = make_test_ip_search_results();
518 let filters = vec![Filter::Name];
519
520 // WHEN
521 let results = extract(&search_results, &filters);
522
523 // THEN
524 assert_eq!(results.len(), 1);
525 assert_eq!(results[0].filter, Filter::Name);
526 match &results[0].value {
527 FilterValue::StringArray(s) => {
528 assert_eq!(s.len(), 2);
529 assert!(s.contains(&"EXAMPLE-NET-1".to_string()));
530 assert!(s.contains(&"EXAMPLE-NET-2".to_string()));
531 }
532 _ => panic!("Expected StringArray"),
533 }
534 }
535
536 #[test]
537 fn filter_type() {
538 // GIVEN
539 let search_results = make_test_ip_search_results();
540 let filters = vec![Filter::Type];
541
542 // WHEN
543 let results = extract(&search_results, &filters);
544
545 // THEN
546 assert_eq!(results.len(), 1);
547 assert_eq!(results[0].filter, Filter::Type);
548 match &results[0].value {
549 FilterValue::StringArray(s) => {
550 assert_eq!(s.len(), 2);
551 assert!(s.iter().all(|t| t == "ASSIGNED PORTABLE PREFIX"));
552 }
553 _ => panic!("Expected StringArray"),
554 }
555 }
556
557 #[test]
558 fn filter_parent_handle() {
559 // GIVEN
560 let search_results = make_test_ip_search_results();
561 let filters = vec![Filter::ParentHandle];
562
563 // WHEN
564 let results = extract(&search_results, &filters);
565
566 // THEN
567 assert_eq!(results.len(), 1);
568 assert_eq!(results[0].filter, Filter::ParentHandle);
569 match &results[0].value {
570 FilterValue::StringArray(s) => {
571 assert_eq!(s.len(), 2);
572 assert!(s.iter().all(|h| h == "TOP-LEVEL-NET"));
573 }
574 _ => panic!("Expected StringArray"),
575 }
576 }
577
578 #[test]
579 fn filter_registrant_email() {
580 // GIVEN
581 let search_results = make_test_ip_search_results();
582 let filters = vec![Filter::RegistrantEmail];
583
584 // WHEN
585 let results = extract(&search_results, &filters);
586
587 // THEN
588 assert_eq!(results.len(), 1);
589 assert_eq!(results[0].filter, Filter::RegistrantEmail);
590 match &results[0].value {
591 FilterValue::StringArray(s) => {
592 assert_eq!(s.len(), 2);
593 assert!(s.contains(&"[email protected]".to_string()));
594 assert!(s.contains(&"[email protected]".to_string()));
595 }
596 _ => panic!("Expected StringArray"),
597 }
598 }
599
600 #[test]
601 fn filter_registrant_full_name() {
602 // GIVEN
603 let search_results = make_test_ip_search_results();
604 let filters = vec![Filter::RegistrantFullName];
605
606 // WHEN
607 let results = extract(&search_results, &filters);
608
609 // THEN
610 assert_eq!(results.len(), 1);
611 assert_eq!(results[0].filter, Filter::RegistrantFullName);
612 match &results[0].value {
613 FilterValue::StringArray(s) => {
614 assert_eq!(s.len(), 2);
615 assert!(s.contains(&"Network Registrant One".to_string()));
616 assert!(s.contains(&"Network Registrant Two".to_string()));
617 }
618 _ => panic!("Expected StringArray"),
619 }
620 }
621
622 #[test]
623 fn filter_abuse_email() {
624 // GIVEN
625 let search_results = make_test_ip_search_results();
626 let filters = vec![Filter::AbuseEmail];
627
628 // WHEN
629 let results = extract(&search_results, &filters);
630
631 // THEN
632 assert_eq!(results.len(), 1);
633 assert_eq!(results[0].filter, Filter::AbuseEmail);
634 match &results[0].value {
635 FilterValue::StringArray(s) => {
636 assert_eq!(s.len(), 2);
637 assert!(s.contains(&"[email protected]".to_string()));
638 assert!(s.contains(&"[email protected]".to_string()));
639 }
640 _ => panic!("Expected StringArray"),
641 }
642 }
643
644 #[test]
645 fn filter_abuse_full_name() {
646 // GIVEN
647 let search_results = make_test_ip_search_results();
648 let filters = vec![Filter::AbuseFullName];
649
650 // WHEN
651 let results = extract(&search_results, &filters);
652
653 // THEN
654 assert_eq!(results.len(), 1);
655 assert_eq!(results[0].filter, Filter::AbuseFullName);
656 match &results[0].value {
657 FilterValue::StringArray(s) => {
658 assert_eq!(s.len(), 2);
659 assert!(s.contains(&"Network Abuse One".to_string()));
660 assert!(s.contains(&"Network Abuse Two".to_string()));
661 }
662 _ => panic!("Expected StringArray"),
663 }
664 }
665
666 #[test]
667 fn filter_technical_email() {
668 // GIVEN
669 let search_results = make_test_ip_search_results();
670 let filters = vec![Filter::TechnicalEmail];
671
672 // WHEN
673 let results = extract(&search_results, &filters);
674
675 // THEN
676 assert_eq!(results.len(), 1);
677 assert_eq!(results[0].filter, Filter::TechnicalEmail);
678 match &results[0].value {
679 FilterValue::StringArray(s) => {
680 assert_eq!(s.len(), 2);
681 assert!(s.contains(&"[email protected]".to_string()));
682 assert!(s.contains(&"[email protected]".to_string()));
683 }
684 _ => panic!("Expected StringArray"),
685 }
686 }
687
688 #[test]
689 fn filter_technical_full_name() {
690 // GIVEN
691 let search_results = make_test_ip_search_results();
692 let filters = vec![Filter::TechnicalFullName];
693
694 // WHEN
695 let results = extract(&search_results, &filters);
696
697 // THEN
698 assert_eq!(results.len(), 1);
699 assert_eq!(results[0].filter, Filter::TechnicalFullName);
700 match &results[0].value {
701 FilterValue::StringArray(s) => {
702 assert_eq!(s.len(), 2);
703 assert!(s.contains(&"Network Tech One".to_string()));
704 assert!(s.contains(&"Network Tech Two".to_string()));
705 }
706 _ => panic!("Expected StringArray"),
707 }
708 }
709
710 #[test]
711 fn filter_registrar_email() {
712 // GIVEN
713 let search_results = make_test_ip_search_results();
714 let filters = vec![Filter::RegistrarEmail];
715
716 // WHEN
717 let results = extract(&search_results, &filters);
718
719 // THEN
720 assert_eq!(results.len(), 1);
721 assert_eq!(results[0].filter, Filter::RegistrarEmail);
722 match &results[0].value {
723 FilterValue::StringArray(s) => {
724 assert_eq!(s.len(), 2);
725 assert!(s.contains(&"[email protected]".to_string()));
726 assert!(s.contains(&"[email protected]".to_string()));
727 }
728 _ => panic!("Expected StringArray"),
729 }
730 }
731
732 #[test]
733 fn filter_registrar_full_name() {
734 // GIVEN
735 let search_results = make_test_ip_search_results();
736 let filters = vec![Filter::RegistrarFullName];
737
738 // WHEN
739 let results = extract(&search_results, &filters);
740
741 // THEN
742 assert_eq!(results.len(), 1);
743 assert_eq!(results[0].filter, Filter::RegistrarFullName);
744 match &results[0].value {
745 FilterValue::StringArray(s) => {
746 assert_eq!(s.len(), 2);
747 assert!(s.contains(&"Network Registrar One".to_string()));
748 assert!(s.contains(&"Network Registrar Two".to_string()));
749 }
750 _ => panic!("Expected StringArray"),
751 }
752 }
753
754 #[test]
755 fn filter_unknown_returns_null() {
756 // GIVEN
757 let search_results = make_test_ip_search_results();
758 let filters = vec![Filter::LdhName];
759
760 // WHEN
761 let results = extract(&search_results, &filters);
762
763 // THEN
764 assert_eq!(results.len(), 1);
765 assert_eq!(results[0].filter, Filter::LdhName);
766 assert_eq!(results[0].value, FilterValue::Null);
767 }
768
769 #[test]
770 fn filter_multiple_filters_at_once() {
771 // GIVEN
772 let search_results = make_test_ip_search_results();
773 let filters = vec![
774 Filter::Handle,
775 Filter::Status,
776 Filter::ObjectClassName,
777 Filter::Cidr,
778 Filter::RegistrantEmail,
779 Filter::AbuseEmail,
780 ];
781
782 // WHEN
783 let results = extract(&search_results, &filters);
784
785 // THEN
786 assert_eq!(results.len(), 6);
787 assert_eq!(results[0].filter, Filter::Handle);
788 assert_eq!(results[1].filter, Filter::Status);
789 assert_eq!(results[2].filter, Filter::ObjectClassName);
790 assert_eq!(results[3].filter, Filter::Cidr);
791 assert_eq!(results[4].filter, Filter::RegistrantEmail);
792 assert_eq!(results[5].filter, Filter::AbuseEmail);
793 }
794 }