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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
//! WHOIS-style output formatter for RDAP objects
//!
//! Converts RDAP responses into traditional WHOIS/RPSL-style text output,
//! producing familiar key-value formatted objects similar to those returned
//! by whois(1) queries against RIR databases.

use crate::display::collect_entities;
use crate::models::vcard::{VCardAddress, VCardValue};
use crate::models::*;
use std::collections::HashMap;
use std::fmt::Write as FmtWrite;

/// Key column width for WHOIS-style output (RPSL convention)
const KEY_WIDTH: usize = 16;

/// Display an RDAP object in WHOIS format to stdout
pub fn display_whois(obj: &RdapObject) {
    let output = format_whois(obj);
    print!("{}", output);
}

/// Format an RDAP object as WHOIS-style text, returning a String
pub fn format_whois(obj: &RdapObject) -> String {
    match obj {
        RdapObject::Autnum(a) => format_autnum(a),
        RdapObject::Domain(d) => format_domain(d),
        RdapObject::IpNetwork(ip) => format_ip_network(ip),
        RdapObject::Entity(e) => format_top_entity(e),
        RdapObject::Nameserver(ns) => format_nameserver_obj(ns),
        RdapObject::Error(err) => format_error(err),
        RdapObject::DomainSearch(ds) => format_domain_search(ds),
        RdapObject::EntitySearch(es) => format_entity_search(es),
        RdapObject::NameserverSearch(ns) => format_nameserver_search(ns),
        RdapObject::Help(h) => format_help(h),
    }
}

// ---------------------------------------------------------------------------
// Formatting helpers
// ---------------------------------------------------------------------------

/// Format a single WHOIS field line: `key:    value\n`
fn field(key: &str, value: &str) -> String {
    let key_with_colon = format!("{}:", key);
    format!("{:<width$}{}\n", key_with_colon, value, width = KEY_WIDTH)
}

/// Extract source name from port43 field (e.g. "whois.ripe.net" -> "RIPE")
fn source_from_port43(port43: Option<&str>) -> Option<String> {
    let p43 = port43?;
    let parts: Vec<&str> = p43.split('.').collect();
    if parts.len() >= 2 {
        let candidate = parts[parts.len() - 2].to_uppercase();
        match candidate.as_str() {
            "RIPE" | "ARIN" | "APNIC" | "AFRINIC" | "LACNIC" => Some(candidate),
            _ => {
                // Try the first part after "whois."
                if parts[0].eq_ignore_ascii_case("whois") && parts.len() >= 2 {
                    Some(parts[1].to_uppercase())
                } else {
                    Some(p43.to_uppercase())
                }
            }
        }
    } else {
        Some(p43.to_uppercase())
    }
}

/// Get the vCard `kind` property value for an entity
fn entity_kind(entity: &Entity) -> Option<String> {
    let vcard = entity.vcard.as_ref()?;
    for prop in vcard.properties() {
        if prop.name == "kind"
            && let VCardValue::Text(s) = &prop.value
        {
            return Some(s.clone());
        }
    }
    None
}

/// Format events into created/last-modified fields
fn format_events(events: &[Event], out: &mut String) {
    for event in events {
        match event.action.as_str() {
            "registration" => out.push_str(&field("created", &event.date)),
            "last changed" => out.push_str(&field("last-modified", &event.date)),
            _ => {}
        }
    }
}

/// Format remarks as `remarks:` lines
fn format_remarks(remarks: &[Remark], out: &mut String) {
    for remark in remarks {
        for desc in &remark.description {
            out.push_str(&field("remarks", desc));
        }
    }
}

/// Entity role classification result
#[derive(Default)]
struct EntityRoles {
    admin_c: Vec<String>,
    tech_c: Vec<String>,
    abuse_c: Vec<String>,
    org: Vec<String>,
    mnt_by: Vec<String>,
}

/// Classify entities by their roles and return handles grouped by role type
fn classify_entities(entities: &HashMap<String, Entity>) -> EntityRoles {
    let mut roles = EntityRoles::default();

    for (handle, entity) in entities {
        for role in &entity.roles {
            match role.as_str() {
                "administrative" => roles.admin_c.push(handle.clone()),
                "technical" => roles.tech_c.push(handle.clone()),
                "abuse" => roles.abuse_c.push(handle.clone()),
                "registrant" => {
                    let kind = entity_kind(entity);
                    match kind.as_deref() {
                        Some("org") => roles.org.push(handle.clone()),
                        _ => roles.mnt_by.push(handle.clone()),
                    }
                }
                _ => {}
            }
        }
    }

    // Sort for deterministic output
    roles.admin_c.sort();
    roles.tech_c.sort();
    roles.abuse_c.sort();
    roles.org.sort();
    roles.mnt_by.sort();
    roles
}

/// Format entity reference fields (org, admin-c, tech-c, abuse-c, mnt-by) on the parent object
fn format_entity_refs(roles: &EntityRoles, out: &mut String) {
    for handle in &roles.org {
        out.push_str(&field("org", handle));
    }
    for handle in &roles.admin_c {
        out.push_str(&field("admin-c", handle));
    }
    for handle in &roles.tech_c {
        out.push_str(&field("tech-c", handle));
    }
    for handle in &roles.abuse_c {
        out.push_str(&field("abuse-c", handle));
    }
    for handle in &roles.mnt_by {
        out.push_str(&field("mnt-by", handle));
    }
}

/// Format a vCard address into WHOIS `address:` lines
fn format_address(addr: &VCardAddress, out: &mut String) {
    if let Some(label) = &addr.label {
        // Pre-formatted address label - split by newlines or commas
        for line in label.as_str().lines() {
            let trimmed = line.trim();
            if !trimmed.is_empty() {
                out.push_str(&field("address", trimmed));
            }
        }
    } else {
        // Construct from structured components
        let parts: [&str; 7] = [
            &addr.po_box,
            &addr.extended,
            &addr.street,
            &addr.locality,
            &addr.region,
            &addr.postal_code,
            &addr.country,
        ];
        let non_empty: Vec<&str> = parts.iter().filter(|p| !p.is_empty()).copied().collect();
        if !non_empty.is_empty() {
            out.push_str(&field("address", &non_empty.join(", ")));
        }
    }
}

/// Format an entity as a WHOIS object block (organisation, role, or person)
///
/// When `top_level` is true, all entities are displayed (including -MNT handles).
/// When false, -MNT handles are skipped (they are only referenced by handle in the parent).
fn format_entity_block(entity: &Entity, source: Option<&str>, top_level: bool) -> String {
    let mut out = String::new();
    let handle = entity.handle.as_deref().unwrap_or("UNKNOWN");
    let kind = entity_kind(entity);

    match kind.as_deref() {
        Some("org") => {
            // Organisation object
            out.push_str(&field("organisation", handle));
            if let Some(vcard) = &entity.vcard {
                if let Some(name) = vcard.name() {
                    out.push_str(&field("org-name", name));
                }
                if let Some(addr) = vcard.address() {
                    format_address(&addr, &mut out);
                }
                if let Some(email) = vcard.email() {
                    out.push_str(&field("e-mail", email));
                }
                if let Some(tel) = vcard.tel() {
                    out.push_str(&field("phone", tel));
                }
            }
            // Nested entity refs for mnt-by
            if !entity.entities.is_empty() {
                let nested = collect_entities(&entity.entities);
                let nested_roles = classify_entities(&nested);
                for h in &nested_roles.mnt_by {
                    out.push_str(&field("mnt-by", h));
                }
            }
            format_events(&entity.events, &mut out);
            if let Some(src) = source {
                out.push_str(&field("source", src));
            }
        }
        Some("group") => {
            // Role object (group kind in vCard)
            out.push_str(&field(
                "role",
                entity
                    .vcard
                    .as_ref()
                    .and_then(|v| v.name())
                    .unwrap_or(handle),
            ));
            if let Some(vcard) = &entity.vcard {
                if let Some(addr) = vcard.address() {
                    format_address(&addr, &mut out);
                }
                if let Some(email) = vcard.email() {
                    out.push_str(&field("abuse-mailbox", email));
                }
                if let Some(tel) = vcard.tel() {
                    out.push_str(&field("phone", tel));
                }
            }
            out.push_str(&field("nic-hdl", handle));
            // Nested entity refs for mnt-by
            if !entity.entities.is_empty() {
                let nested = collect_entities(&entity.entities);
                let nested_roles = classify_entities(&nested);
                for h in &nested_roles.mnt_by {
                    out.push_str(&field("mnt-by", h));
                }
            }
            format_events(&entity.events, &mut out);
            if let Some(src) = source {
                out.push_str(&field("source", src));
            }
        }
        _ => {
            // "individual" or unknown kind
            // Handles ending in -MNT are maintainer references; skip as separate
            // objects when displayed as associated entities (not top-level).
            if !top_level && (handle.ends_with("-MNT") || handle.ends_with("-mnt")) {
                return String::new();
            }

            // Determine object type: mntner for -MNT handles, person otherwise
            if handle.ends_with("-MNT") || handle.ends_with("-mnt") {
                out.push_str(&field("mntner", handle));
                if let Some(vcard) = &entity.vcard {
                    if let Some(name) = vcard.name() {
                        out.push_str(&field("descr", name));
                    }
                    if let Some(addr) = vcard.address() {
                        format_address(&addr, &mut out);
                    }
                    if let Some(email) = vcard.email() {
                        out.push_str(&field("upd-to", email));
                    }
                }
            } else {
                // Person object
                out.push_str(&field(
                    "person",
                    entity
                        .vcard
                        .as_ref()
                        .and_then(|v| v.name())
                        .unwrap_or(handle),
                ));
                if let Some(vcard) = &entity.vcard {
                    if let Some(addr) = vcard.address() {
                        format_address(&addr, &mut out);
                    }
                    if let Some(email) = vcard.email() {
                        out.push_str(&field("e-mail", email));
                    }
                    if let Some(tel) = vcard.tel() {
                        out.push_str(&field("phone", tel));
                    }
                }
                out.push_str(&field("nic-hdl", handle));
            }
            // Nested entity refs for mnt-by
            if !entity.entities.is_empty() {
                let nested = collect_entities(&entity.entities);
                let nested_roles = classify_entities(&nested);
                for h in &nested_roles.mnt_by {
                    out.push_str(&field("mnt-by", h));
                }
            }
            format_events(&entity.events, &mut out);
            if let Some(src) = source {
                out.push_str(&field("source", src));
            }
        }
    }

    out
}

