rdap 0.2.0

A modern RDAP (Registration Data Access Protocol) client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
//! Beautiful colored output for RDAP objects

use crate::models::*;
use colored::*;
use std::collections::HashMap;

/// Trait for rendering RDAP objects as colored terminal output.
///
/// Implemented for [`RdapObject`] and each concrete RDAP model type.
pub trait RdapDisplay {
    /// Print this object to stdout with ANSI-colored formatting.
    ///
    /// When `verbose` is `true`, additional details such as links, remarks,
    /// notices, and RDAP conformance extensions are included in the output.
    fn display(&self, verbose: bool);
}

impl RdapDisplay for RdapObject {
    fn display(&self, verbose: bool) {
        match self {
            RdapObject::Domain(d) => d.display(verbose),
            RdapObject::Entity(e) => e.display(verbose),
            RdapObject::Nameserver(ns) => ns.display(verbose),
            RdapObject::Autnum(a) => a.display(verbose),
            RdapObject::IpNetwork(ip) => ip.display(verbose),
            RdapObject::Error(err) => err.display(verbose),
            RdapObject::DomainSearch(ds) => ds.display(verbose),
            RdapObject::EntitySearch(es) => es.display(verbose),
            RdapObject::NameserverSearch(ns) => ns.display(verbose),
            RdapObject::Help(h) => h.display(verbose),
        }
    }
}

impl RdapDisplay for Domain {
    fn display(&self, verbose: bool) {
        // Domain name
        if let Some(name) = &self.ldh_name {
            println!(
                "{}: {}",
                "Domain Name".bright_white().bold(),
                name.bright_cyan().bold()
            );
        }

        if let Some(unicode) = &self.unicode_name {
            println!("{}: {}", "Unicode Name".white(), unicode.cyan());
        }

        if let Some(handle) = &self.handle {
            println!("{}: {}", "Handle".white(), handle.normal());
        }

        // Object class
        println!(
            "{}: {}",
            "Object Class".white(),
            self.object_class_name.normal()
        );

        // Port43
        if let Some(port43) = &self.port43 {
            println!("{}: {}", "Port43".white(), port43.normal());
        }

        // Status
        if !self.status.is_empty() {
            for status in &self.status {
                let color_status = match status.as_str() {
                    s if s.contains("active") => status.green(),
                    s if s.contains("delete") || s.contains("prohibit") => status.red(),
                    _ => status.yellow(),
                };
                println!("{}: {}", "Status".white(), color_status);
            }
        }

        // Nameservers
        if !self.nameservers.is_empty() {
            for ns in &self.nameservers {
                if let Some(name) = &ns.ldh_name {
                    print!("{}: {}", "Nameserver".white(), name.cyan());
                    if let Some(ips) = &ns.ip_addresses {
                        let addrs: Vec<String> = ips.v4.iter().chain(&ips.v6).cloned().collect();
                        if !addrs.is_empty() {
                            print!(" ({})", addrs.join(", ").dimmed());
                        }
                    }
                    println!();
                }
            }
        }

        // DNSSEC
        if let Some(dnssec) = &self.secure_dns {
            if let Some(zone_signed) = dnssec.zone_signed {
                println!(
                    "{}: {}",
                    "Zone Signed".white(),
                    if zone_signed {
                        "yes".green()
                    } else {
                        "no".red()
                    }
                );
            }
            if let Some(delegation_signed) = dnssec.delegation_signed {
                println!(
                    "{}: {}",
                    "Delegation Signed".white(),
                    if delegation_signed {
                        "yes".green()
                    } else {
                        "no".red()
                    }
                );
            }
            for ds in &dnssec.ds_data {
                if let Some(key_tag) = ds.key_tag {
                    println!("{}: {}", "DS Key Tag".white(), key_tag.to_string().normal());
                }
                if let Some(algorithm) = ds.algorithm {
                    println!(
                        "{}: {}",
                        "DS Algorithm".white(),
                        algorithm.to_string().normal()
                    );
                }
                if let Some(digest_type) = ds.digest_type {
                    println!(
                        "{}: {}",
                        "DS Digest Type".white(),
                        digest_type.to_string().normal()
                    );
                }
                if let Some(digest) = &ds.digest {
                    println!("{}: {}", "DS Digest".white(), digest.normal());
                }
            }
        }

        // Events
        for event in &self.events {
            let action = match event.action.as_str() {
                "registration" => "Registration",
                "expiration" => "Expiration",
                "last changed" => "Last Changed",
                "last update of RDAP database" => "Last Update",
                "transferred" => "Transferred",
                "locked" => "Locked",
                "unlocked" => "Unlocked",
                a => a,
            };
            println!("{}: {}", action.white(), event.date.normal());
        }

        // Entities
        if !self.entities.is_empty() {
            println!();
            for entity in &self.entities {
                display_entity(entity, verbose);
            }
        }

        // Links
        if verbose {
            for link in &self.links {
                if let Some(rel) = &link.rel {
                    println!(
                        "{}: {} ({})",
                        "Link".white(),
                        link.href.cyan(),
                        rel.dimmed()
                    );
                } else {
                    println!("{}: {}", "Link".white(), link.href.cyan());
                }
            }
        }

        // Remarks
        if verbose {
            for remark in &self.remarks {
                display_notice(remark);
            }
        }

        // Notices
        if verbose {
            for notice in &self.notices {
                display_notice(notice);
            }
        }

        // Conformance
        if verbose && !self.conformance.is_empty() {
            println!("\n{}", "RDAP Conformance:".dimmed());
            for conf in &self.conformance {
                println!("  {}", conf.dimmed());
            }
        }
    }
}

