Repository: duesee/imap-codec (language: rust)

Issue:
Add quirk to map NIL body encoding to empty string
https://github.com/pimalaya/himalaya/issues/525#issuecomment-2618088354


Discussion / hints:

Fix patch:
diff --git a/imap-codec/Cargo.toml b/imap-codec/Cargo.toml
index 0266d848..999620d6 100644
--- a/imap-codec/Cargo.toml
+++ b/imap-codec/Cargo.toml
@@ -28,6 +28,7 @@ quirk = [
     "quirk_rectify_numbers",
     "quirk_trailing_space",
     "quirk_spaces_between_addresses",
+    "quirk_body_fld_enc_nil_to_empty",
 ]
 # Make `\r` in `\r\n` optional.
 quirk_crlf_relaxed = []
@@ -41,6 +42,8 @@ quirk_rectify_numbers = []
 quirk_spaces_between_addresses = []
 # Accept a trailing space in `STATUS` data response.
 quirk_trailing_space = []
+# Encode NIL `body-fld-enc` as empty string.
+quirk_body_fld_enc_nil_to_empty = []
 
 # <Forward to imap-types>
 arbitrary = ["imap-types/arbitrary"]
diff --git a/imap-codec/src/body.rs b/imap-codec/src/body.rs
index b064d614..49b8646a 100644
--- a/imap-codec/src/body.rs
+++ b/imap-codec/src/body.rs
@@ -263,8 +263,21 @@ pub(crate) fn body_fld_desc(input: &[u8]) -> IMAPResult<&[u8], NString> {
 /// `body-fld-enc = string`
 ///
 /// TODO: why the special case?
+///
+/// # Quirks
+///
+/// The following erroneous content observed:
+///
+/// * A NIL value in maddy.
 pub(crate) fn body_fld_enc(input: &[u8]) -> IMAPResult<&[u8], IString> {
-    string(input)
+    #[cfg(not(feature = "quirk_body_fld_enc_nil_to_empty"))]
+    return string(input);
+
+    #[cfg(feature = "quirk_body_fld_enc_nil_to_empty")]
+    map(nstring, |enc| match enc.0 {
+        Some(enc) => enc,
+        None => IString::try_from("").unwrap(),
+    })(input)
 }
 
 #[inline]


Test patch: