email-message 0.7.0

Typed outbound email message and address model
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
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
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
//! MIME content-type, content-disposition, and content-transfer-encoding
//! types.
//!
//! Most of this module, `ContentType`, `MediaType`, `ContentDisposition`,
//! `ContentTransferEncoding`, `ParameterValue`, is **always available**
//! regardless of feature flags. The `mime` Cargo feature gates only
//! [`MimePart`], the multipart/leaf MIME tree used by full-message
//! rendering. A consumer that just wants typed content-type validation
//! can use `email-message` with `default-features = false` and skip the
//! `mime` feature.

use std::fmt::Display;
use std::str::FromStr;

/// MIME content type.
///
/// # Equality and hashing
///
/// `PartialEq` / `Eq` / `Hash` are derived. To make derived equality
/// match RFC 2045 §5.1 semantics (type, subtype, and parameter names
/// are case-insensitive), construction lowercases those tokens. Parameter
/// values are preserved as-is because their case sensitivity depends on
/// the parameter (`boundary` is case-sensitive per RFC 2046 §5.1.1;
/// `charset` is case-insensitive per RFC 2046 §4.1.2 but the kernel
/// leaves the caller's bytes intact for round-trip fidelity).
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ContentType(String);

impl ContentType {
    #[must_use]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Borrowed type/subtype view, with no parameters.
    ///
    /// Cheap: it slices the stored string; no allocation. Validation guarantees
    /// a well-formed `type/subtype` prefix exists.
    #[must_use]
    pub fn media_type(&self) -> MediaType<'_> {
        let head = self.0.split(';').next().unwrap_or("").trim();
        let (type_, subtype) = head.split_once('/').unwrap_or((head, ""));
        MediaType { type_, subtype }
    }

    /// Iterate `(name, value)` parameter pairs in declaration order.
    ///
    /// Quoted values are returned with surrounding quotes stripped and
    /// backslash escapes resolved.
    pub fn parameters(&self) -> impl Iterator<Item = (&str, ParameterValue<'_>)> {
        let mut segments = split_content_type_segments(self.0.as_str()).into_iter();
        // Skip the type/subtype segment.
        let _ = segments.next();
        segments.filter_map(|segment| {
            let (name, value) = segment.trim().split_once('=')?;
            Some((name.trim(), ParameterValue::from_raw(value.trim())))
        })
    }

    /// Look up a parameter by case-insensitive name.
    #[must_use]
    pub fn parameter(&self, name: &str) -> Option<ParameterValue<'_>> {
        self.parameters()
            .find(|(key, _)| key.eq_ignore_ascii_case(name))
            .map(|(_, value)| value)
    }

    /// Convenience accessor for the `boundary` parameter (multipart only).
    #[must_use]
    pub fn boundary(&self) -> Option<ParameterValue<'_>> {
        self.parameter("boundary")
    }

    /// Convenience accessor for the `charset` parameter.
    #[must_use]
    pub fn charset(&self) -> Option<ParameterValue<'_>> {
        self.parameter("charset")
    }
}

/// Borrowed view of a content-type's `type/subtype`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct MediaType<'a> {
    type_: &'a str,
    subtype: &'a str,
}

impl<'a> MediaType<'a> {
    #[must_use]
    pub const fn type_(&self) -> &'a str {
        self.type_
    }

    #[must_use]
    pub const fn subtype(&self) -> &'a str {
        self.subtype
    }

    #[must_use]
    pub fn is_text(&self) -> bool {
        self.type_.eq_ignore_ascii_case("text")
    }

    #[must_use]
    pub fn is_multipart(&self) -> bool {
        self.type_.eq_ignore_ascii_case("multipart")
    }

    #[must_use]
    pub fn is_image(&self) -> bool {
        self.type_.eq_ignore_ascii_case("image")
    }

    /// Case-insensitive compare against a `"type/subtype"` literal.
    #[must_use]
    pub fn matches(&self, expected: &str) -> bool {
        let Some((ty, sub)) = expected.split_once('/') else {
            return false;
        };
        self.type_.eq_ignore_ascii_case(ty) && self.subtype.eq_ignore_ascii_case(sub)
    }
}

/// Borrowed parameter value, lazily resolving quoted-string escapes.
#[derive(Clone, Debug)]
pub struct ParameterValue<'a> {
    raw: &'a str,
}

