Skip to content

Commit

Permalink
refactor: remove indexing/slicing from squash_attachment_parts
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Nov 18, 2024
1 parent ce02f2a commit 5765b0c
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/mimeparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,13 @@ impl MimeMessage {
/// Delta Chat sends attachments, such as images, in two-part messages, with the first message
/// containing a description. If such a message is detected, text from the first part can be
/// moved to the second part, and the first part dropped.
#[allow(clippy::indexing_slicing)]
fn squash_attachment_parts(&mut self) {
if let [textpart, filepart] = &self.parts[..] {
let need_drop = textpart.typ == Viewtype::Text
&& match filepart.typ {
if self.parts.len() == 2
&& self.parts.first().map(|textpart| textpart.typ) == Some(Viewtype::Text)
&& self
.parts
.get(1)
.map_or(false, |filepart| match filepart.typ {
Viewtype::Image
| Viewtype::Gif
| Viewtype::Sticker
Expand All @@ -680,24 +682,24 @@ impl MimeMessage {
| Viewtype::File
| Viewtype::Webxdc => true,
Viewtype::Unknown | Viewtype::Text | Viewtype::VideochatInvitation => false,
};

if need_drop {
let mut filepart = self.parts.swap_remove(1);

// insert new one
filepart.msg.clone_from(&self.parts[0].msg);
if let Some(quote) = self.parts[0].param.get(Param::Quote) {
filepart.param.set(Param::Quote, quote);
}

// forget the one we use now
self.parts[0].msg = "".to_string();
})
{
let mut parts = std::mem::take(&mut self.parts);
let Some(textpart) = parts.pop() else {
// Should never happen.
return;
};
let Some(mut filepart) = parts.pop() else {
// Should never happen.
return;
};

// swap new with old
self.parts.push(filepart); // push to the end
let _ = self.parts.swap_remove(0); // drops first element, replacing it with the last one in O(1)
filepart.msg.clone_from(&textpart.msg);
if let Some(quote) = textpart.param.get(Param::Quote) {
filepart.param.set(Param::Quote, quote);
}

self.parts = vec![filepart];
}
}

Expand Down

0 comments on commit 5765b0c

Please sign in to comment.