vcard-rs 0.2.1

vCard parser, validator, editor and builder library for Rust
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
//! # Concrete syntax tree
//!
//! The core representation: a whole vCard as generic, byte-faithful syntax.
//!
//! [`VcardCst`] is the hub of the crate. It models a card as its real lines: an
//! optional `BEGIN` / `END` envelope (absent for a bare RFC 2425 record) and
//! the property lines between them, the `VERSION` line among them, made of the
//! nodes in the sibling modules ([`line`](crate::tree::line),
//! [`param`](crate::tree::param), [`value`](crate::tree::value),
//! [`leaf`](crate::tree::leaf)). It knows nothing about what a property
//! *means*. It is filled from bytes (`parse`) or from typed properties
//! (`push`), exports its bytes byte-faithfully
//! ([`to_bytes`](VcardCst::to_bytes), or the lossy-for-non-UTF-8
//! [`Display`](core::fmt::Display)), and offers typed access by lens (`prop`,
//! `prop_mut`, `remove`). The semantic projection ([`decode`](VcardCst::decode))
//! and the codec live in the [`decode`](crate::tree::codec::decode) /
//! [`encode`](crate::tree::codec::encode) siblings.
//!
//! # Example
//!
//! Parse raw bytes into a CST, edit a field in place (byte-preservingly), then
//! project onto the decoded model:
//!
//! ```rust
//! use vcard::tree::cst::VcardCst;
//! use vcard::tree::prop::r#fn::FN;
//! use vcard::version::VcardVersion;
//!
//! let raw = "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nEND:VCARD\r\n";
//! let mut cst = VcardCst::parse(raw).unwrap();
//! assert_eq!(cst.to_string(), raw);
//!
//! cst.prop_mut::<FN>().unwrap().set_text("Jane Doe");
//! assert_eq!(
//!     cst.to_string(),
//!     "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Jane Doe\r\nEND:VCARD\r\n",
//! );
//!
//! let card = cst.decode();
//! assert_eq!(card.version, VcardVersion::V4_0);
//! assert_eq!(&*card.properties[0].name, "FN");
//! ```

use core::fmt;

use alloc::{
    string::{String, ToString},
    vec,
    vec::Vec,
};

use crate::{
    prop::{VcardProp, VcardPropKind},
    tree::{
        codec::mode::VcardEscaper,
        error::VcardParseError,
        line::VcardLine,
        prop::{cardinality::VcardPropCardinality, lens::VcardPropLens, spec::prop_spec},
    },
    value::VcardValue,
    version::VcardVersion,
};

/// A whole card as raw syntax: an optional `BEGIN` / `END` envelope and the
/// property lines between them (the `VERSION` line among them, wherever it
/// falls). All are real lines so nothing is reconstructed by rule, `VERSION`
/// keeps its source position, and the envelope is absent only for a bare RFC
/// 2425 directory record.
#[derive(Clone, Debug)]
pub struct VcardCst<'a> {
    /// The BEGIN line, or `None` for a bare RFC 2425 directory record parsed
    /// without a `BEGIN:VCARD` envelope.
    pub begin: Option<VcardLine<'a>>,
    /// The property lines, in source order, including the `VERSION` line.
    pub props: Vec<VcardLine<'a>>,
    /// The END line, absent exactly when [`begin`](Self::begin) is.
    pub end: Option<VcardLine<'a>>,
}

impl<'a> VcardCst<'a> {
    /// Start an empty vCard 4.0, BEGIN/VERSION/END seeded, ready for
    /// properties.
    pub fn v4() -> Self {
        Self {
            begin: Some(VcardLine::text("BEGIN", "VCARD")),
            props: vec![VcardLine::text("VERSION", &*VcardVersion::V4_0)],
            end: Some(VcardLine::text("END", "VCARD")),
        }
    }