impl<'a> ParameterValue<'a> {
    fn from_raw(raw: &'a str) -> Self {
        Self { raw }
    }

    /// Raw textual form as it appears in the header (still quoted/escaped if it
    /// was emitted that way).
    #[must_use]
    pub const fn as_raw(&self) -> &'a str {
        self.raw
    }

    /// Returns the unquoted, unescaped string. For unquoted values this is a
    /// borrow; for quoted values it allocates only to materialize the escapes.
    #[must_use]
    pub fn unquoted(&self) -> std::borrow::Cow<'a, str> {
        let raw = self.raw;
        if !raw.starts_with('"') || !raw.ends_with('"') || raw.len() < 2 {
            return std::borrow::Cow::Borrowed(raw);
        }

        let inner = &raw[1..raw.len() - 1];
        if !inner.contains('\\') {
            return std::borrow::Cow::Borrowed(inner);
        }

        let mut out = String::with_capacity(inner.len());
        let mut escaped = false;
        for ch in inner.chars() {
            if escaped {
                out.push(ch);
                escaped = false;
            } else if ch == '\\' {
                escaped = true;
            } else {
                out.push(ch);
            }
        }
        std::borrow::Cow::Owned(out)
    }
}

impl PartialEq<&str> for ParameterValue<'_> {
    fn eq(&self, other: &&str) -> bool {
        self.unquoted().as_ref() == *other
    }
}

impl Display for ContentType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("content type must have a type/subtype form")]
pub struct ContentTypeParseError;

impl FromStr for ContentType {
    type Err = ContentTypeParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        normalize_parameterized_value(s, true)
            .map(Self)
            .ok_or(ContentTypeParseError)
    }
}

fn is_mime_token(value: &str) -> bool {
    value.bytes().all(is_mime_token_byte)
}

fn split_content_type_segments(value: &str) -> Vec<&str> {
    let mut segments = Vec::new();
    let mut start = 0;
    let mut in_quotes = false;
    let mut escaped = false;

    for (index, ch) in value.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }

        if in_quotes && ch == '\\' {
            escaped = true;
            continue;
        }

        if ch == '"' {
            in_quotes = !in_quotes;
            continue;
        }

        if ch == ';' && !in_quotes {
            segments.push(&value[start..index]);
            start = index + ch.len_utf8();
        }
    }

    segments.push(&value[start..]);
    segments
}

const fn is_mime_token_byte(byte: u8) -> bool {
    matches!(
        byte,
        b'!' | b'#'
            | b'$'
            | b'%'
            | b'&'
            | b'\''
            | b'*'
            | b'+'
            | b'-'
            | b'.'
            | b'^'
            | b'_'
            | b'`'
            | b'|'
            | b'~'
            | b'0'..=b'9'
            | b'A'..=b'Z'
            | b'a'..=b'z'
    )
}

fn is_parameter_value(value: &str) -> bool {
    if value.starts_with('"') {
        return is_quoted_parameter_value(value);
    }

    is_mime_token(value)
}

fn is_quoted_parameter_value(value: &str) -> bool {
    if !(value.ends_with('"') && value.len() >= 2) {
        return false;
    }

    let mut escaped = false;
    for byte in value[1..value.len() - 1].bytes() {
        if escaped {
            if is_forbidden_quoted_parameter_byte(byte) {
                return false;
            }
            escaped = false;
            continue;
        }

        if byte == b'\\' {
            escaped = true;
            continue;
        }

        if byte == b'"' || is_forbidden_quoted_parameter_byte(byte) {
            return false;
        }
    }

    !escaped
}

/// Reject NUL, CR, LF, and any non-tab ASCII control character inside a
/// MIME quoted parameter. Matches the byte-discipline `validate_header`
/// (in `crate::message`) and `push_header_line` (in
/// `email_message_wire::rfc822`) apply to header values, so a parsed
/// `ContentType` cannot carry bytes the wire renderer would later
/// reject (META-001 R3 invariant).
const fn is_forbidden_quoted_parameter_byte(byte: u8) -> bool {
    byte != b'\t' && byte.is_ascii_control()
}

impl TryFrom<&str> for ContentType {
    type Error = ContentTypeParseError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::from_str(value)
    }
}

