cbor-edn 0.0.11

Converter and processor for CBOR Diagnostic Notation (EDN)
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
//! Converters for application-oriented literals
//!
//! Implementations in this module do not use any private APIs, could just as well be implemented
//! by users, but are provided for convenience and reference.

use crate::Transformable as _;

// potential custom extensions: big'123', big'123e-2', big'123p-2', uuid'', mac'', arruint8 etc,
// oid''

/// Visitor for [`visit_application_literals`](super::StandaloneItem::visit_application_literals) that
/// converts all known application oriented literals into their respective values.
///
/// This calls all `visit_application_literals` compatible visitors in this module except
/// [`any_aol_to_tag999`], where the tags are designed to be specifically opt-in by the
/// application.
///
/// This calls:
/// * [`dt_aol_to_item()`]
/// * [`ip_aol_to_item()`]
pub fn all_aol_to_item(id: String, value: String, item: &mut super::Item) -> Result<(), String> {
    // Beware that while those might conflict and overwrite each other, or even panic if they
    // access the item under the premise of being an visit_application_literals visitor (asserting
    // it is an AOL), but ours don't.

    // When extending this list, consider updating the bin/ -- it exerts more fine-grained control
    // and thus does notuse this function.
    //
    // (Or maybe just deprecate this, as the transform builder is the better tool for the job?)
    dt_aol_to_item(id.clone(), value.clone(), item)?;
    ip_aol_to_item(id.clone(), value.clone(), item)?;
    Ok(())
}

/// Visitor for [`visit_tag`](super::StandaloneItem::visit_tag) that processes any known tag into a more
/// understandable form.
///
/// This calls conversions into application orientented literals, annotators (currently none
/// available that are tag driven), and other EDN specifics:
///
/// * [`dt_tag_to_aol`()]
/// * [`ip_tag_to_aol()`]
/// * [`bignum_tag_to_edn_integer()`]
pub fn all_tag_prettify(tag: u64, item: &mut super::Item) -> Result<(), String> {
    // Beware that while those might conflict and overwrite each other, or even panic if they
    // access the item under the premise of being an visit_tag visitor (asserting there is a tagged
    // item), but ours don't.

    // When extending this list, consider updating the bin/ -- it exerts more fine-grained control
    // and thus does notuse this function.
    //
    // (Or maybe just deprecate this, as the transform builder is the better tool for the job?)
    dt_tag_to_aol(tag, item)?;
    ip_tag_to_aol(tag, item)?;
    bignum_tag_to_edn_integer(tag, item)?;
    Ok(())
}

/// Visitor for [`visit_application_literals`](super::StandaloneItem::visit_application_literals) that
/// converts all application oriented literals into tag 999.
///
/// This is convenient when processing EDN into another tool that can deal with the application
/// oriented literals but has no own EDN processor.
///
/// ```rust
/// # use cbor_edn::*;
/// # use cbor_edn::application::*;
/// let mut with_unknown_literals = StandaloneItem::parse("foo'bar'").unwrap();
/// with_unknown_literals.visit_application_literals(&mut any_aol_to_tag999);
/// assert_eq!(
///     with_unknown_literals.serialize(),
///     r#"999(["foo", "bar"])"#,
/// );
/// ```
pub fn any_aol_to_tag999(id: String, value: String, item: &mut super::Item) -> Result<(), String> {
    if matches!(id.as_str(), "h" | "b32" | "h32" | "b64") {
        // These are built into EDN
        return Ok(());
    }

    let id = super::Item::new_text(&id);
    let value = super::Item::new_text(&value);
    let array = super::Item::new_array([id, value].into_iter());
    let mut array = super::StandaloneItem::from(array);
    array.set_delimiters(super::DelimiterPolicy::SingleLineRegularSpacing);
    *item = array.tagged(999);

    Ok(())
}