impl RdapDisplay for IpNetwork {
    fn display(&self, verbose: bool) {
        if let Some(handle) = &self.handle {
            println!("{}: {}", "Handle".white(), handle.normal());
        }

        if let (Some(start), Some(end)) = (&self.start_address, &self.end_address) {
            println!("{}: {}", "Start Address".white(), start.cyan());
            println!("{}: {}", "End Address".white(), end.cyan());
        }

        if let Some(ip_ver) = &self.ip_version {
            println!(
                "{}: {}",
                "IP Version".white(),
                format!("v{}", ip_ver).normal()
            );
        }

        if let Some(name) = &self.name {
            println!("{}: {}", "Name".white(), name.cyan());
        }

        if let Some(net_type) = &self.network_type {
            println!("{}: {}", "Type".white(), net_type.normal());
        }

        if let Some(parent) = &self.parent_handle {
            println!("{}: {}", "Parent Handle".white(), parent.normal());
        }

        if let Some(country) = &self.country {
            println!("{}: {}", "Country".white(), country.green());
        }

        // Status
        for status in &self.status {
            println!("{}: {}", "Status".white(), status.green());
        }

        // Port43
        if let Some(port43) = &self.port43 {
            println!("{}: {}", "Port43".white(), port43.normal());
        }

        // Events
        for event in &self.events {
            println!("{}: {}", event.action.white(), event.date.normal());
        }

        // Entities - deduplicate and display
        if !self.entities.is_empty() {
            println!();
            let entity_map = collect_entities(&self.entities);
            let mut handles: Vec<_> = entity_map.keys().collect();
            handles.sort();

            for handle in handles {
                if let Some(entity) = entity_map.get(handle) {
                    display_entity(entity, verbose);
                    println!();
                }
            }
        }

        // Links, Remarks, Notices
        if verbose {
            for link in &self.links {
                println!("{}: {}", "Link".white(), link.href.cyan());
            }
            for remark in &self.remarks {
                display_notice(remark);
            }
            for notice in &self.notices {
                display_notice(notice);
            }
        }
    }
}