impl From<ContentType> for String {
    fn from(value: ContentType) -> Self {
        value.0
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for ContentType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for ContentType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        value.parse().map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for ContentType {
    fn inline_schema() -> bool {
        true
    }

    fn schema_name() -> std::borrow::Cow<'static, str> {
        "ContentType".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::ContentType").into()
    }

    fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        schemars::json_schema!({
            "type": "string",
            "description": "MIME Content-Type field value"
        })
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for ContentType {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let value = match u.int_in_range::<u8>(0..=4)? {
            0 => "text/plain",
            1 => "text/html; charset=utf-8",
            2 => "application/octet-stream",
            3 => "image/png",
            _ => "multipart/mixed; boundary=boundary",
        };
        value.parse().map_err(|_| arbitrary::Error::IncorrectFormat)
    }
}

/// MIME content-transfer-encoding (RFC 2045 §6).
///
/// The five RFC-defined values are explicit variants; any other syntactically
/// valid mime-token (e.g. an `x-` extension) round-trips through `Other`.
///
/// # Casing
///
/// RFC 2045 §6.1 says encoding names are case-insensitive. Both the
/// known-variant parser and the [`Other`] branch normalize to ASCII
/// lowercase on construction, so equality and hashing through the
/// derived impls are case-insensitive automatically: `Other("Base64")`
/// is unreachable (parses to [`Base64`] instead) and `Other("X-MyEnc")`
/// stores `"x-myenc"`.
///
/// [`Base64`]: Self::Base64
/// [`Other`]: Self::Other
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ContentTransferEncoding {
    SevenBit,
    EightBit,
    Binary,
    QuotedPrintable,
    Base64,
    Other(String),
}

impl ContentTransferEncoding {
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::SevenBit => "7bit",
            Self::EightBit => "8bit",
            Self::Binary => "binary",
            Self::QuotedPrintable => "quoted-printable",
            Self::Base64 => "base64",
            Self::Other(value) => value.as_str(),
        }
    }
}

impl Display for ContentTransferEncoding {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("content-transfer-encoding cannot be empty")]
pub struct ContentTransferEncodingParseError;

impl FromStr for ContentTransferEncoding {
    type Err = ContentTransferEncodingParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let value = s.trim();
        if value.is_empty() || !is_mime_token(value) {
            return Err(ContentTransferEncodingParseError);
        }
        Ok(if value.eq_ignore_ascii_case("7bit") {
            Self::SevenBit
        } else if value.eq_ignore_ascii_case("8bit") {
            Self::EightBit
        } else if value.eq_ignore_ascii_case("binary") {
            Self::Binary
        } else if value.eq_ignore_ascii_case("quoted-printable") {
            Self::QuotedPrintable
        } else if value.eq_ignore_ascii_case("base64") {
            Self::Base64
        } else {
            Self::Other(value.to_ascii_lowercase())
        })
    }
}

impl TryFrom<&str> for ContentTransferEncoding {
    type Error = ContentTransferEncodingParseError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::from_str(value)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for ContentTransferEncoding {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for ContentTransferEncoding {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        value.parse().map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for ContentTransferEncoding {
    fn inline_schema() -> bool {
        true
    }

    fn schema_name() -> std::borrow::Cow<'static, str> {
        "ContentTransferEncoding".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::ContentTransferEncoding").into()
    }

    fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        schemars::json_schema!({
            "type": "string",
            "description": "RFC 2045 Content-Transfer-Encoding token"
        })
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for ContentTransferEncoding {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        Ok(match u.int_in_range::<u8>(0..=5)? {
            0 => Self::SevenBit,
            1 => Self::EightBit,
            2 => Self::Binary,
            3 => Self::QuotedPrintable,
            4 => Self::Base64,
            _ => Self::Other("x-experimental".to_owned()),
        })
    }
}

/// MIME content-disposition token (RFC 2183).
///
/// # Equality and hashing
///
/// Same shape as [`ContentType`]: construction lowercases the disposition
/// kind and parameter names, then `PartialEq` / `Eq` / `Hash` compare that
/// normalized string. RFC 2183 §3 makes the disposition type and parameter
/// names case-insensitive but leaves parameter value case sensitivity
/// dependent on the parameter. The kernel preserves parameter values
/// verbatim; for semantic comparison route through the disposition's
/// accessors rather than comparing raw input strings.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ContentDisposition(String);

impl ContentDisposition {
    #[must_use]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Borrowed disposition kind (`"inline"`, `"attachment"`, or an
    /// `x-` extension), with no parameters.
    ///
    /// Cheap: it slices the stored string; no allocation. Validation
    /// guarantees a well-formed disposition token prefix exists.
    #[must_use]
    pub fn kind(&self) -> &str {
        self.0.split(';').next().unwrap_or("").trim()
    }

    /// Iterate `(name, value)` parameter pairs in declaration order.
    ///
    /// Quoted values are returned with surrounding quotes stripped and
    /// backslash escapes resolved, mirroring [`ContentType::parameters`].
    pub fn parameters(&self) -> impl Iterator<Item = (&str, ParameterValue<'_>)> {
        let mut segments = split_content_type_segments(self.0.as_str()).into_iter();
        // Skip the disposition-kind segment.
        let _ = segments.next();
        segments.filter_map(|segment| {
            let (name, value) = segment.trim().split_once('=')?;
            Some((name.trim(), ParameterValue::from_raw(value.trim())))
        })
    }

    /// Look up a parameter by case-insensitive name.
    #[must_use]
    pub fn parameter(&self, name: &str) -> Option<ParameterValue<'_>> {
        self.parameters()
            .find(|(key, _)| key.eq_ignore_ascii_case(name))
            .map(|(_, value)| value)
    }

    /// Convenience accessor for the `filename` parameter.
    ///
    /// RFC 2183 §2.3 defines this as the suggested filename a recipient's
    /// mail client should use when saving the attachment to disk. For
    /// non-ASCII filenames the kernel emits `filename*` (RFC 2231
    /// charset/language extension); this accessor returns `filename` when
    /// present and otherwise falls back to `filename*`.
    #[must_use]
    pub fn filename(&self) -> Option<ParameterValue<'_>> {
        self.parameter("filename")
            .or_else(|| self.parameter("filename*"))
    }