/// Visitor for [`visit_tag`](super::StandaloneItem::visit_tag) that converts tag 999 into an
/// application oriented literal.
///
/// This is convenient when taking CBOR from a tool that has the domain knowledge for producing
/// application-oriented literals, but not own EDN processor.
///
/// ```rust
/// # use cbor_edn::*;
/// # use cbor_edn::application::*;
/// let mut from_knowledgeable_tool = StandaloneItem::parse(r#"999(["foo", "bar"])"#).unwrap();
/// from_knowledgeable_tool.visit_tag(&mut tag999_to_aol);
/// assert_eq!(
///     from_knowledgeable_tool.serialize(),
///     "foo'bar'",
/// );
/// ```
pub fn tag999_to_aol(tag: u64, item: &mut super::Item) -> Result<(), String> {
    if tag != 999 {
        return Ok(());
    }
    let tagged = item.get_tagged().expect("Visitor promises this is true");
    let Ok(mut items) = tagged.item().get_array_items() else {
        return Err("should be array".into());
    };
    let (Some(ident), Some(value), None) = (items.next(), items.next(), items.next()) else {
        return Err("should contain 2 items".into());
    };
    drop(items);
    let ident = ident.get_string().map_err(|_| "ident should be string")?;
    let value = value.get_string().map_err(|_| "value should be string")?;
    let new_item = crate::Item::new_application_literal(&ident, &value)
        // I don't see how value could ever trigger anything here
        .map_err(|_| "ident string is unsuitable for application-oriented literal")?;
    *item = new_item;
    Ok(())
}

/// Visitor for [`visit_application_literals`](super::StandaloneItem::visit_application_literals) that
/// processes `dt''` literals into RFC9164 (tagged) items.
///
/// ```rust
/// # use cbor_edn::*;
/// # use cbor_edn::application::*;
/// let mut with_dt_literals = StandaloneItem::parse("[dt'1970-01-01T00:00:23Z']").unwrap();
/// with_dt_literals.visit_application_literals(&mut dt_aol_to_item);
/// assert_eq!(
///     with_dt_literals.serialize(),
///     "[23]",
/// );
/// ```
///
/// If the timestamp does not conform to the required format, the application oriented literal is
/// left in place. A comment is added around the item to explain the error.
pub fn dt_aol_to_item(id: String, value: String, item: &mut super::Item) -> Result<(), String> {
    let is_tagged = match id.as_str() {
        "dt" => false,
        "DT" => true,
        _ => return Ok(()),
    };

    let Ok(parsed) = chrono::DateTime::parse_from_rfc3339(&value) else {
        return Err("DateTime parsing failed".to_string());
    };
    let nanos = parsed.timestamp_subsec_nanos();
    let mut replacement = if nanos == 0 {
        super::Item::new_integer_decimal(parsed.timestamp())
    } else {
        // Not worrying about integer precision loss: 2**53 seconds is way beyond age-of-universe.
        //
        // FIXME: if nanos exceeds 999999999 in the event of a leap second, is the conversion correct?
        super::Item::new_float_decimal(parsed.timestamp() as f64 + f64::from(nanos) / 1000000000.0)
    };
    if is_tagged {
        replacement = replacement.tagged(1);
    }
    replacement.set_delimiters(super::DelimiterPolicy::SingleLineRegularSpacing);
    *item = replacement;
    Ok(())
}

/// Visitor for [`visit_tag`](super::StandaloneItem::visit_tag) that
/// processes tag 1 into `dt''` literals.
///
/// ```rust
/// # use cbor_edn::*;
/// # use cbor_edn::application::*;
/// let mut with_dt_literals = StandaloneItem::parse("{/now/0: 1(42)}").unwrap();
/// with_dt_literals.visit_tag(&mut dt_tag_to_aol);
/// assert_eq!(
///     with_dt_literals.serialize(),
///     "{/now/0: DT'1970-01-01T00:00:42+00:00'}",
/// );
/// ```
///
/// Note that the precise format of the serialization is not guaranteed; the implementation might
/// return `...:42Z` just as well.
pub fn dt_tag_to_aol(tag: u64, item: &mut super::Item) -> Result<(), String> {
    if tag != 1 {
        return Ok(());
    }

    let tagged = item
        .get_tagged()
        .expect("Function is required to be called on a tagged item")
        .item();

    *item = dt_item_to_aol(tagged, true)?;
    Ok(())
}

/// Build a dt'' application-oriented literal from an existing item.
///
/// On error, this returns text that can might placed in a comment around the original item.
pub fn dt_item_to_aol<'a>(
    item: &super::Item<'a>,
    wrap_in_tag: bool,
) -> Result<super::Item<'a>, &'static str> {
    let int_part;
    let nano_part;
    if let Ok(timestamp) = item.get_integer() {
        let timestamp = i64::try_from(timestamp)
            .map_err(|_| "Value out of range for integer time processing")?;
        int_part = timestamp;
        nano_part = 0;
    } else {
        let timestamp = item
            .get_float()
            .map_err(|_| "Numeric value expected for dt'' literal")?;
        // As with the other direction, inexactness here has not happened since the beginning of
        // the universe.
        int_part = timestamp as i64;
        nano_part = ((timestamp - int_part as f64) * 1000000000.0) as u32;
    };
    let chronotime = chrono::DateTime::<chrono::offset::Utc>::from_timestamp(int_part, nano_part)
        .ok_or("Value out of range for time stamps")?;

    let label = if wrap_in_tag { "DT" } else { "dt" };
    Ok(
        super::Item::new_application_literal(label, &chronotime.to_rfc3339())
            .expect("DT is a valid application identifier"),
    )
}

#[test]
fn test_dt_vectors() {
    let input = "
    dt'1969-07-21T02:56:16Z',
    dt'1969-07-21T02:56:16.5Z',
    DT'1969-07-21T02:56:16Z'
    ";
    let output = "
    -14159024,
    -14159023.5,
    1(-14159024)
    ";
    test_decode_vectors(input, output, dt_aol_to_item);
    // Not attempting a re-encode: Our format is known to be different because chrono produces
    // +00:00 rather than Z.
}

#[test]
fn test_dt_reencode() {
    // While the integer example is covered in the doc test, the float needs testing too.

    let mut with_dt_literals = super::StandaloneItem::parse("{/now/0: 1(1.5)}").unwrap();
    with_dt_literals.visit_tag(&mut dt_tag_to_aol);
    assert_eq!(
        with_dt_literals.serialize(),
        "{/now/0: DT'1970-01-01T00:00:01.500+00:00'}",
    );
}

/// Visitor for [`visit_application_literals`](super::StandaloneItem::visit_application_literals) that
/// processes `ip''` literals into RFC9164 (tagged) items.
///
/// ```rust
/// # use cbor_edn::*;
/// # use cbor_edn::application::*;
/// let mut with_ip_literals = StandaloneItem::parse("[ip'2001:db8::1234', IP'127.0.0.0/8']").unwrap();
/// with_ip_literals.visit_application_literals(&mut ip_aol_to_item);
/// assert_eq!(
///     with_ip_literals.serialize(),
///     "[h'20010db8000000000000000000001234', 52([8, h'7f'])]",
/// );
/// ```
///
/// If addresses do not conform to the required format, the application oriented literal is left in
/// place. A comment is added around the item to explain the error.
pub fn ip_aol_to_item(id: String, value: String, item: &mut super::Item) -> Result<(), String> {
    let is_tagged = match id.as_str() {
        "ip" => false,
        "IP" => true,
        _ => return Ok(()),
    };
    let ip;
    let prefix: Option<u8>;
    if let Some((first, second)) = value.split_once('/') {
        let Ok(second) = second.parse() else {
            return Err("Prefix is not a (short) number".to_string());
        };
        ip = first;
        prefix = Some(second);
    } else {
        ip = &value;
        prefix = None;
    }
    let ip: std::net::IpAddr = ip
        .parse()
        .map_err(|e| format!("No recognized address format: {}", e))?;
    let (address_bits, ip) = match ip {
        std::net::IpAddr::V4(ip) => (32, u128::from_be_bytes(ip.to_ipv6_compatible().octets())),
        std::net::IpAddr::V6(ip) => (128, u128::from_be_bytes(ip.octets())),
    };
    if let Some(prefix) = prefix {
        if !(0..=address_bits).contains(&prefix) {
            return Err("Prefix exceeds address size".to_string());
        }
        let required_zero_bits = address_bits - prefix;
        let allow_nonzero_mask = u128::MAX << required_zero_bits;
        if ip & allow_nonzero_mask != ip {
            // See Section 4.2 (1) of RFC9164 which disallows that.
            return Err("No bits must be set in the address outside the prefix length".to_string());
        }
    }
    let ip = ip.to_be_bytes();
    let mut ip = ip.as_slice();
    ip = &ip[usize::from((128 - address_bits) / 8)..];
    if prefix.is_some() {
        while ip.last() == Some(&0) {
            ip = &ip[..ip.len() - 1];
        }
    }
    let ip = super::Item::new_bytes_hex(ip);
    let mut replacement = if let Some(prefix) = prefix {
        let prefix = super::Item::new_integer_decimal(prefix);
        super::Item::new_array([prefix, ip].into_iter())
    } else {
        ip
    };
    if is_tagged {
        replacement = replacement.tagged(if address_bits == 32 { 52 } else { 54 });
    }
    replacement.set_delimiters(super::DelimiterPolicy::SingleLineRegularSpacing);
    *item = replacement;
    Ok(())
}

/// Visitor for [`visit_tag`](super::StandaloneItem::visit_tag) that
/// processes tag 52/54 into `ip''` literals.
pub fn ip_tag_to_aol(tag: u64, item: &mut super::Item) -> Result<(), String> {
    let Some(version) = IpVersion::from_tag(tag) else {
        return Ok(());
    };

    let tagged = item
        .get_tagged()
        .expect("Function is required to be called on a tagged item")
        .item();

    *item = ip_item_to_aol(tagged, version, true)?;
    Ok(())
}

#[derive(Copy, Clone)]
pub enum IpVersion {
    V4,
    V6,
}

impl IpVersion {
    fn from_tag(tag: u64) -> Option<Self> {
        match tag {
            52 => Some(IpVersion::V4),
            54 => Some(IpVersion::V6),
            _ => None,
        }
    }

    fn tag_number(&self) -> u64 {
        match self {
            IpVersion::V4 => 52,
            IpVersion::V6 => 54,
        }
    }

    fn addr_len(&self) -> usize {
        match self {
            IpVersion::V4 => 4,
            IpVersion::V6 => 16,
        }
    }
}

/// Build an ip'' application-oriented literal from an existing item.
///
/// On error, this returns text that can might placed in a comment around the original item.
pub fn ip_item_to_aol<'a>(
    item: &super::Item<'a>,
    ip_version: IpVersion,
    wrap_in_tag: bool,
) -> Result<super::Item<'a>, &'static str> {
    let (mut addr, prefix) = if let Ok(addr) = item.get_bytes() {
        (addr, None)
    } else {
        let mut items = item
            .get_array_items()
            .map_err(|_| "Unexpected type for this tag")?;
        let elem1 = items.next().ok_or("Array too short for IP tag")?;
        if elem1.get_bytes().is_ok() {
            // This is the address-with-prefix form; we can't offer an application-oriented literal
            // for that.
            //
            // But we can offer a version of the item where the value is replaced in-place:
            let mut updated = item.clone();
            let address = updated
                .get_array_items_mut()
                .ok()
                .and_then(|mut i| i.next())
                .expect("Succeeded in clone template");
            *address = ip_item_to_aol(address, ip_version, false)?;
            if wrap_in_tag {
                updated = updated.tagged(ip_version.tag_number());
            }
            return Ok(updated);
        };
        let prefix = elem1;
        let addr = items.next().ok_or("Array too short for IP tag")?;
        if items.next().is_some() {
            return Err("Address with prefix and zone identifier is inexpressible in IP'' literal");
        }
        let prefix = prefix
            .get_integer()
            .map_err(|_| "Address with prefix is inexpressible in IP'' literal")?;
        let addr = addr
            .get_bytes()
            .map_err(|_| "Second element after an integer must be a byte string")?;
        (addr, Some(prefix))
    };

    use std::fmt::Write;
    let mut formatted = String::new();

    if addr.len() > ip_version.addr_len() {
        return Err("Data too long for address type");
    }
    addr.resize_with(ip_version.addr_len(), || 0);
    match ip_version {
        IpVersion::V4 => {
            let addr: [u8; 4] = addr.as_slice().try_into().expect("Works by construction");
            write!(formatted, "{}", std::net::Ipv4Addr::from(addr))
                .expect("Writing to a String is infallible");
        }
        IpVersion::V6 => {
            let addr: [u8; 16] = addr.as_slice().try_into().expect("Works by construction");
            write!(formatted, "{}", std::net::Ipv6Addr::from(addr))
                .expect("Writing to a String is infallible");
        }
    }
    if let Some(prefix) = prefix {
        write!(formatted, "/{}", prefix).expect("Writing to a String is infallible");
    }

    let label = if wrap_in_tag { "IP" } else { "ip" };
    Ok(super::Item::new_application_literal(label, &formatted)
        .expect("IP is a valid application identifier"))
}

