//! `parse_vcard`, case by case, plus sixteen whole-card readings as vectors.//!//! Every card below is INVENTED — no real person's details go into this repo —//! but each is shaped like one WhatsApp actually attaches.usesuper::*;/// The TypeScript fixture, CRLF-joined with a trailing blank line, exactly as/// an Android export writes it.fn card() -> String { ["BEGIN:VCARD","VERSION:3.0","N:Rao;Anita;;;","FN:Anita Rao","ORG:Fixture Works;Dispatch","TEL;type=CELL;waid=910123462001:+91 01234 62001","EMAIL;type=INTERNET:anita@example.test","END:VCARD","", ] .join("\r\n")}fn contact(name: &str, phones: &[&str], emails: &[&str], org: Option<&str>) -> VcardContact {VcardContact { name: name.to_string(), phones: phones.iter().map(|s| s.to_string()).collect(), emails: emails.iter().map(|s| s.to_string()).collect(), org: org.map(str::to_string), }}#[test]fn reads_the_name_the_numbers_the_addresses_and_the_company() { assert_eq!( parse_vcard(&card()),Some(contact("Anita Rao", &["+91 01234 62001"], &["anita@example.test"],Some("Fixture Works \u{b7} Dispatch"), )) );}#[test]fn assembles_a_name_out_of_n_when_the_card_has_no_fn() {let card = ["BEGIN:VCARD","VERSION:3.0","N:Rao;Anita;;Dr.;","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card).map(|c| c.name),Some("Dr. Anita Rao".to_string()) );}/// A folded line continues with a leading space, and RFC 6350 says that space/// is the FOLD MARK, not part of the value — so the halves join directly./// Unfolding at all is the point: without it the second half is read as a whole/// property line and the address is lost.#[test]fn unfolds_a_value_the_card_wrapped_over_two_lines() {let card = ["BEGIN:VCARD","FN:Anita Rao","EMAIL:anita.rao.dispatch"," @example.test","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card).map(|c| c.emails),Some(vec!["anita.rao.dispatch@example.test".to_string()]) );}/// `अनिता` as UTF-8, the way an Android card spells it.#[test]fn decodes_the_quoted_printable_android_writes_for_a_non_ascii_name() {let card = ["BEGIN:VCARD","VERSION:2.1","FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E0=A4=85=E0=A4=A8=E0=A4=BF=E0=A4=A4=E0=A4=BE","TEL;CELL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card).map(|c| c.name),Some("\u{905}\u{928}\u{93f}\u{924}\u{93e}".to_string()) );}#[test]fn keeps_a_literal_percent_sign_in_a_value_the_card_did_not_encode() {let card = ["BEGIN:VCARD","FN:100% Fixtures","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card).map(|c| c.name),Some("100% Fixtures".to_string()) );}#[test]fn keeps_every_number_and_drops_the_duplicates() {let card = ["BEGIN:VCARD","FN:Anita Rao","TEL;type=CELL:+910123462001","TEL;type=WORK:+910123462002","TEL;type=HOME:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card).map(|c| c.phones),Some(vec!["+910123462001".to_string(),"+910123462002".to_string() ]) );}#[test]fn reads_only_the_first_card_when_a_file_carries_several() {let second = ["BEGIN:VCARD","FN:Ravi Kumar","TEL:+910123462002","END:VCARD", ] .join("\n");let two = [card(), second].join("\n"); assert_eq!( parse_vcard(&two).map(|c| c.name),Some("Anita Rao".to_string()) );}#[test]fn falls_back_to_the_company_then_to_the_first_number() {let org_only = ["BEGIN:VCARD","ORG:Fixture Works","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&org_only),Some(contact("Fixture Works", &["+910123462001"], &[], None)) );let phone_only = ["BEGIN:VCARD", "TEL:+910123462001", "END:VCARD"].join("\n"); assert_eq!( parse_vcard(&phone_only),Some(contact("+910123462001", &["+910123462001"], &[], None)) );}#[test]fn answers_none_when_there_is_nothing_readable_to_name() {let empty_card = ["BEGIN:VCARD", "VERSION:3.0", "END:VCARD"].join("\n"); assert_eq!(parse_vcard(&empty_card), None); assert_eq!(parse_vcard("this is not a vcard"), None); assert_eq!(parse_vcard(""), None);}// ── Node vectors ────────────────────────────────────────────────────────────//// Sixteen whole-card readings, each produced by RUNNING// `packages/import-core/src/vcard.ts` under Node 22. Four of them corrected a// guess and are called out where they sit./// A fold marked with a TAB joins exactly as a space-marked one does.#[test]fn vector_a_tab_folds_a_line_too() {let card = ["BEGIN:VCARD","FN:Anita Rao","EMAIL:anita.rao.dispatch","\t@example.test","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card),Some(contact("Anita Rao", &[], &["anita.rao.dispatch@example.test"],None )) );}/// **A guess corrected.** `ORG:Fixture\; Works;Line\nTwo` does NOT read as/// `Fixture; Works · Line⏎Two`.////// The value is split on `;` BEFORE it is unescaped, so the escaped semicolon/// has already separated two components and left its backslash stranded at the/// end of the first — where a trailing backslash escapes nothing and survives/// as itself. The company reads `Fixture\ · Works · Line⏎Two`. Reproduced/// rather than repaired: the app and the CLI must agree on the string, and a/// tidier one here would be a company name that changes when a card is/// re-imported by the other reader.#[test]fn vector_a_semicolon_escape_is_split_before_it_is_unescaped() {let card = ["BEGIN:VCARD","FN:Rao\\, Anita","ORG:Fixture\\; Works;Line\\nTwo","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card),Some(contact("Rao, Anita", &["+910123462001"], &[],Some("Fixture\\ \u{b7} Works \u{b7} Line\nTwo"), )) );}/// **A guess corrected.** A quoted-printable SOFT LINE BREAK does not survive/// unfolding.////// `=` at the end of a line is the encoding's own continuation marker, but the/// unfolder runs first and it only joins lines that begin with a space or tab —/// so the continuation becomes its own line, has no colon, and is dropped. The/// name keeps the dangling `=`, because `=` followed by nothing is not `=XX`./// That is the TypeScript's behaviour and it is why an Android card wrapped at/// 76 columns loses everything past the first fold.#[test]fn vector_a_quoted_printable_soft_break_is_not_rejoined() {let card = ["BEGIN:VCARD","FN;ENCODING=QUOTED-PRINTABLE:=E0=A4=85=","=E0=A4=A8=E0=A4=BF","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card).map(|c| c.name),Some("\u{905}=".to_string()) );}/// A quoted-printable value that is not valid UTF-8 comes back as the equals/// signs it literally holds — never as replacement characters.#[test]fn vector_unreadable_quoted_printable_is_left_exactly_as_it_was() {let card = ["BEGIN:VCARD","FN;ENCODING=QUOTED-PRINTABLE:=E0 not utf8 =FF","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card).map(|c| c.name),Some("=E0 not utf8 =FF".to_string()) );}/// A literal `%` inside a quoted-printable value survives the `%`-escaping/// round trip — which is the whole reason the TypeScript escapes it first.#[test]fn vector_a_percent_inside_quoted_printable_survives() {let card = ["BEGIN:VCARD","FN;ENCODING=QUOTED-PRINTABLE:100%=20off","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card).map(|c| c.name),Some("100% off".to_string()) );}/// When the company IS the name, `org` is absent — the label is shown once.#[test]fn vector_org_equal_to_the_name_is_not_carried_twice() {let card = ["BEGIN:VCARD","FN:Fixture Works","ORG:Fixture Works","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card),Some(contact("Fixture Works", &["+910123462001"], &[], None)) );}/// Without a `BEGIN:VCARD` nothing is read at all, however well-formed the/// properties look. A `.vcf` that lost its first line is not a card.#[test]fn vector_a_file_with_no_begin_reads_as_nothing() {let card = ["FN:Anita Rao", "TEL:+910123462001"].join("\n"); assert_eq!(parse_vcard(&card), None);}/// Property names are matched case-INSENSITIVELY, because they are/// upper-cased before the match. An all-lowercase card reads identically.#[test]fn vector_lowercase_property_names_read_the_same() {let card = ["begin:vcard","fn:Anita Rao","tel:+910123462001","end:vcard", ] .join("\n"); assert_eq!( parse_vcard(&card),Some(contact("Anita Rao", &["+910123462001"], &[], None)) );}/// `N:Rao;;;;` — a family name and nothing else is still a name.#[test]fn vector_a_structured_name_with_only_a_family_component() {let card = ["BEGIN:VCARD", "N:Rao;;;;", "TEL:+910123462001", "END:VCARD"].join("\n"); assert_eq!( parse_vcard(&card),Some(contact("Rao", &["+910123462001"], &[], None)) );}/// All five components, in reading order: prefix, given, middle, family,/// suffix — NOT the order the file writes them in.#[test]fn vector_a_structured_name_with_all_five_components() {let card = ["BEGIN:VCARD", "N:Rao;Anita;Kumari;Dr.;Jr.", "END:VCARD"].join("\n"); assert_eq!( parse_vcard(&card),Some(contact("Dr. Anita Kumari Rao Jr.", &[], &[], None)) );}/// A blank address is dropped, a duplicate is dropped, and a padded one is/// trimmed — in first-seen order.#[test]fn vector_emails_are_trimmed_deduplicated_and_kept_in_order() {let card = ["BEGIN:VCARD","FN:Anita Rao","EMAIL: ","EMAIL:a@example.test","EMAIL:a@example.test","EMAIL: b@example.test ","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card),Some(contact("Anita Rao", &[], &["a@example.test", "b@example.test"],None )) );}/// CRLF line endings and blank lines inside the card change nothing.#[test]fn vector_crlf_and_blank_lines_inside_the_card() {let card = "BEGIN:VCARD\r\n\r\nFN:Anita Rao\r\nTEL:+910123462001\r\n\r\nEND:VCARD\r\n"; assert_eq!( parse_vcard(card),Some(contact("Anita Rao", &["+910123462001"], &[], None)) );}/// **A guess corrected.** A line whose colon is at index 0 is not a property/// and is SKIPPED, not treated as the end of the card — the guard is/// `colon <= 0`, so a leading `:no property` line before `BEGIN:VCARD` does not/// stop the card being read.#[test]fn vector_a_line_whose_colon_is_at_index_zero_is_skipped() {let card = [":no property", "BEGIN:VCARD", "FN:Anita Rao", "END:VCARD"].join("\n"); assert_eq!( parse_vcard(&card),Some(contact("Anita Rao", &[], &[], None)) );}/// An astral emoji in a name survives untouched — nothing here slices by code/// unit, so no surrogate pair can be cut in half.#[test]fn vector_an_emoji_in_a_name_survives() {let card = ["BEGIN:VCARD","FN:Anita \u{1f33f} Rao","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card).map(|c| c.name),Some("Anita \u{1f33f} Rao".to_string()) );}/// **A guess corrected.** `ORG:;;Dispatch;;` names the contact `Dispatch` — the/// empty components drop out, one component is left, and with no `FN` and no/// `N` the company becomes the name. `org` is then absent, because it equals/// the name.#[test]fn vector_an_org_with_blank_components_becomes_the_name() {let card = ["BEGIN:VCARD","ORG:;;Dispatch;;","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card),Some(contact("Dispatch", &["+910123462001"], &[], None)) );}/// The FIRST `FN` wins; a second is ignored rather than overwriting it.#[test]fn vector_the_first_fn_wins() {let card = ["BEGIN:VCARD","FN:First","FN:Second","TEL:+910123462001","END:VCARD", ] .join("\n"); assert_eq!( parse_vcard(&card),Some(contact("First", &["+910123462001"], &[], None)) );}/// The wire shape: `org` is ABSENT, never `null`, which is the TypeScript's/// `org?:`. A reader one release behind must not see a key it has no branch/// for.#[test]fn the_json_shape_omits_an_absent_org() {let with_org = contact("Anita Rao", &["+910123462001"], &[], Some("Fixture Works")); assert_eq!( serde_json::to_string(&with_org).expect("serialises"),r#"{"name":"Anita Rao","phones":["+910123462001"],"emails":[],"org":"Fixture Works"}"# );let without = contact("Anita Rao", &[], &[], None); assert_eq!( serde_json::to_string(&without).expect("serialises"),r#"{"name":"Anita Rao","phones":[],"emails":[]}"# );}