    /// Returns `true` if the disposition kind is `inline`
    /// (case-insensitive).
    #[must_use]
    pub fn is_inline(&self) -> bool {
        self.kind().eq_ignore_ascii_case("inline")
    }

    /// Returns `true` if the disposition kind is `attachment`
    /// (case-insensitive).
    #[must_use]
    pub fn is_attachment(&self) -> bool {
        self.kind().eq_ignore_ascii_case("attachment")
    }
}

impl Display for ContentDisposition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("content-disposition cannot be empty")]
pub struct ContentDispositionParseError;

impl FromStr for ContentDisposition {
    type Err = ContentDispositionParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        normalize_parameterized_value(s, false)
            .map(Self)
            .ok_or(ContentDispositionParseError)
    }
}

impl TryFrom<&str> for ContentDisposition {
    type Error = ContentDispositionParseError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::from_str(value)
    }
}

/// Validate and normalize a parameterized header value (`Content-Type` shape
/// or `Content-Disposition` shape). When `with_subtype` is true, the head
/// must be `type/subtype`; otherwise it must be a single MIME token.
///
/// Lowercases the type/subtype tokens and parameter names so derived
/// equality matches RFC 2045 §5.1 semantics. Parameter values are
/// preserved verbatim. Returns `None` if the input fails any validation
/// rule the previous bool-returning checks enforced.
fn normalize_parameterized_value(value: &str, with_subtype: bool) -> Option<String> {
    let value = value.trim();
    if value.is_empty() {
        return None;
    }

    let segments = split_content_type_segments(value);
    let mut parts = segments.into_iter();
    let head = parts.next()?.trim();

    let canonical_head = if with_subtype {
        let (ty, subtype) = head.split_once('/')?;
        if ty.is_empty()
            || subtype.is_empty()
            || subtype.contains('/')
            || !is_mime_token(ty)
            || !is_mime_token(subtype)
        {
            return None;
        }
        format!(
            "{}/{}",
            ty.to_ascii_lowercase(),
            subtype.to_ascii_lowercase()
        )
    } else {
        if head.is_empty() || !is_mime_token(head) {
            return None;
        }
        head.to_ascii_lowercase()
    };

    let mut canonical = canonical_head;
    for parameter in parts {
        let parameter = parameter.trim();
        let (name, raw_value) = parameter.split_once('=')?;
        let name = name.trim();
        let raw_value = raw_value.trim();
        if name.is_empty()
            || raw_value.is_empty()
            || !is_mime_token(name)
            || !is_parameter_value(raw_value)
        {
            return None;
        }
        canonical.push_str("; ");
        canonical.push_str(&name.to_ascii_lowercase());
        canonical.push('=');
        canonical.push_str(raw_value);
    }

    Some(canonical)
}