/// Visitor for [`visit_tag`](super::StandaloneItem::visit_tag) that places comments inside a ta 38
/// language tagged string.
///
/// ```
/// # use cbor_edn::*;
/// # use cbor_edn::application::*;
/// let mut texts = StandaloneItem::parse(r#"[38(["en", "Hello"]), 38(["he", "שלום", true])]"#).unwrap();
/// texts.visit_tag(&mut comment_lang_tag);
/// assert_eq!(
///     texts.serialize(),
///     r#"[38(["en", "Hello"]), 38(["he", "שלום", true/ Right-to-left /])]"#,
/// );
/// ```
pub fn comment_lang_tag<'a>(tag: u64, item: &mut super::Item<'a>) -> Result<(), String> {
    if tag != 38 {
        return Ok(());
    }

    let tagged = item
        .get_tagged_mut()
        .expect("Function is required to be called on a tagged item")
        .item_mut();

    // FIXME: Simplify now that lifetimes are less generic.
    let mut index = 0;
    let mut commenter: Box<dyn FnMut(&mut crate::Item<'a>) -> _> = Box::new(move |item| {
        let result = match index {
            0 | 1 => None,
            2 => {
                // FIXME: This shows the need for simple value accessors -- or should this maybe be
                // played via CBOR encoding instead?
                if item == crate::StandaloneItem::parse("true").unwrap().item() {
                    Some("Right-to-left")
                } else if item == crate::StandaloneItem::parse("false").unwrap().item() {
                    Some("Left-to-right")
                } else if item == crate::StandaloneItem::parse("null").unwrap().item() {
                    Some("Automatic direction")
                } else {
                    None
                }
                .map(|s| s.to_string())
            }
            // Might be extended compatibly
            _ => None,
        };
        index += 1;
        Ok(result)
    });

    tagged
        .visit_array_elements(&mut commenter)
        .map_err(|e| format!("{e}"))
}

#[test]
fn test_ip_vectors() {
    let input = "
    ip'192.0.2.42',
    IP'192.0.2.42',
    IP'192.0.2.0/24',
    ip'2001:db8::42',
    IP'2001:db8::42',
    IP'2001:db8::/64'
    52([ip'192.0.2.42',24])
    ";
    let output = "
    h'c000022a',
    52(h'c000022a'),
    52([24,h'c00002']),
    h'20010db8000000000000000000000042',
    54(h'20010db8000000000000000000000042'),
    54([64,h'20010db8'])
    52([h'c000022a',24])
    ";
    test_decode_vectors(input, output, ip_aol_to_item);
    test_encode_vectors(input, output, ip_tag_to_aol);
}