impl RdapDisplay for Autnum {
    fn display(&self, verbose: bool) {
        // AS Number
        if let (Some(start), Some(end)) = (self.start_autnum, self.end_autnum) {
            if start == end {
                println!(
                    "{}: {}",
                    "AS Number".white(),
                    format!("AS{}", start).cyan().bold()
                );
            } else {
                println!(
                    "{}: {}",
                    "Start Autnum".white(),
                    format!("AS{}", start).cyan()
                );
                println!("{}: {}", "End Autnum".white(), format!("AS{}", end).cyan());
            }
        }

        if let Some(name) = &self.name {
            println!("{}: {}", "Name".white(), name.cyan());
        }

        if let Some(handle) = &self.handle {
            println!("{}: {}", "Handle".white(), handle.normal());
        }

        // Object class
        if let Some(class) = &self.object_class_name {
            println!("{}: {}", "Object Class".white(), class.normal());
        }

        if let Some(as_type) = &self.as_type {
            println!("{}: {}", "Type".white(), as_type.normal());
        }

        if let Some(country) = &self.country {
            println!("{}: {}", "Country".white(), country.green());
        }

        // Status
        for status in &self.status {
            println!("{}: {}", "Status".white(), status.green());
        }

        // Port43
        if let Some(port43) = &self.port43 {
            println!("{}: {}", "Port43".white(), port43.normal());
        }

        // Events
        for event in &self.events {
            let action = match event.action.as_str() {
                "registration" => "Registration",
                "last changed" => "Last Changed",
                a => a,
            };
            println!("{}: {}", action.white(), event.date.normal());
        }

        // Entities - deduplicate and display
        if !self.entities.is_empty() {
            println!();
            let entity_map = collect_entities(&self.entities);
            let mut handles: Vec<_> = entity_map.keys().collect();
            handles.sort();

            for handle in handles {
                if let Some(entity) = entity_map.get(handle) {
                    display_entity(entity, verbose);
                    println!();
                }
            }
        }

        // Links, Remarks, Notices
        if verbose {
            for link in &self.links {
                if let Some(rel) = &link.rel {
                    println!(
                        "{}: {} ({})",
                        "Link".white(),
                        link.href.cyan(),
                        rel.dimmed()
                    );
                } else {
                    println!("{}: {}", "Link".white(), link.href.cyan());
                }
            }
            for remark in &self.remarks {
                display_notice(remark);
            }
            for notice in &self.notices {
                display_notice(notice);
            }
        }

        // Conformance
        if verbose && !self.conformance.is_empty() {
            println!("\n{}", "RDAP Conformance:".dimmed());
            for conf in &self.conformance {
                println!("  {}", conf.dimmed());
            }
        }
    }
}

impl RdapDisplay for Entity {
    fn display(&self, verbose: bool) {
        // If this entity has nested entities, collect and deduplicate them
        if !self.entities.is_empty() {
            // Display main entity first
            display_entity(self, verbose);
            println!();

            // Collect and display all nested entities
            let entity_map = collect_entities(&self.entities);
            let mut handles: Vec<_> = entity_map.keys().collect();
            handles.sort();

            for handle in handles {
                if let Some(entity) = entity_map.get(handle) {
                    display_entity(entity, verbose);
                    println!();
                }
            }
        } else {
            // No nested entities, just display this one
            display_entity(self, verbose);
        }

        // Display notices (for top-level entity response)
        if verbose && !self.notices.is_empty() {
            for notice in &self.notices {
                display_notice(notice);
            }
        }
    }
}