#[cfg(feature = "serde")]
impl serde::Serialize for ContentDisposition {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for ContentDisposition {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        value.parse().map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for ContentDisposition {
    fn inline_schema() -> bool {
        true
    }

    fn schema_name() -> std::borrow::Cow<'static, str> {
        "ContentDisposition".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::ContentDisposition").into()
    }

    fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        schemars::json_schema!({
            "type": "string",
            "description": "RFC 2183 Content-Disposition field value"
        })
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for ContentDisposition {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let value = match u.int_in_range::<u8>(0..=2)? {
            0 => "inline",
            1 => "attachment",
            _ => "attachment; filename=example.txt",
        };
        value.parse().map_err(|_| arbitrary::Error::IncorrectFormat)
    }
}

/// Low-level MIME tree node, gated behind the `mime` Cargo feature.
///
/// `MimePart` is the kernel's escape hatch for callers building exotic
/// MIME structures (custom multipart shapes, hand-rolled
/// transfer-encoding choices, etc.). High-level paths through
/// [`Body::Text`](crate::Body) / `Body::Html` / `Body::TextAndHtml` cover
/// the common cases and apply byte-discipline (auto-promote non-ASCII
/// text to base64, etc.) on the caller's behalf.
///
/// # Body byte-discipline is the caller's responsibility
///
/// Constructing `MimePart::Leaf` directly bypasses the kernel's
/// auto-promotion path. The wire renderer enforces *header* invariants
/// strictly (rejects raw CR / LF / NUL / non-tab control chars in any
/// header value, regardless of `Content-Transfer-Encoding`), but it
/// **trusts the caller's bytes** for body content under any transfer
/// encoding other than `base64` / `quoted-printable`. That includes
/// `7bit`, `8bit`, `binary`, and any `Other(...)` value: the renderer
/// emits the body verbatim. RFC 2045 §6.2 forbids bytes > 127 under
/// `7bit` and forbids bare CR / LF under both `7bit` and `8bit`;
/// callers building `MimePart::Leaf` with a non-base64 / non-QP
/// encoding must satisfy those invariants themselves, or downstream
/// MTAs may reject the message.
///
/// # Variant set
///
/// Deliberately *not* `#[non_exhaustive]`. RFC 2046 closes MIME
/// parts to exactly `discrete` (Leaf) and `composite` (Multipart);
/// the kernel cannot honestly add a third variant without an RFC
/// update. The exhaustive `match` shape lets downstream callers
/// type-cover both arms without an `_ =>` clause.
///
/// # Untrusted-deserialize caveat
///
/// `MimePart::Multipart { parts: Vec<Self> }` is recursive: any
/// caller deserializing a `MimePart` (or a `Body` containing one)
/// from untrusted input must pre-bound the input length and the
/// recursion depth. `serde_json` defaults to a 128-frame recursion
/// limit which is safe; other formats (e.g. `serde_yaml`,
/// `bincode`, `rmp-serde`, `serde_cbor`) may not, and a deeply
/// nested attacker payload yields a `MimePart` value of arbitrary
/// depth. The wire renderer (`email_message_wire::render_rfc822`)
/// enforces a `MAX_MULTIPART_DEPTH` cap on outbound trees, including
/// up to two frames of attachment-wrapping when inline and/or regular
/// attachments are present, but other consumers of a deserialized
/// `MimePart` (e.g. arbitrary caller code that walks the tree) must
/// defend themselves.
#[cfg(feature = "mime")]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MimePart {
    Leaf {
        content_type: ContentType,
        content_transfer_encoding: Option<ContentTransferEncoding>,
        content_disposition: Option<ContentDisposition>,
        body: Vec<u8>,
    },
    Multipart {
        content_type: ContentType,
        boundary: Option<String>,
        parts: Vec<Self>,
    },
}

#[cfg(all(feature = "mime", feature = "serde"))]
impl serde::Serialize for MimePart {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use base64::Engine as _;
        use serde::ser::SerializeStruct as _;