#[cfg(test)]
fn test_decode_vectors(
    input: &str,
    output: &str,
    mut map: impl FnMut(String, String, &mut super::Item<'_>) -> Result<(), String>,
) {
    let input = input
        .lines()
        .map(|l| l.trim().trim_end_matches(','))
        .filter(|l| !l.is_empty());
    let output = output
        .lines()
        .map(|l| l.trim().trim_end_matches(','))
        .filter(|l| !l.is_empty());

    for (input, output) in input.zip(output) {
        println!("Converting {:?} to {:?}", input, output);
        let mut input_item =
            super::StandaloneItem::parse(input).expect("Failed to parse test vector");
        input_item.visit_application_literals(&mut map);
        // The converter tries to give nice optical outptu, but the test vectors are compact
        input_item.set_delimiters(super::DelimiterPolicy::DiscardAllButComments);
        assert_eq!(
            input_item.serialize(),
            output,
            "Test vector {input} did not convert exactly"
        );
    }
}

#[cfg(test)]
fn test_encode_vectors(
    aol: &str,
    maybetagged: &str,
    mut map: impl FnMut(u64, &mut super::Item<'_>) -> Result<(), String>,
) {
    let aol = aol
        .lines()
        .map(|l| l.trim().trim_end_matches(','))
        .filter(|l| !l.is_empty());
    let maybetagged = maybetagged
        .lines()
        .map(|l| l.trim().trim_end_matches(','))
        .filter(|l| !l.is_empty());

    for (aol, maybetagged) in aol.zip(maybetagged) {
        println!("Converting {:?} to {:?}", maybetagged, aol);
        let mut input_item =
            super::StandaloneItem::parse(maybetagged).expect("Failed to parse test vector");
        if input_item.item().get_tag().is_err() {
            println!("Skipping this item: not tagged, thus not convertible");
            continue;
        }
        input_item.visit_tag(&mut map);
        // The converter tries to give nice optical outptu, but the test vectors are compact
        input_item.set_delimiters(super::DelimiterPolicy::DiscardAllButComments);
        assert_eq!(
            input_item.serialize(),
            aol,
            "Test vector {maybetagged} did not convert exactly"
        );
    }
}

/// Given a map that represents a CWT Claims Set (CCS), annotate the keys with their corresponding
/// JWT Claim Names in a comment, and possibly apply other nested annotations.
///
/// This currently only processes the items of the registry that were placed there by RFCs, but
/// that is subject to change.
#[allow(clippy::type_complexity)] // reason: fitting a callback style
pub fn comment_ccs<'a>(
    key: &mut super::Item<'a>,
    value: &mut super::Item<'a>,
) -> Result<(Option<String>, Result<Option<String>, String>), String> {
    let Ok(key) = key.get_integer() else {
        // FIXME complain about non-int/tstr keys
        return Ok((None, Ok(None)));
    };

    let handle_timestamp = |item: &mut crate::Item<'_>| -> _ {
        *item = dt_item_to_aol(item, false)?;
        Ok(None)
    };

    let (ks, vs) = match key {
        1 => (Some("iss"), Ok(None)),
        2 => (Some("sub"), Ok(None)),
        3 => (Some("aud"), Ok(None)),
        4 => (Some("exp"), handle_timestamp(value)),
        5 => (Some("nbf"), handle_timestamp(value)),
        6 => (Some("iat"), handle_timestamp(value)),
        7 => (Some("jti"), Ok(None)),
        8 => (
            Some("cnf"),
            // Not enforcing a particular cardinality: "At most one of the COSE_Key and
            // Encrypted_COSE_Key […] may be present", but I think a KID to go with it is fine.
            value
                .visit_map_elements(&mut comment_ccs_cnf)
                .map(|()| None)
                .map_err(|e| format!("{e}")),
        ),
        9 => (Some("scope"), Ok(None)),
        38 => (Some("ace_profile"), Ok(None)),
        39 => (Some("cnonce"), Ok(None)),
        40 => (Some("exi"), Ok(None)),
        _ => (None, Ok(None)),
    };

    Ok((ks.map(String::from), vs))
}

/// Given a COSE header map, annotate its keys with their corresponding names, and possibly apply
/// other nested annotations.
#[allow(clippy::type_complexity)] // reason: fitting a callback style
pub fn comment_cose_header<'a>(
    key: &mut super::Item<'a>,
    value: &mut super::Item<'a>,
) -> Result<(Option<String>, Result<Option<String>, String>), String> {
    let Ok(key) = key.get_integer() else {
        // FIXME complain about non-int/tstr keys
        return Ok((None, Ok(None)));
    };

    let handle_ccs = Box::new(|item: &mut crate::Item<'_>| {
        item.visit_map_elements(&mut comment_ccs)
            .map(|_| None)
            .map_err(|e| format!("{e}"))
    });

    let (ks, vs) = match key {
        1 => (Some("alg"), comment_cose_algorithm(value)),
        2 => (Some("crit"), Ok(None)),
        3 => (Some("content type"), Ok(None)),
        4 => (Some("kid"), Ok(None)),
        // FIXME more
        13 => (Some("kcwt"), Ok(None)),
        14 => (Some("kccs"), handle_ccs(value)),
        15 => (Some("CWT Claims"), handle_ccs(value)),
        16 => (Some("typ"), Ok(None)),
        22 => (Some("c5t"), Ok(None)),
        23 => (Some("c5u"), Ok(None)),
        24 => (Some("c5b"), Ok(None)),
        25 => (Some("c5c"), Ok(None)),
        32 => (Some("x5bag"), Ok(None)),
        33 => (Some("x5chain"), Ok(None)),
        34 => (Some("x5t"), Ok(None)),
        35 => (Some("x5u"), Ok(None)),
        // FIXME more
        _ => (None, Ok(None)),
    };

    Ok((ks.map(String::from), vs))
}

