mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 21:06:31 +03:00
refactor: remove indexing/slicing from squash_attachment_parts
This commit is contained in:
@@ -665,11 +665,13 @@ impl MimeMessage {
|
|||||||
/// Delta Chat sends attachments, such as images, in two-part messages, with the first message
|
/// 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
|
/// 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.
|
/// moved to the second part, and the first part dropped.
|
||||||
#[allow(clippy::indexing_slicing)]
|
|
||||||
fn squash_attachment_parts(&mut self) {
|
fn squash_attachment_parts(&mut self) {
|
||||||
if let [textpart, filepart] = &self.parts[..] {
|
if self.parts.len() == 2
|
||||||
let need_drop = textpart.typ == Viewtype::Text
|
&& self.parts.first().map(|textpart| textpart.typ) == Some(Viewtype::Text)
|
||||||
&& match filepart.typ {
|
&& self
|
||||||
|
.parts
|
||||||
|
.get(1)
|
||||||
|
.map_or(false, |filepart| match filepart.typ {
|
||||||
Viewtype::Image
|
Viewtype::Image
|
||||||
| Viewtype::Gif
|
| Viewtype::Gif
|
||||||
| Viewtype::Sticker
|
| Viewtype::Sticker
|
||||||
@@ -680,24 +682,24 @@ impl MimeMessage {
|
|||||||
| Viewtype::File
|
| Viewtype::File
|
||||||
| Viewtype::Webxdc => true,
|
| Viewtype::Webxdc => true,
|
||||||
Viewtype::Unknown | Viewtype::Text | Viewtype::VideochatInvitation => false,
|
Viewtype::Unknown | Viewtype::Text | Viewtype::VideochatInvitation => false,
|
||||||
};
|
})
|
||||||
|
{
|
||||||
|
let mut parts = std::mem::take(&mut self.parts);
|
||||||
|
let Some(mut filepart) = parts.pop() else {
|
||||||
|
// Should never happen.
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some(textpart) = parts.pop() else {
|
||||||
|
// Should never happen.
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
if need_drop {
|
filepart.msg.clone_from(&textpart.msg);
|
||||||
let mut filepart = self.parts.swap_remove(1);
|
if let Some(quote) = textpart.param.get(Param::Quote) {
|
||||||
|
filepart.param.set(Param::Quote, quote);
|
||||||
// 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();
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.parts = vec![filepart];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user