        match self {
            Self::Leaf {
                content_type,
                content_transfer_encoding,
                content_disposition,
                body,
            } => {
                let mut len = 3; // type + content_type + body
                if content_transfer_encoding.is_some() {
                    len += 1;
                }
                if content_disposition.is_some() {
                    len += 1;
                }
                let encoded = base64::engine::general_purpose::STANDARD.encode(body);
                let mut value = serializer.serialize_struct("MimePart", len)?;
                value.serialize_field("type", "leaf")?;
                value.serialize_field("content_type", content_type)?;
                if let Some(cte) = content_transfer_encoding {
                    value.serialize_field("content_transfer_encoding", cte)?;
                }
                if let Some(cd) = content_disposition {
                    value.serialize_field("content_disposition", cd)?;
                }
                value.serialize_field("body", &encoded)?;
                value.end()
            }
            Self::Multipart {
                content_type,
                boundary,
                parts,
            } => {
                let mut len = 3; // type + content_type + parts
                if boundary.is_some() {
                    len += 1;
                }
                let mut value = serializer.serialize_struct("MimePart", len)?;
                value.serialize_field("type", "multipart")?;
                value.serialize_field("content_type", content_type)?;
                if let Some(b) = boundary {
                    value.serialize_field("boundary", b)?;
                }
                value.serialize_field("parts", parts)?;
                value.end()
            }
        }
    }
}

#[cfg(all(feature = "mime", feature = "serde"))]
impl<'de> serde::Deserialize<'de> for MimePart {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use base64::Engine as _;

        #[derive(serde::Deserialize)]
        #[serde(tag = "type", rename_all = "snake_case")]
        enum RawMimePart {
            Leaf {
                content_type: ContentType,
                #[serde(default)]
                content_transfer_encoding: Option<ContentTransferEncoding>,
                #[serde(default)]
                content_disposition: Option<ContentDisposition>,
                body: String,
            },
            Multipart {
                content_type: ContentType,
                #[serde(default)]
                boundary: Option<String>,
                #[serde(default)]
                parts: Vec<MimePart>,
            },
        }

        Ok(match RawMimePart::deserialize(deserializer)? {
            RawMimePart::Leaf {
                content_type,
                content_transfer_encoding,
                content_disposition,
                body,
            } => {
                let decoded = base64::engine::general_purpose::STANDARD
                    .decode(body.as_bytes())
                    .map_err(|err| {
                        serde::de::Error::custom(format!("invalid base64 MIME body: {err}"))
                    })?;
                Self::Leaf {
                    content_type,
                    content_transfer_encoding,
                    content_disposition,
                    body: decoded,
                }
            }
            RawMimePart::Multipart {
                content_type,
                boundary,
                parts,
            } => Self::Multipart {
                content_type,
                boundary,
                parts,
            },
        })
    }
}

#[cfg(all(feature = "mime", feature = "schemars"))]
impl schemars::JsonSchema for MimePart {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "MimePart".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::MimePart").into()
    }

    /// MIME parts have no RFC 5322 string form, so this schema is *not*
    /// wrapped in an `rfc5322-string-compat` `oneOf: [object, string]`
    /// the way `Mailbox` / `Group` / `Address` are. The asymmetry is
    /// deliberate: there is no producer-side wire shape for "MIME part
    /// as a header-like string" to migrate from.
    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        let recursive = generator.subschema_for::<MimePart>();
        schemars::json_schema!({
            "oneOf": [
                {
                    "type": "object",
                    "properties": {
                        "type": {"const": "leaf"},
                        "content_type": {
                            "type": "string",
                            "description": "MIME Content-Type field value"
                        },
                        "content_transfer_encoding": {
                            "type": "string",
                            "description": "RFC 2045 Content-Transfer-Encoding token"
                        },
                        "content_disposition": {
                            "type": "string",
                            "description": "RFC 2183 Content-Disposition field value"
                        },
                        "body": {
                            "type": "string",
                            "contentEncoding": "base64",
                            "description": "Base64-encoded MIME part body (RFC 4648, with padding)"
                        }
                    },
                    "required": ["type", "content_type", "body"]
                },
                {
                    "type": "object",
                    "properties": {
                        "type": {"const": "multipart"},
                        "content_type": {
                            "type": "string",
                            "description": "MIME Content-Type field value"
                        },
                        "boundary": {"type": "string"},
                        "parts": {"type": "array", "items": recursive}
                    },
                    "required": ["type", "content_type", "parts"]
                }
            ]
        })
    }
}

#[cfg(test)]
mod tests {
    use super::{ContentTransferEncoding, ContentType};