#[allow(clippy::type_complexity)] // reason: fitting a callback style
fn comment_ccs_cnf<'a>(
    key: &mut super::Item<'a>,
    value: &mut super::Item<'a>,
) -> Result<(Option<String>, Result<Option<String>, String>), String> {
    let Ok(key) = key.get_integer() else {
        // It doesn't really say that they are integers in 8747, does it?
        return Ok((None, Ok(None)));
    };

    // Keys from https://www.iana.org/assignments/cwt/cwt.xhtml#confirmation-methods
    let (ks, vs) = match key {
        1 => (Some("COSE_Key"), {
            let mut kty = None;
            value
                .visit_map_elements(&mut |k, v| comment_cose_key(k, v, &mut kty))
                .map(|_| None)
                .map_err(|e| format!("{e}"))
        }),
        3 => (Some("kid"), Ok(None)),
        4 => (
            Some("osc"),
            value
                .visit_map_elements(&mut comment_cnf_osc)
                .map(|_| None)
                .map_err(|e| format!("{e}")),
        ),
        _ => (None, Ok(None)),
    };

    Ok((ks.map(String::from), vs))
}

#[allow(clippy::type_complexity)] // reason: fitting a callback style
fn comment_cnf_osc<'a>(
    key: &mut super::Item<'a>,
    value: &mut super::Item<'a>,
) -> Result<(Option<String>, Result<Option<String>, String>), String> {
    let key = key.get_integer().map_err(|e| format!("{e}"))?;

    // Keys from https://www.iana.org/assignments/ace/ace.xhtml#oscore-security-context-parameters

    let (ks, vs) = match key {
        0 => (Some("id"), Ok(None)),
        1 => (Some("version"), Ok(None)),
        2 => (Some("ms"), Ok(None)),
        3 => (Some("hkdf"), comment_cose_algorithm(value)),
        4 => (Some("alg"), comment_cose_algorithm(value)),
        5 => (Some("salt"), Ok(None)),
        6 => (Some("contextId"), Ok(None)),
        _ => (None, Ok(None)),
    };

    Ok((ks.map(String::from), vs))
}

fn comment_cose_algorithm(item: &mut super::Item<'_>) -> Result<Option<String>, String> {
    // From https://www.iana.org/assignments/cose/cose.xhtml#algorithms
    // FIXME do more of those
    if let Ok(n) = item.get_integer() {
        Ok(Some(
            match n {
                -18 => "SHAKE128",
                -17 => "SHA-512/256",
                -16 => "SHA-256",
                -15 => "SHA-256/64",
                -14 => "SHA-1",
                -13 => "direct+HKDF-AES-256",
                -12 => "direct+HKDF-AES-128",
                -11 => "direct+HKDF-SHA-512",
                -10 => "direct+HKDF-SHA-256",
                -8 => "EdDSA",
                -7 => "ES256",
                -6 => "direct",
                -5 => "A256KW",
                -4 => "A192KW",
                -3 => "A128KW",
                0 => return Err("Reserved".into()),
                1 => "A128GCM",
                2 => "A192GCM",
                3 => "A256GCM",
                4 => "HMAC 256/64",
                5 => "HMAC 256/256",
                6 => "HMAC 384/384",
                7 => "HMAC 512/512",
                10 => "AES-CCM-16-64-128",
                11 => "AES-CCM-16-64-256",
                12 => "AES-CCM-64-64-128",
                13 => "AES-CCM-64-64-256",
                14 => "AES-MAC 128/64",
                15 => "AES-MAC 256/64",
                24 => "ChaCha20/Poly1305",
                // Not commenting on values we don't know
                _ => return Ok(None),
            }
            .to_string(),
        ))
    } else {
        // FIXME: Should return Err("COSE algorithms are integer or strings"), but we don't have an
        // accessor for string literals yet.
        Ok(None)
    }
}