impl RdapDisplay for Nameserver {
    fn display(&self, verbose: bool) {
        if let Some(name) = &self.ldh_name {
            println!("{}: {}", "Nameserver".white(), name.cyan().bold());
        }

        if let Some(handle) = &self.handle {
            println!("{}: {}", "Handle".white(), handle.normal());
        }

        if let Some(ips) = &self.ip_addresses {
            for ip in &ips.v4 {
                println!("{}: {}", "IPv4".white(), ip.cyan());
            }
            for ip in &ips.v6 {
                println!("{}: {}", "IPv6".white(), ip.cyan());
            }
        }

        // Status
        for status in &self.status {
            println!("{}: {}", "Status".white(), status.green());
        }

        // Events
        for event in &self.events {
            println!("{}: {}", event.action.white(), event.date.normal());
        }

        // Entities - deduplicate and display
        if !self.entities.is_empty() {
            println!();
            let entity_map = collect_entities(&self.entities);
            let mut handles: Vec<_> = entity_map.keys().collect();
            handles.sort();

            for handle in handles {
                if let Some(entity) = entity_map.get(handle) {
                    display_entity(entity, verbose);
                    println!();
                }
            }
        }

        if verbose {
            for link in &self.links {
                println!("{}: {}", "Link".white(), link.href.cyan());
            }
            for remark in &self.remarks {
                display_notice(remark);
            }
            for notice in &self.notices {
                display_notice(notice);
            }
        }
    }
}

impl RdapDisplay for ErrorResponse {
    fn display(&self, _verbose: bool) {
        if let Some(code) = self.error_code {
            println!("{}: {}", "Error Code".red(), code.to_string().red().bold());
        }

        if let Some(title) = &self.title {
            println!("{}: {}", "Title".white(), title.normal());
        }

        for desc in &self.description {
            println!("{}: {}", "Description".white(), desc.normal());
        }

        for notice in &self.notices {
            display_notice(notice);
        }
    }
}

impl RdapDisplay for DomainSearchResults {
    fn display(&self, verbose: bool) {
        println!(
            "{}: {}",
            "Domain Search Results".white(),
            self.domains.len().to_string().cyan()
        );
        println!();

        for (i, domain) in self.domains.iter().enumerate() {
            if i > 0 {
                println!("\n{}", "---".dimmed());
            }
            domain.display(verbose);
        }
    }
}

impl RdapDisplay for EntitySearchResults {
    fn display(&self, verbose: bool) {
        println!(
            "{}: {}",
            "Entity Search Results".white(),
            self.entities.len().to_string().cyan()
        );
        println!();

        for (i, entity) in self.entities.iter().enumerate() {
            if i > 0 {
                println!("\n{}", "---".dimmed());
            }
            display_entity(entity, verbose);
        }
    }
}

impl RdapDisplay for NameserverSearchResults {
    fn display(&self, verbose: bool) {
        println!(
            "{}: {}",
            "Nameserver Search Results".white(),
            self.nameservers.len().to_string().cyan()
        );
        println!();

        for (i, ns) in self.nameservers.iter().enumerate() {
            if i > 0 {
                println!("\n{}", "---".dimmed());
            }
            ns.display(verbose);
        }
    }
}

impl RdapDisplay for HelpResponse {
    fn display(&self, _verbose: bool) {
        for notice in &self.notices {
            display_notice(notice);
        }
    }
}

// Helper functions

/// Collect all entities (including nested ones) into a flat map, deduplicating
/// by handle and merging roles, events, links, and status from duplicates.
pub fn collect_entities(entities: &[Entity]) -> HashMap<String, Entity> {
    let mut entity_map: HashMap<String, Entity> = HashMap::new();

    fn collect_recursive(entities: &[Entity], map: &mut HashMap<String, Entity>) {
        for entity in entities {
            if let Some(handle) = &entity.handle {
                // Merge with existing or insert new
                map.entry(handle.clone())
                    .and_modify(|existing| {
                        // Merge roles
                        for role in &entity.roles {
                            if !existing.roles.contains(role) {
                                existing.roles.push(role.clone());
                            }
                        }
                        // Update vcard if missing
                        if existing.vcard.is_none() && entity.vcard.is_some() {
                            existing.vcard = entity.vcard.clone();
                        }
                        // Merge other fields if needed
                        if existing.port43.is_none() {
                            existing.port43 = entity.port43.clone();
                        }
                        for event in &entity.events {
                            if !existing
                                .events
                                .iter()
                                .any(|e| e.action == event.action && e.date == event.date)
                            {
                                existing.events.push(event.clone());
                            }
                        }
                        for link in &entity.links {
                            if !existing.links.iter().any(|l| l.href == link.href) {
                                existing.links.push(link.clone());
                            }
                        }
                        for status in &entity.status {
                            if !existing.status.contains(status) {
                                existing.status.push(status.clone());
                            }
                        }
                    })
                    .or_insert_with(|| entity.clone());

                // Recursively collect nested entities
                collect_recursive(&entity.entities, map);
            }
        }
    }

    collect_recursive(entities, &mut entity_map);
    entity_map
}

