Merge pull request 'add a raw field to mime' (#22) from better_access_to_bytes into main

Reviewed-on: #22
This commit is contained in:
Quentin 2023-08-16 14:18:51 +00:00
commit 5cff5510ac
4 changed files with 17 additions and 4 deletions

2
Cargo.lock generated
View file

@ -70,7 +70,7 @@ checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]] [[package]]
name = "eml-codec" name = "eml-codec"
version = "0.1.0" version = "0.1.2"
dependencies = [ dependencies = [
"base64", "base64",
"chrono", "chrono",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "eml-codec" name = "eml-codec"
version = "0.1.1" version = "0.1.2"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://git.deuxfleurs.fr/Deuxfleurs/eml-codec" repository = "https://git.deuxfleurs.fr/Deuxfleurs/eml-codec"

View file

@ -63,6 +63,7 @@ pub struct NaiveMIME<'a> {
pub description: Option<Unstructured<'a>>, pub description: Option<Unstructured<'a>>,
pub header_ext: Vec<header::Kv<'a>>, pub header_ext: Vec<header::Kv<'a>>,
pub header_bad: Vec<&'a [u8]>, pub header_bad: Vec<&'a [u8]>,
pub raw: &'a [u8],
} }
impl<'a> FromIterator<Content<'a>> for NaiveMIME<'a> { impl<'a> FromIterator<Content<'a>> for NaiveMIME<'a> {
@ -89,6 +90,9 @@ impl<'a> NaiveMIME<'a> {
pub fn with_bad(mut self, bad: Vec<&'a [u8]>) -> Self { pub fn with_bad(mut self, bad: Vec<&'a [u8]>) -> Self {
self.header_bad = bad; self self.header_bad = bad; self
} }
pub fn with_raw(mut self, raw: &'a [u8]) -> Self {
self.raw = raw; self
}
pub fn to_interpreted<T: WithDefaultType>(self) -> AnyMIME<'a> { pub fn to_interpreted<T: WithDefaultType>(self) -> AnyMIME<'a> {
self.ctype.as_ref().map(|c| c.to_type()).unwrap_or(T::default_type()).to_mime(self).into() self.ctype.as_ref().map(|c| c.to_type()).unwrap_or(T::default_type()).to_mime(self).into()
} }

View file

@ -74,7 +74,16 @@ pub fn multipart<'a>(
// parse mime headers, otherwise pick default mime // parse mime headers, otherwise pick default mime
let (input, naive_mime) = match header(mime::field::content)(input) { let (input, naive_mime) = match header(mime::field::content)(input) {
Ok((input, (known, unknown, bad))) => (input, known.into_iter().collect::<mime::NaiveMIME>().with_opt(unknown).with_bad(bad)), Ok((input_eom, (known, unknown, bad))) => {
let raw_hdrs = pointers::parsed(input, input_eom);
let mime = known
.into_iter()
.collect::<mime::NaiveMIME>()
.with_opt(unknown)
.with_bad(bad)
.with_raw(raw_hdrs);
(input_eom, mime)
},
Err(_) => (input, mime::NaiveMIME::default()), Err(_) => (input, mime::NaiveMIME::default()),
}; };
@ -132,7 +141,7 @@ pub fn message<'a>(
let imf = imf.with_opt(unknown).with_bad(bad); let imf = imf.with_opt(unknown).with_bad(bad);
// interpret headers to choose a mime type // interpret headers to choose a mime type
let in_mime = naive_mime.to_interpreted::<mime::WithGenericDefault>().into(); let in_mime = naive_mime.with_raw(raw_headers).to_interpreted::<mime::WithGenericDefault>().into();
// parse this mimetype // parse this mimetype
let (input, part) = part::anypart(in_mime)(input)?; let (input, part) = part::anypart(in_mime)(input)?;