/// Provides comments for the keys, and comment generators for the values, of a COSE key.
///
/// This is different from other handlers in that it also takes a key type: initially None, this is
/// where (even though separately visiting the various keys of a map), the key type is stored when
/// first encountered, to be used for later decisions on the key type specific map keys.
#[allow(clippy::type_complexity)] // reason: fitting a callback style, at least in return value
fn comment_cose_key<'a>(
    key: &mut super::Item<'a>,
    value: &mut super::Item<'a>,
    kty: &mut Option<RecognizedCoseKty>,
) -> Result<(Option<String>, Result<Option<String>, String>), String> {
    use RecognizedCoseKty::*;

    let key = key.get_integer().map_err(|e| format!("{e}"))?;

    // Keys from https://www.iana.org/assignments/cose#key-common-parameters
    let (ks, vs) = match (key, *kty) {
        (1, _) => (Some("kty"), comment_and_store_cose_kty(value, kty)),
        (2, _) => (Some("kid"), Ok(None)),
        (3, _) => (Some("alg"), Ok(None)),
        (4, _) => (Some("key_ops"), Ok(None)),
        (5, _) => (Some("Base IV"), Ok(None)),

        (-1, Some(Okp | Ec2)) => (Some("crv"), comment_cose_elliptic_crv(value)),
        (-2, Some(Okp | Ec2)) => (Some("x"), Ok(None)),
        (-3, Some(Okp)) => (Some("y"), Ok(None)),
        (-4, Some(Okp | Ec2)) => (Some("d"), Ok(None)),
        // FIXME: missing everything RSA
        (-1, Some(Symmetric)) => (Some("k"), Ok(None)),
        (-1, Some(HssLms)) => (Some("pub"), Ok(None)),
        // FIXME: missing everything WalnutDSA
        (-1, Some(Akp)) => (Some("pub"), Ok(None)),
        (-2, Some(Akp)) => (Some("priv"), Ok(None)),

        _ => (None, Ok(None)),
    };

    Ok((ks.map(String::from), vs))
}

#[derive(Copy, Clone, Debug)]
enum RecognizedCoseKty {
    Okp,
    Ec2,
    Rsa,
    Symmetric,
    HssLms,
    WalnutDsa,
    Akp,
}

fn comment_and_store_cose_kty(
    item: &mut super::Item<'_>,
    kty: &mut Option<RecognizedCoseKty>,
) -> Result<Option<String>, String> {
    // From https://www.iana.org/assignments/cose#key-type
    use RecognizedCoseKty::*;
    if let Ok(n) = item.get_integer() {
        let (text, stored) = match n {
            1 => ("OKP", Okp),
            2 => ("EC2", Ec2),
            3 => ("RSA", Rsa),
            4 => ("Symmetric", Symmetric),
            5 => ("HSS-LMS", HssLms),
            6 => ("WalnutDSA", WalnutDsa),
            7 => ("AKP", Akp),

            // Not commenting on values we don't know
            _ => return Ok(None),
        };
        *kty = Some(stored);
        Ok(Some(text.to_string()))
    } else {
        Err("expected integer".into())
    }
}

fn comment_cose_elliptic_crv(item: &mut super::Item<'_>) -> Result<Option<String>, String> {
    // From https://www.iana.org/assignments/cose#elliptic-curves
    let Ok(n) = item.get_integer() else {
        return Err("COSE Elliptic curves are integers".to_string());
    };

    Ok(Some(
        match n {
            1 => "P-256",
            2 => "P-384",
            3 => "P-521",
            4 => "X25519",
            5 => "X448",
            6 => "Ed25519",
            7 => "Ed448",
            8 => "secp256k1",
            256 => "brainpoolP256r1",
            257 => "brainpoolP320r1",
            258 => "brainpoolP384r1",
            259 => "brainpoolP512r1",
            // Not commenting on values we don't know
            _ => return Ok(None),
        }
        .to_string(),
    ))
}

#[doc(inline)]
pub use crate::number::bignum_tag_to_edn_integer;
#[allow(deprecated)] // reason: need to export it to have deprecation useful
#[doc(inline)]
pub use crate::number::tag23_to_edn_integer;

#[doc(inline)]
pub use crate::string::apply_bytestring_heuristincs;