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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
//! Typed header value parsing for RFC 8621 JMAP `As*` header forms.
//!
//! RFC 8621 §4.1.2 defines several parsed-form selectors that a JMAP server
//! may apply to a single header field's raw bytes:
//!
//! | RFC 8621 form | Section | mime-tree result variant |
//! |----------------------|------------|------------------------------------------|
//! | `asRaw` | §4.1.2.1 | [`HeaderValueTyped::Raw`] |
//! | `asText` | §4.1.2.2 | [`HeaderValueTyped::Text`] |
//! | `asAddresses` | §4.1.2.3 | [`HeaderValueTyped::Addresses`] |
//! | `asGroupedAddresses` | §4.1.2.4 | [`HeaderValueTyped::GroupedAddresses`] |
//! | `asMessageIds` | §4.1.2.5 | [`HeaderValueTyped::MessageIds`] |
//! | `asDate` | §4.1.2.6 | [`HeaderValueTyped::DateTime`] |
//! | `asURLs` | §4.1.2.7 | [`HeaderValueTyped::URLs`] |
//!
//! The entry point is [`parse_header_typed`]. It takes the [`HeaderForm`]
//! selector and the raw bytes of the header field value (the portion to the
//! right of the `:` in the header line, including any folded continuation
//! lines but excluding the header name and the trailing CRLF).
//!
//! Parsing is best-effort. On failure the function returns the appropriate
//! empty value (an empty `Vec`, an empty `Raw` string, or `DateTime(None)`
//! for an unparseable date) — it never panics and never returns an error.
//!
//! These types are independent of the [`crate::ParsedHeader`] surface,
//! which continues to expose only the decoded raw string. To layer a
//! typed view on top of an existing `ParsedHeader`, feed its `value`
//! bytes to [`parse_header_typed`]:
//!
//! ```ignore
//! let msg = mime_tree::parse(raw)?;
//! if let Some(h) = msg.headers.iter().find(|h| h.name.eq_ignore_ascii_case("From")) {
//! let addrs = mime_tree::parse_addresses(h.value.as_bytes());
//! }
//! ```
//!
//! For convenience, [`parse_header_typed_from`] is a thin wrapper that
//! takes the `ParsedHeader` directly:
//!
//! ```ignore
//! let typed = mime_tree::parse_header_typed_from(h, mime_tree::HeaderForm::Addresses);
//! ```
//!
//! Either path yields the same result.
use Cow;
use fmt;
use ;
use ;
use UnicodeNormalization;
use crateParsedHeader;
/// A single RFC 5322 `mailbox` parsed from an `address-list`.
///
/// Mirrors the JMAP `EmailAddress` object defined in RFC 8621 §4.1.2.3.
///
/// `name` is the optional display name. `address` is the `addr-spec`. Both
/// are populated best-effort; either may be `None` if the original header
/// is malformed.
///
/// # Equality semantics
///
/// The derived `PartialEq`/`Eq`/`Hash` is byte-exact on both fields. In
/// particular, `address` comparison is case-sensitive across the entire
/// addr-spec, even though RFC 5321 §2.4 defines the *domain* part of an
/// addr-spec as case-insensitive — so `alice@example.com` and
/// `alice@EXAMPLE.COM` compare as not equal and hash differently. Callers
/// that need RFC-5321-conformant equality (HashSet dedup of recipient
/// lists, etc.) MUST canonicalise the domain part themselves before
/// comparing or hashing.
/// A group of `EmailAddress` values, optionally named.
///
/// Mirrors the JMAP `EmailAddressGroup` object defined in RFC 8621 §4.1.2.4.
///
/// Per RFC 8621 §4.1.2.4, consecutive mailboxes that are not part of a
/// declared RFC 5322 `group` are still collected under an `AddressGroup`
/// whose `name` is `None`, "to provide a uniform type".
/// Sign of a `date-time` timezone offset from GMT (RFC 5322 §3.3).
///
/// East of GMT corresponds to positive `+HHMM` offsets (e.g. `+0100`).
/// West of GMT corresponds to negative `-HHMM` offsets (e.g. `-0600`).
/// An RFC 5322 §3.3 `date-time` value parsed from a header.
///
/// Public fields permit serde transparency and direct field access from
/// JMAP-shaped code. The fields mirror `mail_parser::DateTime` 1-to-1
/// **except** for `tz_sign`, which is an explicit enum rather than a
/// bool. This is a deliberate API choice — see `TzSign` — and means
/// `HeaderDateTime` and `mail_parser::DateTime` are not bit-identical
/// even though they round-trip via [`HeaderDateTime::from_mail_parser`]
/// / [`HeaderDateTime::to_mail_parser`].
///
/// # Wire-format dependency on mail-parser
///
/// [`Self::to_rfc3339`] and [`Self::to_timestamp`] delegate to
/// `mail_parser::DateTime`'s formatters. The exact strings produced by
/// `to_rfc3339`, and the exact value produced by `to_timestamp` for
/// edge-case input, are therefore defined by the pinned mail-parser
/// version. mime-tree's Cargo.toml uses a caret range (`mail-parser =
/// "0.11"`) so 0.11.x patch updates can in principle change the output
/// without a mime-tree version bump. Downstream callers that persist
/// these strings (database keys, JMAP wire responses, indexed columns)
/// SHOULD pin mail-parser tightly if they require byte-stable output
/// across mime-tree patch bumps.
///
/// # Field invariants
///
/// `parse_header_typed` only constructs `HeaderDateTime` values that
/// passed mail-parser's validation: `year >= 1900`, `month ∈ 1..=12`,
/// `day ∈ 1..=31` (calendar-validated), `hour ∈ 0..=23`,
/// `minute ∈ 0..=59`, `second ∈ 0..=60` (RFC 5322 §4.3 leap second),
/// `tz_hour ∈ 0..=23`, `tz_minute ∈ 0..=59`.
///
/// Direct construction with public fields can produce out-of-range
/// values. The behaviour of `to_rfc3339` and `to_timestamp` on such
/// values is unspecified — output may be syntactically malformed
/// RFC 3339 or a meaningless `i64`. Callers that build `HeaderDateTime`
/// from external sources should validate ranges themselves.
///
/// # Equality semantics
///
/// The derived `PartialEq`/`Eq`/`Hash` is **field-wise**, not
/// **instant-wise**. Two `HeaderDateTime` values representing the same
/// moment in time at different offsets compare as not-equal and hash
/// differently. For example:
///
/// ```text
/// 2024-01-01T12:00:00+00:00 (12:00 UTC)
/// 2024-01-01T13:00:00+01:00 (12:00 UTC, expressed +01:00)
/// ```
///
/// are the same instant but compare `!=`. Callers needing
/// instant-equality (deduping timestamps across clients in different
/// time zones, time-series bucketing) MUST compare
/// [`Self::to_timestamp`] values rather than relying on the derived
/// `PartialEq`.
/// Selector for the RFC 8621 parsed-form of a header value.
///
/// This is the form-token from a JMAP `header:<name>:as<form>` property
/// selector, normalised to an enum.
///
/// [`Display`](fmt::Display) emits the canonical JMAP form-token string
/// (`asRaw`, `asAddresses`, …, `asURLs`).
/// [`FromStr`](std::str::FromStr) accepts exactly that set of strings;
/// any other input yields [`UnknownHeaderForm`].
/// Error returned by [`HeaderForm`]'s [`FromStr`](std::str::FromStr) impl
/// when the input is not a recognised JMAP form-token.
///
/// The wrapped string is the input as given (case-sensitive); JMAP
/// form-tokens are case-sensitive per RFC 8621 §4.1.2.
;
/// A header field value rendered in one of the RFC 8621 parsed forms.
///
/// # Serde wire format
///
/// `HeaderValueTyped` and [`HeaderForm`] use serde's *default*
/// representation: externally-tagged for the enum, capitalised Rust
/// variant names. Examples:
///
/// ```json
/// {"Addresses": [{"name": "Alice", "address": "alice@example.com"}]}
/// {"DateTime": null}
/// {"Raw": "Subject line"}
/// "Addresses" // serialized HeaderForm
/// ```
///
/// This is a **deliberate** choice for in-crate serialization (between
/// services, into databases). It is **not** the RFC 8621 wire format
/// used over JMAP HTTP/JSON. RFC 8621 §4.1.2 uses property-selector
/// strings such as `header:Subject:asAddresses` rather than serializing
/// the form name as an enum tag. Callers exposing parsed headers to a
/// JMAP client SHOULD map between this representation and the JMAP
/// wire format at the API boundary; relying on the in-crate serde
/// shape as the wire format will produce a non-conformant JMAP
/// response.
///
/// Pre-1.0, this representation is subject to change. From 1.0 onward
/// the in-crate serde format will be a stability surface and will be
/// changed only with a major version bump.
/// Parse a header field value into the requested RFC 8621 parsed form.
///
/// `raw_value` is the bytes of the header field value — the portion to
/// the right of the `:` in the header line, including any folded
/// continuation lines.
///
/// # Trailing line ending
///
/// A trailing CRLF (or bare LF) is **permitted but not required**:
///
/// * Input ending in `\r\n` is used as-is.
/// * Input ending in `\n` is converted to `\r\n` internally.
/// * Input with no trailing line ending has `\r\n` appended internally.
///
/// All three shapes produce the same result. Callers may pass the field
/// body with or without the line ending — pick whichever is easiest to
/// extract from the source.
///
/// # Best-effort parsing
///
/// Malformed input yields the empty result for the requested form
/// (empty `Vec`, empty string, or `DateTime(None)`). The function never
/// panics and never returns an error.
///
/// # Empty-result ambiguity
///
/// The empty result is the **same value** regardless of cause:
///
/// | Form | Empty value | Triggered by |
/// |---------------------|-------------------------|----------------------------------------------------|
/// | `Raw` | `Raw("")` | empty, all-whitespace, or all-malformed-UTF-8 input |
/// | `Addresses` | `Addresses(vec![])` | empty, malformed, or zero-mailbox input |
/// | `GroupedAddresses` | `GroupedAddresses(vec![])` | same as above |
/// | `MessageIds` | `MessageIds(vec![])` | empty, no `<...>` brackets, all garbage |
/// | `Date` | `DateTime(None)` | empty, malformed, or all-zero day/month |
/// | `URLs` | `URLs(vec![])` | empty, no `<...>` brackets, all garbage |
///
/// Callers cannot distinguish "header was present but empty" from
/// "header was present but malformed" from "input was non-UTF-8 noise"
/// using this API. Out-of-band signalling (e.g. recording a warning
/// alongside the parse result) is the caller's responsibility. Future
/// minor releases may add a parallel `parse_header_typed_strict`
/// returning `Result` or a tuple `(HeaderValueTyped, Warnings)` —
/// neither is exposed today.
///
/// # Examples
///
/// ## Addresses (RFC 8621 §4.1.2.3)
///
/// ```
/// use mime_tree::{parse_header_typed, EmailAddress, HeaderForm, HeaderValueTyped};
///
/// // RFC 8621 §4.1.2.3 example (the "James Smythe" address-list, simplified).
/// let raw = b" \"James Smythe\" <james@example.com>";
/// let parsed = parse_header_typed(HeaderForm::Addresses, raw);
/// assert_eq!(
/// parsed,
/// HeaderValueTyped::Addresses(vec![EmailAddress::new(
/// Some("James Smythe".to_owned()),
/// Some("james@example.com".to_owned()),
/// )]),
/// );
/// ```
///
/// ## GroupedAddresses (RFC 8621 §4.1.2.4)
///
/// ```
/// use mime_tree::{parse_header_typed, AddressGroup, EmailAddress, HeaderForm, HeaderValueTyped};
///
/// let raw = b"Friends: alice@example.com, bob@example.com;";
/// let parsed = parse_header_typed(HeaderForm::GroupedAddresses, raw);
/// assert_eq!(
/// parsed,
/// HeaderValueTyped::GroupedAddresses(vec![AddressGroup::new(
/// Some("Friends".to_owned()),
/// vec![
/// EmailAddress::new(None, Some("alice@example.com".to_owned())),
/// EmailAddress::new(None, Some("bob@example.com".to_owned())),
/// ],
/// )]),
/// );
/// ```
///
/// ## MessageIds (RFC 8621 §4.1.2.5)
///
/// ```
/// use mime_tree::{parse_header_typed, HeaderForm, HeaderValueTyped};
///
/// let raw = b"<abc@example.com> <def@example.com>";
/// let parsed = parse_header_typed(HeaderForm::MessageIds, raw);
/// assert_eq!(
/// parsed,
/// HeaderValueTyped::MessageIds(vec![
/// "abc@example.com".to_owned(),
/// "def@example.com".to_owned(),
/// ]),
/// );
/// ```
///
/// ## Date (RFC 5322 §3.3 / RFC 3339)
///
/// ```
/// use mime_tree::{parse_header_typed, HeaderForm, HeaderValueTyped};
///
/// let raw = b" Fri, 21 Nov 1997 09:55:06 -0600";
/// let parsed = parse_header_typed(HeaderForm::Date, raw);
/// if let HeaderValueTyped::DateTime(Some(dt)) = parsed {
/// assert_eq!(dt.to_rfc3339(), "1997-11-21T09:55:06-06:00");
/// } else {
/// panic!("expected DateTime");
/// }
/// ```
///
/// ## URLs (RFC 8621 §4.1.2.7 / RFC 2369)
///
/// ```
/// use mime_tree::{parse_header_typed, HeaderForm, HeaderValueTyped};
///
/// // RFC 2369 List-Help with comment after the URL.
/// let raw = b" <mailto:list@host.com?subject=help> (List Instructions)";
/// let parsed = parse_header_typed(HeaderForm::URLs, raw);
/// assert_eq!(
/// parsed,
/// HeaderValueTyped::URLs(vec!["mailto:list@host.com?subject=help".to_owned()]),
/// );
/// ```
///
/// ## Raw (RFC 8621 §4.1.2.1)
///
/// ```
/// use mime_tree::{parse_header_typed, HeaderForm, HeaderValueTyped};
///
/// // Surrounding whitespace stripped; no other transformation.
/// // Encoded-words survive verbatim.
/// let raw = b" Subject line with =?UTF-8?Q?encoded?= words ";
/// let parsed = parse_header_typed(HeaderForm::Raw, raw);
/// assert_eq!(
/// parsed,
/// HeaderValueTyped::Raw("Subject line with =?UTF-8?Q?encoded?= words".to_owned()),
/// );
/// ```
// ---------------------------------------------------------------------------
// Per-form entry points
// ---------------------------------------------------------------------------
//
// Convenience wrappers over `parse_header_typed` for callers that know
// the form statically. Each wrapper unwraps the `HeaderValueTyped`
// variant that `parse_header_typed` is contractually required to return
// for the matching `HeaderForm`, eliminating the boilerplate match at
// every call site. If `parse_header_typed` ever broke that contract, the
// wrapper returns the empty result for the form (defensive, not panic).
/// Parse the header field value as an RFC 8621 §4.1.2.1 Raw form: trim
/// surrounding whitespace, decode bytes as UTF-8 (lossy with U+FFFD on
/// invalid sequences).
///
/// Convenience wrapper over [`parse_header_typed`] with
/// [`HeaderForm::Raw`].
/// Parse the header field value as an RFC 8621 §4.1.2.2 Text form:
/// whitespace unfolded, RFC 2047 encoded-words decoded, Unicode
/// normalised to NFC.
///
/// Convenience wrapper over [`parse_header_typed`] with
/// [`HeaderForm::Text`].
/// Parse the header field value as an RFC 8621 §4.1.2.3 Addresses form:
/// a flat list of mailboxes with group structure discarded.
///
/// Convenience wrapper over [`parse_header_typed`] with
/// [`HeaderForm::Addresses`].
/// Parse the header field value as an RFC 8621 §4.1.2.4 GroupedAddresses
/// form: preserves group structure; a flat mailbox-list is wrapped in a
/// single anonymous group.
///
/// Convenience wrapper over [`parse_header_typed`] with
/// [`HeaderForm::GroupedAddresses`].
/// Parse the header field value as an RFC 8621 §4.1.2.5 MessageIds form:
/// `<...>`-stripped msg-id strings.
///
/// Convenience wrapper over [`parse_header_typed`] with
/// [`HeaderForm::MessageIds`].
/// Parse the header field value as an RFC 8621 §4.1.2.6 Date form: an
/// RFC 5322 §3.3 date-time, or `None` on parse failure.
///
/// Convenience wrapper over [`parse_header_typed`] with
/// [`HeaderForm::Date`].
/// Parse the header field value as an RFC 8621 §4.1.2.7 URLs form: bare
/// URL strings with surrounding angle brackets stripped (RFC 2369).
///
/// Convenience wrapper over [`parse_header_typed`] with
/// [`HeaderForm::URLs`].
/// Parse an existing [`ParsedHeader`]'s value into the requested
/// RFC 8621 parsed form.
///
/// Composition helper: feeds `header.value.as_bytes()` to
/// [`parse_header_typed`]. Equivalent to writing that call by hand —
/// provided so callers do not have to remember to convert through
/// `as_bytes()` and so the typed-header API composes cleanly with the
/// `ParsedHeader` surface produced by [`crate::parse`].
// ---------------------------------------------------------------------------
// Conversion helpers
// ---------------------------------------------------------------------------
/// Trim ASCII whitespace from `s` and return `Some(trimmed)` if the
/// result is non-empty, otherwise `None`.
///
/// Centralises the RFC 8621 §4.1.2.3 / §4.1.2.4 normalisation rule for
/// display names ("trim surrounding white space; an empty result is
/// `None`").
/// Ensure `raw_value` ends with a CRLF, returning the original bytes
/// when it already does (no allocation), and a freshly allocated copy
/// terminated with `\r\n` otherwise.
///
/// mail-parser's `MessageStream` parsers expect header field bodies in
/// a real RFC 5322 stream — i.e. terminated by CRLF. Callers pass the
/// field value with no trailing CRLF; this helper normalises any of
/// {already-CRLF, LF-only, no-line-ending} to CRLF-terminated.
/// Flatten an `Address` (which is either a flat list of mailboxes or a
/// list of groups) into a single `Vec<EmailAddress>`. Used for
/// [`HeaderForm::Addresses`], which per RFC 8621 §4.1.2.3 discards group
/// structure and produces one item per mailbox.
/// Convert an `Address` into a list of groups, per RFC 8621 §4.1.2.4. A
/// flat list of mailboxes is wrapped in a single group with `name = None`.
/// Extract bare msg-id strings from a `HeaderValue` produced by
/// mail-parser's `parse_id`.
///
/// `had_angle_brackets` indicates whether the original raw input
/// contained at least one `<` byte. mail-parser's `parse_id` returns
/// `HeaderValue::Text` both for a single valid bracket-stripped msg-id
/// and for its broken-client recovery branch on input with no brackets at
/// all (returning the lossy UTF-8 of the unparsed bytes). Without this
/// discriminator, malformed input would leak the unparsed bytes into the
/// result vec, violating the RFC 8621 §4.1.2.5 empty-on-malformed
/// contract.
/// Extract URL strings from RFC 2369 / RFC 8621 §4.1.2.7 bracketed
/// list-URL syntax.
///
/// Scans `raw_value` for `<...>` substrings and yields the byte sequence
/// between each matching pair, in order. Bytes outside the brackets are
/// ignored — including comments (`(...)`), CFWS, commas, and any
/// malformed framing — per RFC 8621 §4.1.2.7: "Any value outside of the
/// angle bracket arguments MUST be ignored."
///
/// ASCII whitespace inside a bracketed value is stripped, because RFC
/// 3986 URIs cannot contain literal whitespace; any whitespace seen is a
/// CRLF folding artifact. Non-UTF-8 bracket contents are dropped.
///
/// An unclosed `<` (no matching `>`) is ignored. An empty `<>` is
/// ignored.