    #[test]
    fn content_type_accepts_valid_media_types_and_parameters() {
        for value in [
            "text/plain",
            "text/plain;charset=utf-8",
            "multipart/related; type=\"text/html\"",
            "application/octet-stream; name=\"a;b.txt\"",
        ] {
            assert!(
                ContentType::try_from(value).is_ok(),
                "expected valid content type: {value}"
            );
        }
    }

    #[test]
    fn content_type_rejects_invalid_media_types() {
        for value in [
            "text/",
            "/plain",
            "text/plain/html",
            "text /plain",
            "text/plain; charset",
            "text/plain; charset=\"unterminated",
        ] {
            assert!(
                ContentType::try_from(value).is_err(),
                "expected invalid content type: {value}"
            );
        }
    }

    #[test]
    fn content_type_rejects_quoted_parameter_with_control_chars() {
        // Direct bytes, NUL, BEL, VT, ESC must be rejected to match the
        // wire renderer's `push_header_line` byte discipline.
        for value in [
            "text/plain; name=\"x\u{0}y\"",
            "text/plain; name=\"x\u{07}y\"",
            "text/plain; name=\"x\u{0B}y\"",
            "text/plain; name=\"x\u{1B}y\"",
        ] {
            assert!(
                ContentType::try_from(value).is_err(),
                "expected control-char rejection: {value:?}"
            );
        }
    }

    #[test]
    fn content_type_rejects_quoted_parameter_with_escaped_control_chars() {
        // Even after a `\` escape, control chars are still rejected.
        for value in [
            "text/plain; name=\"x\\\u{0}y\"",
            "text/plain; name=\"x\\\u{07}y\"",
        ] {
            assert!(
                ContentType::try_from(value).is_err(),
                "expected escaped-control-char rejection: {value:?}"
            );
        }
    }

    #[test]
    fn content_type_accepts_tab_inside_quoted_parameter() {
        // Tab is the documented exception in the byte-discipline rule.
        assert!(ContentType::try_from("text/plain; name=\"a\tb\"").is_ok());
    }

    #[test]
    fn content_type_media_type_view_splits_type_and_subtype() {
        let ct: ContentType = "text/plain; charset=utf-8".parse().unwrap();
        let media = ct.media_type();
        assert_eq!(media.type_(), "text");
        assert_eq!(media.subtype(), "plain");
        assert!(media.is_text());
        assert!(!media.is_multipart());
        assert!(media.matches("text/plain"));
        assert!(media.matches("TEXT/PLAIN"));
    }

    #[test]
    fn content_type_parameter_lookup_is_case_insensitive_and_unquotes() {
        let ct: ContentType = "multipart/mixed; Boundary=\"abc\\\"def\"".parse().unwrap();
        let boundary = ct.boundary().expect("boundary present");
        assert_eq!(boundary.as_raw(), "\"abc\\\"def\"");
        assert_eq!(boundary.unquoted().as_ref(), "abc\"def");
    }

    #[test]
    fn content_type_parameters_iterates_in_declaration_order() {
        let ct: ContentType = "text/html; charset=utf-8; boundary=x".parse().unwrap();
        let pairs: Vec<(String, String)> = ct
            .parameters()
            .map(|(k, v)| (k.to_owned(), v.unquoted().into_owned()))
            .collect();
        assert_eq!(
            pairs,
            vec![
                ("charset".to_owned(), "utf-8".to_owned()),
                ("boundary".to_owned(), "x".to_owned()),
            ]
        );
    }

    #[test]
    fn content_transfer_encoding_canonicalizes_known_tokens() {
        assert_eq!(
            "Base64"
                .parse::<ContentTransferEncoding>()
                .unwrap()
                .as_str(),
            "base64"
        );
        assert_eq!(
            "7BIT".parse::<ContentTransferEncoding>().unwrap().as_str(),
            "7bit"
        );
        assert_eq!(
            "Quoted-Printable"
                .parse::<ContentTransferEncoding>()
                .unwrap(),
            ContentTransferEncoding::QuotedPrintable
        );

        let other: ContentTransferEncoding = "x-my-encoding".parse().unwrap();
        assert_eq!(
            other,
            ContentTransferEncoding::Other("x-my-encoding".to_owned())
        );
        assert_eq!(other.as_str(), "x-my-encoding");
    }

