diff --git a/src/header.rs b/src/header.rs index 3f28840..61ec911 100644 --- a/src/header.rs +++ b/src/header.rs @@ -62,9 +62,9 @@ pub trait Header<'a>: Clone + Display { fn get_key_raw(&self) -> &[u8]; fn get_value_raw(&self) -> &[u8]; fn set_value_raw(&mut self, value: Vec); - fn serialize(&self) -> Vec>; + fn serialize(&self) -> Vec>; - fn get_key(&self) -> Cow { + fn get_key(&self) -> Cow<'_, str> { decode_latin1(self.get_key_raw()) } @@ -72,7 +72,7 @@ pub trait Header<'a>: Clone + Display { self.get_key().eq_ignore_ascii_case(key) } - fn get_value_ref(&self) -> Cow { + fn get_value_ref(&self) -> Cow<'_, str> { // RFC 6532 says that header values can be UTF-8. Let's try that first, and // fall back to latin1 if that fails let value = self.get_value_raw(); @@ -241,7 +241,7 @@ impl<'a> Header<'a> for CowHeader<'a> { raw_bytes.extend(b"\r\n"); } - fn serialize(&self) -> Vec> { + fn serialize(&self) -> Vec> { vec![Cow::Borrowed(&self.raw_bytes)] } } diff --git a/src/lib.rs b/src/lib.rs index 2383d9f..da38ebd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -346,19 +346,12 @@ impl<'a, H: Header<'a> + 'a> CowMail<'a, H> { } } - fn make_multipart(&mut self, to_type: Name, boundary: Option) -> Result<(), CowMailError> { + fn make_multipart(&mut self, to_type: Name, boundary: Option) { let ctype = self.get_mime_type(); if ctype.ty() == MULTIPART { let subtype = ctype.subty(); - if subtype == to_type - || (to_type == ALTERNATIVE && subtype == MIXED) - || (to_type == RELATED && [ALTERNATIVE, MIXED].contains(&subtype)) - { - return Err(CowMailError::Generic(format!( - "cannot convert {} to multipart/{}", - ctype.essence(), - to_type - ))); + if subtype == to_type { + return; } } @@ -396,52 +389,50 @@ impl<'a, H: Header<'a> + 'a> CowMail<'a, H> { ); self.subparts.push(subpart); - Ok(()) } - pub fn make_mixed(&mut self, boundary: Option) -> Result<(), CowMailError> { + pub fn make_mixed(&mut self, boundary: Option) { self.make_multipart(MIXED, boundary) } - pub fn make_alternative(&mut self, boundary: Option) -> Result<(), CowMailError> { + pub fn make_alternative(&mut self, boundary: Option) { self.make_multipart(ALTERNATIVE, boundary) } - pub fn make_related(&mut self, boundary: Option) -> Result<(), CowMailError> { + pub fn make_related(&mut self, boundary: Option) { self.make_multipart(RELATED, boundary) } - fn add_multipart(&mut self, subpart: CowMail<'a, H>, ctype: MediaType) -> Result<(), CowMailError> { + fn add_multipart(&mut self, subpart: CowMail<'a, H>, ctype: MediaType) { match self.is_multipart() { false => { - self.make_multipart(ctype.subty, None)?; + self.make_multipart(ctype.subty, None); self.subparts.push(subpart); } true => { let existing_ctype = self.get_mime_type(); if existing_ctype.ty() == MULTIPART && existing_ctype != ctype { - self.make_multipart(ctype.subty, None)?; + self.make_multipart(ctype.subty, None); self.subparts.push(subpart); } else { self.subparts.push(subpart); } } } - Ok(()) } - pub fn add_related(&mut self, mut subpart: CowMail<'a, H>) -> Result<(), CowMailError> { + pub fn add_related(&mut self, mut subpart: CowMail<'a, H>) { if subpart.headers.find_header("Content-Disposition").is_none() { subpart.headers.add_header("Content-Disposition", "inline"); } self.add_multipart(subpart, MediaType::new(MULTIPART, RELATED)) } - pub fn add_alternative(&mut self, subpart: CowMail<'a, H>) -> Result<(), CowMailError> { + pub fn add_alternative(&mut self, subpart: CowMail<'a, H>) { self.add_multipart(subpart, MediaType::new(MULTIPART, ALTERNATIVE)) } - pub fn add_attachment(&mut self, mut subpart: CowMail<'a, H>) -> Result<(), CowMailError> { + pub fn add_attachment(&mut self, mut subpart: CowMail<'a, H>) { if subpart.headers.find_header("Content-Disposition").is_none() { subpart.headers.add_header("Content-Disposition", "attachment"); } @@ -513,7 +504,7 @@ impl<'a, H: Header<'a> + 'a> CowMail<'a, H> { let html_part = parent .get_body_part_mut(BodyType::HTML) .ok_or(CowMailError::Generic("get_body_parent_part_mut returned a parent without a plain or html body, this should never happen!".into()))?; - html_part.make_alternative(None)?; + html_part.make_alternative(None); html_part.subparts.insert(0, text_part); } } @@ -523,7 +514,7 @@ impl<'a, H: Header<'a> + 'a> CowMail<'a, H> { } if !self.is_multipart() { - self.make_mixed(None)?; + self.make_mixed(None); } let mut text_part = Self::new(); @@ -546,7 +537,7 @@ impl<'a, H: Header<'a> + 'a> CowMail<'a, H> { let text_part = parent .get_body_part_mut(BodyType::PLAIN) .ok_or(CowMailError::Generic("get_body_parent_part_mut returned a parent without a plain or html body, this should never happen!".into()))?; - text_part.make_alternative(None)?; + text_part.make_alternative(None); text_part.subparts.push(html_part); } } @@ -556,7 +547,7 @@ impl<'a, H: Header<'a> + 'a> CowMail<'a, H> { } if !self.is_multipart() { - self.make_mixed(None)?; + self.make_mixed(None); } let mut html_part = Self::new(); @@ -571,13 +562,13 @@ impl<'a, H: Header<'a> + 'a> CowMail<'a, H> { self.set_html_body(html) } - pub fn serialize_headers(&self) -> Vec> { + pub fn serialize_headers(&self) -> Vec> { let mut res: Vec> = self.headers.iter().flat_map(|h| h.serialize()).collect(); res.push(Cow::Borrowed(b"\r\n")); res } - pub fn serialize_body(&self) -> Vec> { + pub fn serialize_body(&self) -> Vec> { match self.message_type() { MessageType::SinglePart => match self.body_bytes { Cow::Borrowed(body_bytes) => vec![Cow::Borrowed(body_bytes)], @@ -604,7 +595,7 @@ impl<'a, H: Header<'a> + 'a> CowMail<'a, H> { } } - pub fn serialize(&self) -> Vec> { + pub fn serialize(&self) -> Vec> { let mut res = self.serialize_headers(); res.append(&mut self.serialize_body()); res @@ -1055,7 +1046,7 @@ mod tests { Some(MediaType::new(TEXT, HTML)), ); - mail.add_alternative(html).unwrap(); + mail.add_alternative(html); let alt_boundary = mail.get_boundary().unwrap(); let ctype = mail.content_type(); @@ -1071,7 +1062,7 @@ mod tests { let mut disp = CowHeader::new("Content-Disposition", "attachment"); disp.set_param("filename", "Test☺File.html"); attachment.headers.push(disp); - mail.add_attachment(attachment).unwrap(); + mail.add_attachment(attachment); let mixed_boundary = mail.get_boundary().unwrap(); let encoded = mail