/// Format all associated entity objects as separate WHOIS blocks
fn format_associated_entities(
    entity_map: &HashMap<String, Entity>,
    source: Option<&str>,
) -> String {
    let mut out = String::new();
    let mut handles: Vec<_> = entity_map.keys().collect();
    handles.sort();

    for handle in handles {
        if let Some(entity) = entity_map.get(handle) {
            let block = format_entity_block(entity, source, false);
            if !block.is_empty() {
                if !out.is_empty() {
                    out.push('\n');
                }
                out.push_str(&block);
            }
        }
    }

    out
}

// ---------------------------------------------------------------------------
// Per-type formatters
// ---------------------------------------------------------------------------

/// Format an Autnum (ASN) as WHOIS aut-num object
fn format_autnum(autnum: &Autnum) -> String {
    let mut out = String::new();
    let source = source_from_port43(autnum.port43.as_deref());

    // aut-num header
    if let Some(start) = autnum.start_autnum {
        let _ = writeln!(out, "{:<width$}AS{}", "aut-num:", start, width = KEY_WIDTH);
    }

    if let Some(name) = &autnum.name {
        out.push_str(&field("as-name", name));
    }

    // Collect and classify entities
    let entity_map = collect_entities(&autnum.entities);
    let roles = classify_entities(&entity_map);

    // Entity references (org, admin-c, tech-c, etc.)
    format_entity_refs(&roles, &mut out);

    // Remarks
    format_remarks(&autnum.remarks, &mut out);

    // Status
    for status in &autnum.status {
        out.push_str(&field("status", &status.to_uppercase()));
    }

    // Events
    format_events(&autnum.events, &mut out);

    // Source
    if let Some(src) = &source {
        out.push_str(&field("source", src));
    }

    // Associated entity objects
    let entity_blocks = format_associated_entities(&entity_map, source.as_deref());
    if !entity_blocks.is_empty() {
        out.push('\n');
        out.push_str(&entity_blocks);
    }

    out
}