    #[test]
    fn content_disposition_kind_and_parameter_accessors() {
        use super::ContentDisposition;
        let cd: ContentDisposition = "attachment; filename=\"report.pdf\""
            .parse()
            .expect("disposition should parse");
        assert_eq!(cd.kind(), "attachment");
        assert!(cd.is_attachment());
        assert!(!cd.is_inline());
        let filename = cd.filename().expect("filename present");
        assert_eq!(filename.unquoted().as_ref(), "report.pdf");
    }

    #[test]
    fn content_disposition_filename_falls_back_to_extended_parameter() {
        use super::ContentDisposition;
        let cd: ContentDisposition = "attachment; filename*=utf-8''f%C3%A1jl.txt"
            .parse()
            .expect("disposition should parse");

        let filename = cd.filename().expect("filename* present");
        assert_eq!(filename.as_raw(), "utf-8''f%C3%A1jl.txt");
    }

    #[test]
    fn content_disposition_inline_kind_is_case_insensitive() {
        use super::ContentDisposition;
        let cd: ContentDisposition = "INLINE".parse().expect("disposition should parse");
        assert!(cd.is_inline());
        assert!(!cd.is_attachment());
    }

    #[test]
    fn content_disposition_parameters_iterates_in_declaration_order() {
        use super::ContentDisposition;
        let cd: ContentDisposition = "attachment; filename=report.pdf; size=42".parse().unwrap();
        let pairs: Vec<(String, String)> = cd
            .parameters()
            .map(|(k, v)| (k.to_owned(), v.unquoted().into_owned()))
            .collect();
        assert_eq!(
            pairs,
            vec![
                ("filename".to_owned(), "report.pdf".to_owned()),
                ("size".to_owned(), "42".to_owned()),
            ]
        );
    }

    #[test]
    fn content_disposition_parameter_lookup_is_case_insensitive() {
        use super::ContentDisposition;
        let cd: ContentDisposition = "attachment; FileName=\"x.txt\"".parse().unwrap();
        assert_eq!(
            cd.parameter("filename").unwrap().unquoted().as_ref(),
            "x.txt"
        );
        assert_eq!(
            cd.parameter("FILENAME").unwrap().unquoted().as_ref(),
            "x.txt"
        );
    }

    #[test]
    fn content_transfer_encoding_other_is_case_insensitive() {
        // RFC 2045 §6.1, encoding names are case-insensitive. Two
        // differently-cased spellings of the same x-* extension must
        // compare equal and hash to the same value.
        let a: ContentTransferEncoding = "X-MyEnc".parse().unwrap();
        let b: ContentTransferEncoding = "x-myenc".parse().unwrap();
        let c: ContentTransferEncoding = "X-MYENC".parse().unwrap();
        assert_eq!(a, b);
        assert_eq!(a, c);
        assert_eq!(a.as_str(), "x-myenc");
        assert_eq!(c.as_str(), "x-myenc");

        // Same value can be safely used as a HashMap/HashSet key.
        use std::collections::HashSet;
        let mut set: HashSet<ContentTransferEncoding> = HashSet::new();
        set.insert(a);
        assert!(set.contains(&b));
        assert!(set.contains(&c));
    }

    #[test]
    fn content_type_eq_is_case_insensitive_after_normalize() {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let upper = ContentType::try_from("TEXT/PLAIN; CHARSET=UTF-8").unwrap();
        let lower = ContentType::try_from("text/plain; charset=UTF-8").unwrap();

        assert_eq!(upper, lower);
        assert_eq!(upper.as_str(), "text/plain; charset=UTF-8");

        let mut h_u = DefaultHasher::new();
        upper.hash(&mut h_u);
        let mut h_l = DefaultHasher::new();
        lower.hash(&mut h_l);
        assert_eq!(h_u.finish(), h_l.finish());

        // Parameter values are preserved as-is (case-sensitive per RFC 2046
        // §5.1.1 for `boundary`).
        let preserved = ContentType::try_from("multipart/mixed; BOUNDARY=\"AbC\"").unwrap();
        assert_eq!(preserved.as_str(), "multipart/mixed; boundary=\"AbC\"");
    }

    #[test]
    fn content_disposition_eq_is_case_insensitive_after_normalize() {
        use super::ContentDisposition;
        let upper = ContentDisposition::try_from("ATTACHMENT; FILENAME=\"x.pdf\"").unwrap();
        let lower = ContentDisposition::try_from("attachment; filename=\"x.pdf\"").unwrap();

        assert_eq!(upper, lower);
        assert_eq!(upper.as_str(), "attachment; filename=\"x.pdf\"");
    }
}