    /// Parse the first card from raw text, borrowing it for the Cst lifetime.
    /// `VERSION` is an ordinary property wherever it appears, or nowhere: the
    /// parser is as liberal about its position as real cards are. Anything
    /// after `END` is ignored; use [`parse_many`](Self::parse_many) for a file.
    ///
    /// A bare RFC 2425 record with no `BEGIN:VCARD` is also accepted: every
    /// line becomes a property, [`begin`](Self::begin) / [`end`](Self::end)
    /// stay `None`, and it round-trips without a synthesised envelope. Only
    /// here, since without the envelope `parse_many` has no record boundary.
    pub fn parse<T: AsRef<[u8]> + ?Sized>(input: &'a T) -> Result<Self, VcardParseError> {
        // NOTE: Trim leading blank-line bytes before the bare-vs-wrapped
        // decision, so it does not hinge on stray leading whitespace:
        // serialization normalises that away, so a classification that depended
        // on it would make the round-trip non-idempotent (a bare record whose
        // first line then reads as BEGIN would reparse as a wrapped card).
        let input = trim_leading_eol(input.as_ref());
        let (first, _rest) = VcardLine::take(input)?;

        if first.name.get().eq_ignore_ascii_case("BEGIN") {
            Self::take_card(input).map(|(card, _rest)| card)
        } else {
            Self::parse_bare(input)
        }
    }

    /// Parse a bare directory record: every line is a property, with no
    /// envelope. The escaping mode is stamped from a `VERSION` line if the
    /// record happens to carry one, else the default.
    fn parse_bare(input: &'a [u8]) -> Result<Self, VcardParseError> {
        let mut props: Vec<VcardLine<'a>> = Vec::new();
        let mut rest = trim_leading_eol(input);

        while !rest.is_empty() {
            let (line, tail) = VcardLine::take(rest)?;
            props.push(line);
            rest = trim_leading_eol(tail);
        }

        let escaper = props
            .iter()
            .find(|line| line.name.get().eq_ignore_ascii_case("VERSION"))
            .map(|line| VcardEscaper::for_version_str(line.raw_value_str().as_ref()))
            .unwrap_or_default();

        for line in &mut props {
            line.value.escaper = escaper;
        }

        Ok(Self {
            begin: None,
            props,
            end: None,
        })
    }

    /// Parse every card in the input, lazily, one item per card (or the parse
    /// error that stopped iteration). Blank lines between cards are skipped.
    /// For multi-card `.vcf` files and CardDAV address books.
    ///
    /// ```rust
    /// use vcard::tree::cst::VcardCst;
    ///
    /// let file = concat!(
    ///     "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Alice\r\nEND:VCARD\r\n",
    ///     "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Bob\r\nEND:VCARD\r\n",
    /// );
    /// let cards: Result<Vec<_>, _> = VcardCst::parse_many(file).collect();
    /// assert_eq!(cards.unwrap().len(), 2);
    /// ```
    pub fn parse_many<T: AsRef<[u8]> + ?Sized>(
        input: &'a T,
    ) -> impl Iterator<Item = Result<Self, VcardParseError>> {
        let mut rest = input.as_ref();

        core::iter::from_fn(move || {
            rest = trim_leading_eol(rest);
            if rest.is_empty() {
                return None;
            }

            match Self::take_card(rest) {
                Ok((card, tail)) => {
                    rest = tail;
                    Some(Ok(card))
                }
                Err(error) => {
                    // NOTE: stop after the first failure; a malformed card
                    // leaves no reliable boundary to resume from.
                    rest = b"";
                    Some(Err(error))
                }
            }
        })
    }