/// Format a Domain as WHOIS domain object
fn format_domain(domain: &Domain) -> String {
    let mut out = String::new();
    let source = source_from_port43(domain.port43.as_deref());

    // domain header
    if let Some(name) = &domain.ldh_name {
        out.push_str(&field("domain", name));
    }

    if let Some(handle) = &domain.handle {
        out.push_str(&field("handle", handle));
    }

    // Nameservers
    for ns in &domain.nameservers {
        if let Some(name) = &ns.ldh_name {
            out.push_str(&field("nserver", name));
        }
    }

    // DNSSEC
    if let Some(dnssec) = &domain.secure_dns {
        if let Some(ds) = dnssec.delegation_signed {
            out.push_str(&field("ds-rdata", if ds { "signed" } else { "unsigned" }));
        }
        for ds in &dnssec.ds_data {
            let mut ds_line = String::new();
            if let Some(tag) = ds.key_tag {
                let _ = write!(ds_line, "{}", tag);
            }
            if let Some(alg) = ds.algorithm {
                let _ = write!(ds_line, " {}", alg);
            }
            if let Some(dt) = ds.digest_type {
                let _ = write!(ds_line, " {}", dt);
            }
            if let Some(digest) = &ds.digest {
                let _ = write!(ds_line, " {}", digest);
            }
            if !ds_line.is_empty() {
                out.push_str(&field("ds-rdata", ds_line.trim()));
            }
        }
    }

    // Status
    for status in &domain.status {
        out.push_str(&field("status", status));
    }

    // Collect and classify entities
    let entity_map = collect_entities(&domain.entities);
    let roles = classify_entities(&entity_map);

    // Entity references
    format_entity_refs(&roles, &mut out);

    // Remarks
    format_remarks(&domain.remarks, &mut out);

    // Events
    format_events(&domain.events, &mut out);

    // Source
    if let Some(src) = &source {
        out.push_str(&field("source", src));
    }

    // Associated entity objects
    let entity_blocks = format_associated_entities(&entity_map, source.as_deref());
    if !entity_blocks.is_empty() {
        out.push('\n');
        out.push_str(&entity_blocks);
    }

    out
}

/// Format an IpNetwork as WHOIS inetnum/inet6num object
fn format_ip_network(ip: &IpNetwork) -> String {
    let mut out = String::new();
    let source = source_from_port43(ip.port43.as_deref());

    // Determine object type based on IP version
    let obj_type = match ip.ip_version.as_deref() {
        Some("v6") => "inet6num",
        _ => "inetnum",
    };

    // IP range header
    if let (Some(start), Some(end)) = (&ip.start_address, &ip.end_address) {
        let range = format!("{} - {}", start, end);
        out.push_str(&field(obj_type, &range));
    } else if let Some(handle) = &ip.handle {
        out.push_str(&field(obj_type, handle));
    }

    if let Some(name) = &ip.name {
        out.push_str(&field("netname", name));
    }

    if let Some(net_type) = &ip.network_type {
        out.push_str(&field("type", net_type));
    }

    if let Some(country) = &ip.country {
        out.push_str(&field("country", country));
    }

    // Collect and classify entities
    let entity_map = collect_entities(&ip.entities);
    let roles = classify_entities(&entity_map);

    // Entity references
    format_entity_refs(&roles, &mut out);

    // Remarks
    format_remarks(&ip.remarks, &mut out);

    // Status
    for status in &ip.status {
        out.push_str(&field("status", status));
    }

    // Events
    format_events(&ip.events, &mut out);

    // Source
    if let Some(src) = &source {
        out.push_str(&field("source", src));
    }

    // Associated entity objects
    let entity_blocks = format_associated_entities(&entity_map, source.as_deref());
    if !entity_blocks.is_empty() {
        out.push('\n');
        out.push_str(&entity_blocks);
    }

    out
}

/// Format a top-level Entity query result
fn format_top_entity(entity: &Entity) -> String {
    let mut out = String::new();
    let source = source_from_port43(entity.port43.as_deref());

    out.push_str(&format_entity_block(entity, source.as_deref(), true));

    // Nested entities
    if !entity.entities.is_empty() {
        let entity_map = collect_entities(&entity.entities);
        let blocks = format_associated_entities(&entity_map, source.as_deref());
        if !blocks.is_empty() {
            out.push('\n');
            out.push_str(&blocks);
        }
    }

    out
}

/// Format a Nameserver as WHOIS object
fn format_nameserver_obj(ns: &Nameserver) -> String {
    let mut out = String::new();
    let source = source_from_port43(ns.port43.as_deref());

    if let Some(name) = &ns.ldh_name {
        out.push_str(&field("host", name));
    }

    if let Some(handle) = &ns.handle {
        out.push_str(&field("handle", handle));
    }

    if let Some(ips) = &ns.ip_addresses {
        for ip in &ips.v4 {
            out.push_str(&field("ip-address", ip));
        }
        for ip in &ips.v6 {
            out.push_str(&field("ip-address", ip));
        }
    }

    // Status
    for status in &ns.status {
        out.push_str(&field("status", status));
    }

    // Remarks
    format_remarks(&ns.remarks, &mut out);

    // Events
    format_events(&ns.events, &mut out);

    // Entities
    if !ns.entities.is_empty() {
        let entity_map = collect_entities(&ns.entities);
        let roles = classify_entities(&entity_map);
        format_entity_refs(&roles, &mut out);

        if let Some(src) = &source {
            out.push_str(&field("source", src));
        }

        let blocks = format_associated_entities(&entity_map, source.as_deref());
        if !blocks.is_empty() {
            out.push('\n');
            out.push_str(&blocks);
        }
    } else if let Some(src) = &source {
        out.push_str(&field("source", src));
    }

    out
}

/// Format an ErrorResponse
fn format_error(err: &ErrorResponse) -> String {
    let mut out = String::new();
    if let Some(code) = err.error_code {
        let _ = writeln!(out, "%ERROR:101: no entries found");
        let _ = writeln!(out, "%");
        if let Some(title) = &err.title {
            let _ = writeln!(out, "% {}: {}", code, title);
        } else {
            let _ = writeln!(out, "% Error {}", code);
        }
        for desc in &err.description {
            let _ = writeln!(out, "% {}", desc);
        }
    }
    out
}

/// Format DomainSearchResults
fn format_domain_search(ds: &DomainSearchResults) -> String {
    let mut out = String::new();
    for (i, domain) in ds.domains.iter().enumerate() {
        if i > 0 {
            out.push('\n');
        }
        out.push_str(&format_domain(domain));
    }
    out
}

/// Format EntitySearchResults
fn format_entity_search(es: &EntitySearchResults) -> String {
    let mut out = String::new();
    for (i, entity) in es.entities.iter().enumerate() {
        if i > 0 {
            out.push('\n');
        }
        out.push_str(&format_top_entity(entity));
    }
    out
}

/// Format NameserverSearchResults
fn format_nameserver_search(ns: &NameserverSearchResults) -> String {
    let mut out = String::new();
    for (i, nameserver) in ns.nameservers.iter().enumerate() {
        if i > 0 {
            out.push('\n');
        }
        out.push_str(&format_nameserver_obj(nameserver));
    }
    out
}

/// Format HelpResponse
fn format_help(help: &HelpResponse) -> String {
    let mut out = String::new();
    for notice in &help.notices {
        if let Some(title) = &notice.title {
            let _ = writeln!(out, "% {}", title);
        }
        for desc in &notice.description {
            let _ = writeln!(out, "% {}", desc);
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_field_formatting() {
        let result = field("aut-num", "AS213605");
        assert_eq!(result, "aut-num:        AS213605\n");
    }

    #[test]
    fn test_field_long_key() {
        let result = field("last-modified", "2026-02-05T03:28:52Z");
        assert_eq!(result, "last-modified:  2026-02-05T03:28:52Z\n");
    }

    #[test]
    fn test_field_short_key() {
        let result = field("org", "ORG-LA1994-RIPE");
        assert_eq!(result, "org:            ORG-LA1994-RIPE\n");
    }

    #[test]
    fn test_source_from_port43_ripe() {
        assert_eq!(
            source_from_port43(Some("whois.ripe.net")),
            Some("RIPE".to_string())
        );
    }

    #[test]
    fn test_source_from_port43_arin() {
        assert_eq!(
            source_from_port43(Some("whois.arin.net")),
            Some("ARIN".to_string())
        );
    }

    #[test]
    fn test_source_from_port43_apnic() {
        assert_eq!(
            source_from_port43(Some("whois.apnic.net")),
            Some("APNIC".to_string())
        );
    }

    #[test]
    fn test_source_from_port43_none() {
        assert_eq!(source_from_port43(None), None);
    }

    #[test]
    fn test_format_error() {
        let err = ErrorResponse {
            conformance: vec![],
            notices: vec![],
            error_code: Some(404),
            title: Some("Not Found".to_string()),
            description: vec!["The requested object was not found.".to_string()],
            lang: None,
        };
        let result = format_error(&err);
        assert!(result.contains("no entries found"));
        assert!(result.contains("404"));
        assert!(result.contains("Not Found"));
    }

    #[test]
    fn test_format_help() {
        let help = HelpResponse {
            conformance: vec![],
            notices: vec![Notice {
                title: Some("Terms of Service".to_string()),
                notice_type: None,
                description: vec!["Use is subject to terms.".to_string()],
                links: vec![],
            }],
            lang: None,
        };
        let result = format_help(&help);
        assert!(result.contains("% Terms of Service"));
        assert!(result.contains("% Use is subject to terms."));
    }
}