fn display_entity(entity: &Entity, verbose: bool) {
    // Entity header
    if let Some(handle) = &entity.handle {
        println!("{}: {}", "Entity Handle".white(), handle.normal());
    }

    if !entity.roles.is_empty() {
        for role in &entity.roles {
            println!("{}: {}", "Role".white(), role.yellow());
        }
    }

    // vCard information
    if let Some(vcard) = &entity.vcard {
        if let Some(name) = vcard.name() {
            println!("{}: {}", "Name".white(), name.cyan());
        }
        if let Some(org) = vcard.org() {
            println!("{}: {}", "Organization".white(), org.normal());
        }
        if let Some(email) = vcard.email() {
            println!("{}: {}", "Email".white(), email.cyan());
        }
        if let Some(tel) = vcard.tel() {
            println!("{}: {}", "Phone".white(), tel.normal());
        }

        if let Some(addr) = vcard.address() {
            // If there's a pre-formatted label, use that
            if let Some(label) = &addr.label {
                println!("{}: {}", "Address".white(), label.normal());
            } else {
                // Otherwise, show individual components
                if !addr.po_box.is_empty() {
                    println!("{}: {}", "PO Box".white(), addr.po_box.normal());
                }
                if !addr.extended.is_empty() {
                    println!("{}: {}", "Extended Address".white(), addr.extended.normal());
                }
                if !addr.street.is_empty() {
                    println!("{}: {}", "Street".white(), addr.street.normal());
                }
                if !addr.locality.is_empty() {
                    println!("{}: {}", "Locality".white(), addr.locality.normal());
                }
                if !addr.region.is_empty() {
                    println!("{}: {}", "Region".white(), addr.region.normal());
                }
                if !addr.postal_code.is_empty() {
                    println!("{}: {}", "Postal Code".white(), addr.postal_code.normal());
                }
                if !addr.country.is_empty() {
                    println!("{}: {}", "Country".white(), addr.country.green());
                }
            }
        }
    }

    // Status
    for status in &entity.status {
        println!("{}: {}", "Status".white(), status.green());
    }

    // Port43
    if let Some(port43) = &entity.port43 {
        println!("{}: {}", "Port43".white(), port43.normal());
    }

    // Events
    for event in &entity.events {
        println!("{}: {}", event.action.white(), event.date.normal());
    }

    // Public IDs
    for public_id in &entity.public_ids {
        println!(
            "{}: {}",
            public_id.id_type.white(),
            public_id.identifier.cyan()
        );
    }

    // Links (always show self link)
    for link in &entity.links {
        if let Some(rel) = &link.rel
            && rel == "self"
        {
            println!("{}: {}", "Link".dimmed(), link.href.cyan());
        }
    }

    // More details in verbose mode
    if verbose {
        for link in &entity.links {
            if let Some(rel) = &link.rel
                && rel != "self"
            {
                println!(
                    "{}: {} ({})",
                    "Link".white(),
                    link.href.cyan(),
                    rel.dimmed()
                );
            }
        }
        for remark in &entity.remarks {
            display_notice(remark);
        }
    }
}

fn display_notice(notice: &Notice) {
    if let Some(title) = &notice.title {
        println!("{}: {}", "Notice".white(), title.cyan());
    }
    for desc in &notice.description {
        println!("  {}", desc.normal());
    }
    for link in &notice.links {
        println!("  {}: {}", "Link".dimmed(), link.href.cyan());
    }
}