    /// Take one card off the front of `input`, returning it and the unconsumed
    /// rest. The card's `END` is the one at `BEGIN`/`END` depth zero, so a 2.1
    /// inline `AGENT` (a whole nested `BEGIN`..`END`) does not close the card
    /// early; the nested lines are kept verbatim so the card round-trips.
    fn take_card(input: &'a [u8]) -> Result<(Self, &'a [u8]), VcardParseError> {
        let (begin, mut rest) = VcardLine::take(input)?;

        if !begin.name.get().eq_ignore_ascii_case("BEGIN") {
            return Err(VcardParseError::ExpectedBegin(begin.name.get().to_string()));
        }

        let mut props: Vec<VcardLine<'a>> = Vec::new();
        let mut depth = 0usize;

        loop {
            if rest.is_empty() {
                return Err(VcardParseError::MissingEnd(
                    String::from_utf8_lossy(input).into_owned(),
                ));
            }

            let (line, tail) = VcardLine::take(rest)?;
            rest = tail;

            let name = line.name.get();

            if name.eq_ignore_ascii_case("END") {
                if let Some(next) = depth.checked_sub(1) {
                    // NOTE: a nested END closes an embedded (AGENT) card, not
                    // this one; keep it as a verbatim line.
                    depth = next;
                    props.push(line);
                    continue;
                }

                // NOTE: VERSION can sit anywhere, so the escaping mode is only
                // known once the whole card is parsed: stamp every value node
                // with it.
                let escaper = props
                    .iter()
                    .find(|line| line.name.get().eq_ignore_ascii_case("VERSION"))
                    .map(|line| VcardEscaper::for_version_str(line.raw_value_str().as_ref()))
                    .unwrap_or_default();

                for line in &mut props {
                    line.value.escaper = escaper;
                }

                return Ok((
                    Self {
                        begin: Some(begin),
                        props,
                        end: Some(line),
                    },
                    rest,
                ));
            }

            if name.eq_ignore_ascii_case("BEGIN") {
                depth += 1;
            }

            props.push(line);
        }
    }

    /// The `VERSION` line, wherever it sits among the properties (or `None`).
    pub fn version_line(&self) -> Option<&VcardLine<'a>> {
        self.props
            .iter()
            .find(|line| line.name.get().eq_ignore_ascii_case("VERSION"))
    }

    /// The card's version indicator, read from its `VERSION` line. An
    /// unrecognised or missing version normalises to
    /// [`V4_0`](VcardVersion::V4_0).
    pub fn version(&self) -> VcardVersion {
        self.version_line()
            .and_then(|line| line.raw_value_str().parse().ok())
            .unwrap_or(VcardVersion::V4_0)
    }

    /// Append a typed property, encoding it into a line. Adding to a *parsed*
    /// card leaves every existing line byte for byte intact (they stay
    /// borrowed); only the new line is canonical. The building primitive.
    pub fn push(&mut self, prop: VcardProp<'a>) -> &mut Self {
        let escaper = self
            .version_line()
            .map(|line| VcardEscaper::for_version_str(line.raw_value_str().as_ref()))
            .unwrap_or_default();

        self.props.push(prop.encode(escaper));
        self
    }

    /// Remove every property of type `L`.
    pub fn remove<L: VcardPropLens>(&mut self) -> &mut Self {
        self.props
            .retain(|line| !line.name.get().eq_ignore_ascii_case(&L::KIND));
        self
    }

    /// Append a blank instance of every required property the card is missing
    /// for its version: in practice `N` in 2.1 / 3.0 and `FN` from 3.0 on.
    /// Strict servers (iCloud, Fastmail) reject a 3.0 card with no `N`, and an
    /// empty `N:;;;;` satisfies them without inventing a display value.
    ///
    /// The repair half of
    /// [`Vcard::validate`](crate::vcard::Vcard::validate)'s cardinality check,
    /// idempotent and byte-preserving like [`push`](Self::push).
    pub fn fill_required(&mut self) -> &mut Self {
        let version = self.version();
        for kind in VcardPropKind::ALL {
            let spec = prop_spec(kind);
            if !(spec.allowed_versions)().contains(&version) {
                continue;
            }
            let required = matches!(
                (spec.cardinality)(version),
                VcardPropCardinality::ExactlyOne | VcardPropCardinality::OneOrMore,
            );
            let present = self
                .props
                .iter()
                .any(|line| line.name.get().eq_ignore_ascii_case(&kind));
            if !required || present {
                continue;
            }
            if let Some(value_kind) = (spec.allowed_values)(version).first() {
                self.push(VcardProp {
                    name: kind.into(),
                    params: Vec::new(),
                    value: VcardValue::empty(*value_kind),
                });
            }
        }
        self
    }

    /// The first property of type `L`, decoded into a borrowed snapshot. The
    /// card version is threaded through so version-specific value shapes
    /// (`GEO`, the binary props) decode the same way the whole-card
    /// [`decode`](Self::decode) does.
    pub fn prop<L: VcardPropLens>(&self) -> Option<L::Target<'_>> {
        let version = self.version();
        self.props
            .iter()
            .find(|line| line.name.get().eq_ignore_ascii_case(&L::KIND))
            .map(|line| L::decode(line, version))
    }

    /// The first property of type `L`, as a typed cursor for in-place editing.
    pub fn prop_mut<L: VcardPropLens>(&mut self) -> Option<L::Cursor<'_, 'a>> {
        self.props
            .iter_mut()
            .find(|line| line.name.get().eq_ignore_ascii_case(&L::KIND))
            .map(|line| L::cursor(line))
    }

    /// Own every borrowed leaf, detaching the card from the source bytes so it
    /// can outlive them.
    pub fn into_static(self) -> VcardCst<'static> {
        VcardCst {
            begin: self.begin.map(VcardLine::into_static),
            props: self.props.into_iter().map(VcardLine::into_static).collect(),
            end: self.end.map(VcardLine::into_static),
        }
    }

    /// Serialize the card to raw bytes, exactly as parsed. Unlike
    /// [`Display`](core::fmt::Display) (which is lossy for a value carrying a
    /// non-UTF-8 charset), this is always byte-faithful.
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut out = Vec::new();

        if let Some(begin) = &self.begin {
            begin.write_bytes(&mut out);
        }
        for prop in &self.props {
            prop.write_bytes(&mut out);
        }
        if let Some(end) = &self.end {
            end.write_bytes(&mut out);
        }

        out
    }
}

/// Skip leading blank-line bytes (`\r` / `\n`) between cards.
fn trim_leading_eol(mut bytes: &[u8]) -> &[u8] {
    while let Some((first, rest)) = bytes.split_first() {
        if matches!(first, b'\r' | b'\n') {
            bytes = rest;
        } else {
            break;
        }
    }

    bytes
}

impl fmt::Display for VcardCst<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(begin) = &self.begin {
            write!(f, "{begin}")?;
        }

        for prop in &self.props {
            write!(f, "{prop}")?;
        }

        if let Some(end) = &self.end {
            write!(f, "{end}")?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use alloc::{borrow::Cow, string::ToString, vec, vec::Vec};

    use crate::prop::VcardPropKind;
    use crate::version::VcardVersion;
    use crate::{
        param::VcardParam,
        prop::VcardProp,
        tree::{cst::VcardCst, prop::n::N},
        value::{VcardValue, VcardValueUnknown, n::VcardN, text::VcardText},
        vcard::Vcard,
    };

    const CARD: &str = concat!(
        "BEGIN:VCARD\r\n",
        "VERSION:4.0\r\n",
        "N;PID=1:Doe;John;;Dr.;\r\n",
        "FN:John Doe\r\n",
        "END:VCARD\r\n",
    );

    #[test]
    fn round_trips_byte_for_byte() {
        let card = VcardCst::parse(CARD).unwrap();
        assert_eq!(card.to_string(), CARD);
    }

    #[test]
    fn round_trips_a_non_utf8_value_byte_for_byte() {
        use crate::tree::prop::note::NOTE;

        // NOTE: A vCard 2.1 NOTE in ISO-8859-1: the 0xE9 byte ('é' in Latin-1)
        // is not valid UTF-8, so this card could not be a &str at all.
        let mut raw = Vec::new();
        raw.extend_from_slice(b"BEGIN:VCARD\r\nVERSION:2.1\r\nNOTE;CHARSET=ISO-8859-1:caf");
        raw.push(0xE9);
        raw.extend_from_slice(b"\r\nEND:VCARD\r\n");

        let cst = VcardCst::parse(&raw).unwrap();

        // NOTE: to_bytes() is byte-faithful, including the raw 0xE9 octet.
        assert_eq!(cst.to_bytes(), raw);

        // NOTE: CHARSET rides along as a plain parameter; the value bytes are
        // reachable raw through the lens cursor, not transcoded.
        let mut cst = cst;
        assert_eq!(
            cst.prop_mut::<NOTE>().unwrap().bytes().as_ref(),
            &[b'c', b'a', b'f', 0xE9],
        );
        assert!(
            cst.decode().properties[0]
                .params
                .contains(&VcardParam::Charset(Cow::Borrowed("ISO-8859-1"))),
        );
    }

    #[test]
    fn rejects_a_non_utf8_property_name() {
        use crate::tree::error::VcardParseError;

        // NOTE: Only a value may carry a foreign charset; a non-UTF-8 name or
        // parameter is a hard parse error.
        let mut raw = Vec::new();
        raw.extend_from_slice(b"BEGIN:VCARD\r\nVERSION:4.0\r\nX-");
        raw.push(0xFF);
        raw.extend_from_slice(b":v\r\nEND:VCARD\r\n");

        assert!(matches!(
            VcardCst::parse(&raw),
            Err(VcardParseError::NonUtf8Header(_)),
        ));
    }

    #[test]
    fn round_trips_fuzz_regressions() {
        // NOTE: Inputs the coverage-guided fuzzer found where a parsed card
        // serialized to bytes that either failed to reparse or reparsed to
        // different bytes. Every one must now parse, and its serialization must
        // be a byte-stable fixpoint (its own output reparses identically).
        let cases: &[&[u8]] = &[
            // NOTE: QP value left ending in `=` (a dangling soft-break marker)
            // would, once serialized, re-trigger soft-break joining and swallow
            // the END line on reparse. Two ways in: a soft-break join over a
            // blank continuation (`x==` -> `x=`)...
            b"BEGIN:VCARD\r\nVERSION:2.1\r\nNOTE;ENCODING=QUOTED-PRINTABLE:x==\r\n\r\nEND:VCARD\r\n",
            // NOTE: ...and a folded continuation that appends the `=` (`Luo` +
            // ` =`).
            b"BEGIN:VCARD\r\nVERSION:2.1\r\nNOTE;ENCODING=QUOTED-PRINTABLE:Luo\r\n =\r\nEND:VCARD\r\n",
            // NOTE: A dropped blank line before a space-prefixed (fold-looking)
            // line must not let that line fold into the previous one on
            // reparse.
            b"BEGIN:VCARD\r\nVERSION:4.0\r\nNOTE:a\r\n\r\n FN:b\r\nEND:VCARD\r\n",
            // NOTE: A line whose content starts with folding whitespace or a
            // stray CR must strip to a stable form (leading `\t`/`\r` here).
            b"\t\r:",
        ];

        for raw in cases {
            let cst = VcardCst::parse(raw).unwrap_or_else(|e| panic!("parse {raw:?}: {e}"));
            let bytes = cst.to_bytes();

            let reparsed =
                VcardCst::parse(&bytes).unwrap_or_else(|e| panic!("reparse {raw:?}: {e}"));
            assert_eq!(reparsed.to_bytes(), bytes, "not idempotent: {raw:?}");
        }
    }

    #[test]
    fn parses_a_bare_directory_record_without_an_envelope() {
        // NOTE: An RFC 2425 record with no BEGIN:VCARD wrapper: every line is a
        // property and no envelope is synthesised, so it round-trips exactly.
        let raw = "cn:Babs Jensen\r\nemail:babs@umich.edu\r\n";
        let cst = VcardCst::parse(raw).unwrap();

        assert!(cst.begin.is_none());
        assert!(cst.end.is_none());
        assert_eq!(cst.props.len(), 2);
        assert_eq!(cst.to_string(), raw);
    }

    #[test]
    fn parse_many_refuses_a_bare_record() {
        use crate::tree::error::VcardParseError;

        // NOTE: Without an envelope there is no reliable boundary between
        // records, so the multi-card reader rejects a bare record rather than
        // guess.
        let raw = "cn:Babs Jensen\r\nemail:babs@umich.edu\r\n";
        let first = VcardCst::parse_many(raw).next().unwrap();

        assert!(matches!(first, Err(VcardParseError::ExpectedBegin(_))));
    }

    #[test]
    fn parses_many_cards_from_one_input() {
        let a = "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:A\r\nEND:VCARD\r\n";
        let b = "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:B\r\nEND:VCARD\r\n";
        // NOTE: Two cards separated by a blank line, which the parser skips.
        let input = concat!(
            "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:A\r\nEND:VCARD\r\n",
            "\r\n",
            "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:B\r\nEND:VCARD\r\n",
        );

        let cards = VcardCst::parse_many(input)
            .collect::<Result<Vec<_>, _>>()
            .unwrap();

        assert_eq!(cards.len(), 2);
        assert_eq!(cards[0].to_string(), a);
        assert_eq!(cards[1].to_string(), b);
    }

    #[test]
    fn keeps_a_nested_agent_card_intact() {
        // NOTE: A 2.1 inline AGENT embeds a whole vCard; its inner END must not
        // close the outer card, and the whole thing round-trips byte for byte.
        let raw = concat!(
            "BEGIN:VCARD\r\n",
            "VERSION:2.1\r\n",
            "FN:Has Agent\r\n",
            "AGENT:\r\n",
            "BEGIN:VCARD\r\n",
            "VERSION:2.1\r\n",
            "N:Friday;Fred\r\n",
            "TEL;WORK;VOICE:+1-213-555-1234\r\n",
            "END:VCARD\r\n",
            "END:VCARD\r\n",
        );
        assert_eq!(VcardCst::parse(raw).unwrap().to_string(), raw);
    }

    #[test]
    fn reads_a_property_through_its_lens() {
        let card = VcardCst::parse(CARD).unwrap();
        let name = card.prop::<N>().expect("an N property");

        assert_eq!(name.family, vec![Cow::Borrowed("Doe")]);
        assert_eq!(name.given, vec![Cow::Borrowed("John")]);
        assert_eq!(name.prefixes, vec![Cow::Borrowed("Dr.")]);
    }

    #[test]
    fn pushes_a_typed_property_onto_a_parsed_card() {
        let mut card = VcardCst::parse(CARD).unwrap();
        card.push(VcardProp {
            name: VcardPropKind::Email.into(),
            params: [].into(),
            value: VcardValue::Text("john@doe.example".into()),
        });

        let out = card.to_string();
        // NOTE: existing lines kept verbatim; only the appended one is
        // canonical.
        assert!(out.contains("N;PID=1:Doe;John;;Dr.;\r\n"), "{out}");
        assert!(out.contains("EMAIL:john@doe.example\r\n"), "{out}");
    }

    #[test]
    fn removes_every_property_of_a_kind() {
        let mut card = VcardCst::parse(CARD).unwrap();
        card.remove::<N>();
        assert_eq!(
            card.to_string(),
            "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nEND:VCARD\r\n",
        );
    }

    #[test]
    fn fill_required_injects_the_mandatory_empty_n_into_a_3_0_card() {
        // NOTE: A 3.0 card with only a display name: FN but no N (mandatory).
        let mut card =
            VcardCst::parse("BEGIN:VCARD\r\nVERSION:3.0\r\nUID:x\r\nFN:Only\r\nEND:VCARD\r\n")
                .unwrap();
        card.fill_required();

        let out = card.to_string();
        assert!(out.contains("N:;;;;\r\n"), "{out}");
        // NOTE: Existing lines stay verbatim.
        assert!(out.contains("UID:x\r\n"), "{out}");
        assert!(out.contains("FN:Only\r\n"), "{out}");
        // NOTE: The repair makes the card conformant.
        assert!(card.decode().validate().is_ok());
    }

    #[test]
    fn fill_required_is_idempotent_and_leaves_valid_cards_untouched() {
        // NOTE: Running twice adds only the one missing N.
        let mut once =
            VcardCst::parse("BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Only\r\nEND:VCARD\r\n").unwrap();
        once.fill_required().fill_required();
        assert_eq!(once.to_string().matches("N:;;;;").count(), 1);

        // NOTE: 4.0 makes N optional, so a card with FN is left exactly as it
        // was.
        let mut v4 =
            VcardCst::parse("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Only\r\nEND:VCARD\r\n").unwrap();
        let before = v4.to_string();
        assert_eq!(v4.fill_required().to_string(), before);

        // NOTE: A 3.0 card that already has N (and FN) is untouched too.
        let mut has = VcardCst::parse(
            "BEGIN:VCARD\r\nVERSION:3.0\r\nN:Doe;Jane;;;\r\nFN:Jane\r\nEND:VCARD\r\n",
        )
        .unwrap();
        let before = has.to_string();
        assert_eq!(has.fill_required().to_string(), before);
    }

    #[test]
    fn builds_a_card_from_decoded_types() {
        let card = Vcard {
            version: VcardVersion::V4_0,
            properties: vec![VcardProp {
                name: "N".into(),
                params: Vec::new(),
                value: VcardValue::N(VcardN {
                    family: vec![Cow::Borrowed("Doe")],
                    given: vec![Cow::Borrowed("John")],
                    additional: Vec::new(),
                    prefixes: vec![Cow::Borrowed("Dr.")],
                    suffixes: Vec::new(),
                }),
            }],
        };

        assert_eq!(
            card.to_string(),
            "BEGIN:VCARD\r\nVERSION:4.0\r\nN:Doe;John;;Dr.;\r\nEND:VCARD\r\n",
        );
    }

    #[test]
    fn encodes_a_built_card_with_version_specific_escaping() {
        let note = |version| Vcard {
            version,
            properties: vec![VcardProp {
                name: "NOTE".into(),
                params: Vec::new(),
                value: VcardValue::Text(VcardText(Cow::Borrowed("a,b;c"))),
            }],
        };

        // NOTE: 2.1 escapes only `;`, leaving `,` literal.
        assert_eq!(
            note(VcardVersion::V2_1).to_string(),
            "BEGIN:VCARD\r\nVERSION:2.1\r\nNOTE:a,b\\;c\r\nEND:VCARD\r\n",
        );
        // NOTE: 4.0 escapes both `,` and `;` (modern rules).
        assert_eq!(
            note(VcardVersion::V4_0).to_string(),
            "BEGIN:VCARD\r\nVERSION:4.0\r\nNOTE:a\\,b\\;c\r\nEND:VCARD\r\n",
        );

        // NOTE: Pushing onto a parsed 2.1 card uses the card's 2.1 escaping.
        let mut card =
            VcardCst::parse("BEGIN:VCARD\r\nVERSION:2.1\r\nFN:X\r\nEND:VCARD\r\n").unwrap();
        card.push(VcardProp {
            name: "NOTE".into(),
            params: Vec::new(),
            value: VcardValue::Text(VcardText(Cow::Borrowed("a,b;c"))),
        });
        assert!(
            card.to_string().contains("NOTE:a,b\\;c\r\n"),
            "{}",
            card.to_string(),
        );
    }

    #[test]
    fn decodes_the_whole_card() {
        let cst = VcardCst::parse(CARD).unwrap();
        let vcard = cst.decode();

        assert_eq!(vcard.version, VcardVersion::V4_0);
        assert_eq!(vcard.properties.len(), 2);

        let n = &vcard.properties[0];
        assert_eq!(&*n.name, "N");
        assert_eq!(n.params, vec![VcardParam::Pid(vec![Cow::Borrowed("1")])]);
        assert!(matches!(n.value, VcardValue::N(_)));

        let fnn = &vcard.properties[1];
        assert_eq!(&*fnn.name, "FN");
        assert_eq!(
            fnn.value,
            VcardValue::Text(VcardText(Cow::Borrowed("John Doe"))),
        );

        // NOTE: the canonical rebuild reproduces the (already-canonical) card.
        assert_eq!(vcard.to_string(), CARD);
    }

    #[test]
    fn keeps_an_unknown_property_round_tripping() {
        let card = "BEGIN:VCARD\r\nVERSION:4.0\r\nX-CUSTOM:a;b,c\r\nEND:VCARD\r\n";
        let cst = VcardCst::parse(card).unwrap();
        let vcard = cst.decode();

        match &vcard.properties[0].value {
            VcardValue::Unknown(VcardValueUnknown { components }) => {
                assert_eq!(components.len(), 2);
                assert_eq!(components[1], vec![Cow::Borrowed("b"), Cow::Borrowed("c")]);
            }
            other => panic!("expected Unknown, got {other:?}"),
        }
        assert_eq!(vcard.to_string(), card);
    }

    #[test]
    fn reads_and_edits_a_legacy_property_through_its_lens() {
        use crate::tree::prop::label::LABEL;

        let mut card =
            VcardCst::parse("BEGIN:VCARD\r\nVERSION:3.0\r\nLABEL:Old\r\nFN:X\r\nEND:VCARD\r\n")
                .unwrap();

        assert_eq!(
            card.prop::<LABEL>().unwrap(),
            VcardText(Cow::Borrowed("Old"))
        );

        card.prop_mut::<LABEL>().unwrap().set_text("New");
        assert_eq!(
            card.to_string(),
            "BEGIN:VCARD\r\nVERSION:3.0\r\nLABEL:New\r\nFN:X\r\nEND:VCARD\r\n",
        );
    }

    #[test]
    fn parses_version_anywhere_keeping_its_position() {
        // NOTE: VERSION on the third line, as RFC 2426 example cards do.
        let card = "BEGIN:VCARD\r\nBDAY:1980-01-02\r\nVERSION:3.0\r\nFN:X\r\nEND:VCARD\r\n";
        let cst = VcardCst::parse(card).unwrap();

        // NOTE: byte-faithful: VERSION keeps its source position.
        assert_eq!(cst.to_string(), card);

        let vcard = cst.decode();
        assert_eq!(vcard.version, VcardVersion::V3_0);
        // NOTE: VERSION is the indicator, not a property: only BDAY and FN
        // remain.
        assert_eq!(vcard.properties.len(), 2);
    }

    #[test]
    fn parses_a_card_with_no_version() {
        let card = "BEGIN:VCARD\r\nFN:X\r\nEND:VCARD\r\n";
        let cst = VcardCst::parse(card).unwrap();

        // NOTE: The raw card round-trips byte for byte (no VERSION line
        // invented)...
        assert_eq!(cst.to_string(), card);
        // NOTE: ...but the decoded model normalises a missing version to 4.0.
        assert_eq!(cst.decode().version, VcardVersion::V4_0);
    }

    #[test]
    fn unfolds_folded_lines_across_the_card() {
        let folded = "BEGIN:VCARD\r\nVERSION:4.0\r\nNOTE:a long\r\n  note\r\nEND:VCARD\r\n";
        let card = VcardCst::parse(folded).unwrap();

        // NOTE: the folded value is unfolded on parse, and serialized unfolded.
        assert_eq!(
            card.to_string(),
            "BEGIN:VCARD\r\nVERSION:4.0\r\nNOTE:a long note\r\nEND:VCARD\r\n",
        );
        // NOTE: re-parsing the output is then byte-stable (a fixpoint).
        let output = card.to_string();
        let reparsed = VcardCst::parse(&output).unwrap();
        assert_eq!(reparsed.to_string(), output);
    }

    #[test]
    fn tolerates_blank_lines_and_a_missing_final_break() {
        // NOTE: a stray blank line after VERSION, and no trailing break after
        // END.
        let input = "BEGIN:VCARD\r\nVERSION:4.0\r\n\r\nFN:John\r\nEND:VCARD";
        let card = VcardCst::parse(input).unwrap();

        assert_eq!(card.decode().properties.len(), 1);
        // NOTE: the blank line is dropped; the missing final break is
        // preserved.
        assert_eq!(
            card.to_string(),
            "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John\r\nEND:VCARD",
        );
    }
}