diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 01ccb6a48..577de039f 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -1555,10 +1555,9 @@ pub unsafe extern "C" fn dc_create_contact( let name = to_string_lossy(name); block_on(async move { - match Contact::create(&ctx, name, to_string_lossy(addr)).await { - Ok(id) => id, - Err(_) => 0, - } + Contact::create(&ctx, name, to_string_lossy(addr)) + .await + .unwrap_or(0) }) } diff --git a/src/chat.rs b/src/chat.rs index 8d8f8221f..04ebf641f 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -256,9 +256,11 @@ impl ChatId { }; if promote { - let mut msg = Message::default(); - msg.viewtype = Viewtype::Text; - msg.text = Some(msg_text); + let mut msg = Message { + viewtype: Viewtype::Text, + text: Some(msg_text), + ..Default::default() + }; msg.param.set_cmd(cmd); send_msg(context, self, &mut msg).await?; } else { diff --git a/src/imex.rs b/src/imex.rs index 07b0a33f3..38848a77c 100644 --- a/src/imex.rs +++ b/src/imex.rs @@ -428,7 +428,7 @@ async fn decrypt_setup_file( pub fn normalize_setup_code(s: &str) -> String { let mut out = String::new(); for c in s.chars() { - if c >= '0' && c <= '9' { + if ('0'..='9').contains(&c) { out.push(c); if let 4 | 9 | 14 | 19 | 24 | 29 | 34 | 39 = out.len() { out += "-" diff --git a/src/key.rs b/src/key.rs index 163080d6c..30aa8ce9d 100644 --- a/src/key.rs +++ b/src/key.rs @@ -407,7 +407,7 @@ impl std::str::FromStr for Fingerprint { let hex_repr: String = input .to_uppercase() .chars() - .filter(|&c| c >= '0' && c <= '9' || c >= 'A' && c <= 'F') + .filter(|&c| ('0'..='9').contains(&c) || ('A'..='F').contains(&c)) .collect(); let v: Vec = hex::decode(hex_repr)?; let fp = Fingerprint::new(v)?; diff --git a/src/message.rs b/src/message.rs index aff80bd52..2b5da1270 100644 --- a/src/message.rs +++ b/src/message.rs @@ -310,16 +310,16 @@ pub struct Message { pub(crate) mime_modified: bool, pub(crate) chat_blocked: Blocked, pub(crate) location_id: u32, - error: Option, + pub(crate) error: Option, pub(crate) param: Params, } impl Message { pub fn new(viewtype: Viewtype) -> Self { - let mut msg = Message::default(); - msg.viewtype = viewtype; - - msg + Message { + viewtype, + ..Default::default() + } } pub async fn load_from_db(context: &Context, id: MsgId) -> Result { @@ -360,55 +360,53 @@ impl Message { ), paramsv![id], |row| { - let mut msg = Message::default(); - // msg.id = row.get::<_, AnyMsgId>("id")?; - msg.id = row.get("id")?; - msg.rfc724_mid = row.get::<_, String>("rfc724mid")?; - msg.in_reply_to = row.get::<_, Option>("mime_in_reply_to")?; - msg.server_folder = row.get::<_, Option>("server_folder")?; - msg.server_uid = row.get("server_uid")?; - msg.chat_id = row.get("chat_id")?; - msg.from_id = row.get("from_id")?; - msg.to_id = row.get("to_id")?; - msg.timestamp_sort = row.get("timestamp")?; - msg.timestamp_sent = row.get("timestamp_sent")?; - msg.timestamp_rcvd = row.get("timestamp_rcvd")?; - msg.ephemeral_timer = row.get("ephemeral_timer")?; - msg.ephemeral_timestamp = row.get("ephemeral_timestamp")?; - msg.viewtype = row.get("type")?; - msg.state = row.get("state")?; - let error: String = row.get("error")?; - msg.error = Some(error).filter(|error| !error.is_empty()); - msg.is_dc_message = row.get("msgrmsg")?; - msg.mime_modified = row.get("mime_modified")?; - - let text; - if let rusqlite::types::ValueRef::Text(buf) = row.get_raw("txt") { - if let Ok(t) = String::from_utf8(buf.to_vec()) { - text = t; - } else { - warn!( - context, - concat!( - "dc_msg_load_from_db: could not get ", - "text column as non-lossy utf8 id {}" - ), - id - ); - text = String::from_utf8_lossy(buf).into_owned(); + let text = match row.get_raw("txt") { + rusqlite::types::ValueRef::Text(buf) => { + match String::from_utf8(buf.to_vec()) { + Ok(t) => t, + Err(_) => { + warn!( + context, + concat!( + "dc_msg_load_from_db: could not get ", + "text column as non-lossy utf8 id {}" + ), + id + ); + String::from_utf8_lossy(buf).into_owned() + } + } } - } else { - text = "".to_string(); - } - msg.text = Some(text); - - msg.param = row.get::<_, String>("param")?.parse().unwrap_or_default(); - msg.hidden = row.get("hidden")?; - msg.location_id = row.get("location")?; - msg.chat_blocked = row - .get::<_, Option>("blocked")? - .unwrap_or_default(); - + _ => String::new(), + }; + let msg = Message { + id: row.get("id")?, + rfc724_mid: row.get::<_, String>("rfc724mid")?, + in_reply_to: row.get::<_, Option>("mime_in_reply_to")?, + server_folder: row.get::<_, Option>("server_folder")?, + server_uid: row.get("server_uid")?, + chat_id: row.get("chat_id")?, + from_id: row.get("from_id")?, + to_id: row.get("to_id")?, + timestamp_sort: row.get("timestamp")?, + timestamp_sent: row.get("timestamp_sent")?, + timestamp_rcvd: row.get("timestamp_rcvd")?, + ephemeral_timer: row.get("ephemeral_timer")?, + ephemeral_timestamp: row.get("ephemeral_timestamp")?, + viewtype: row.get("type")?, + state: row.get("state")?, + error: Some(row.get::<_, String>("error")?) + .filter(|error| !error.is_empty()), + is_dc_message: row.get("msgrmsg")?, + mime_modified: row.get("mime_modified")?, + text: Some(text), + param: row.get::<_, String>("param")?.parse().unwrap_or_default(), + hidden: row.get("hidden")?, + location_id: row.get("location")?, + chat_blocked: row + .get::<_, Option>("blocked")? + .unwrap_or_default(), + }; Ok(msg) }, ) @@ -1205,7 +1203,7 @@ pub async fn get_msg_info(context: &Context, msg_id: MsgId) -> String { ret += &format!("\nMessage-ID: {}", msg.rfc724_mid); } if let Some(ref server_folder) = msg.server_folder { - if server_folder != "" { + if !server_folder.is_empty() { ret += &format!("\nLast seen as: {}/{}", server_folder, msg.server_uid); } } diff --git a/src/mimefactory.rs b/src/mimefactory.rs index 8ec10fabf..ed1efc98e 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -864,8 +864,10 @@ impl<'a, 'b> MimeFactory<'a, 'b> { if let Some(grpimage) = grpimage { info!(self.context, "setting group image '{}'", grpimage); - let mut meta = Message::default(); - meta.viewtype = Viewtype::Image; + let mut meta = Message { + viewtype: Viewtype::Image, + ..Default::default() + }; meta.param.set(Param::File, grpimage); let (mail, filename_as_sent) = build_body_file(context, &meta, "group-image").await?; diff --git a/src/mimeparser.rs b/src/mimeparser.rs index d44c87aad..2443252e3 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -438,8 +438,10 @@ impl MimeMessage { // Besides, we want to show something in case our incoming-processing // failed to properly handle an incoming message. if self.parts.is_empty() && self.mdn_reports.is_empty() { - let mut part = Part::default(); - part.typ = Viewtype::Text; + let mut part = Part { + typ: Viewtype::Text, + ..Default::default() + }; if let Some(ref subject) = self.get_subject() { if !self.has_chat_version() { @@ -617,12 +619,13 @@ impl MimeMessage { let msg_body = context.stock_str(StockMessage::CantDecryptMsgBody).await; let txt = format!("[{}]", msg_body); - let mut part = Part::default(); - part.typ = Viewtype::Text; - part.msg_raw = Some(txt.clone()); - part.msg = txt; - part.error = Some("Decryption failed".to_string()); - + let part = Part { + typ: Viewtype::Text, + msg_raw: Some(txt.clone()), + msg: txt, + error: Some("Decryption failed".to_string()), + ..Default::default() + }; self.parts.push(part); any_part_added = true; @@ -654,8 +657,10 @@ impl MimeMessage { // downloading the message again and // delete if automatic message deletion is // enabled. - let mut part = Part::default(); - part.typ = Viewtype::Unknown; + let part = Part { + typ: Viewtype::Unknown, + ..Default::default() + }; self.parts.push(part); any_part_added = true; @@ -783,11 +788,13 @@ impl MimeMessage { }; if !simplified_txt.is_empty() || simplified_quote.is_some() { - let mut part = Part::default(); - part.dehtml_failed = dehtml_failed; - part.typ = Viewtype::Text; - part.mimetype = Some(mime_type); - part.msg = simplified_txt; + let mut part = Part { + dehtml_failed, + typ: Viewtype::Text, + mimetype: Some(mime_type), + msg: simplified_txt, + ..Default::default() + }; if let Some(quote) = simplified_quote { part.param.set(Param::Quote, quote); } diff --git a/src/securejoin.rs b/src/securejoin.rs index 41cbc3f95..e52749244 100644 --- a/src/securejoin.rs +++ b/src/securejoin.rs @@ -385,10 +385,12 @@ async fn send_handshake_msg( fingerprint: Option, grpid: impl AsRef, ) -> Result<(), SendMsgError> { - let mut msg = Message::default(); - msg.viewtype = Viewtype::Text; - msg.text = Some(format!("Secure-Join: {}", step)); - msg.hidden = true; + let mut msg = Message { + viewtype: Viewtype::Text, + text: Some(format!("Secure-Join: {}", step)), + hidden: true, + ..Default::default() + }; msg.param.set_cmd(SystemMessage::SecurejoinMessage); if step.is_empty() { msg.param.remove(Param::Arg); diff --git a/src/simplify.rs b/src/simplify.rs index bf6f75cba..9d440ced6 100644 --- a/src/simplify.rs +++ b/src/simplify.rs @@ -31,7 +31,9 @@ fn remove_message_footer<'a>(lines: &'a [&str]) -> (&'a [&'a str], bool) { // use that only when no other footer is found // and if the line before is empty and the line after is not empty "--" => { - if (ix == 0 || lines[ix - 1] == "") && ix != lines.len() - 1 && lines[ix + 1] != "" + if (ix == 0 || lines[ix - 1].is_empty()) + && ix != lines.len() - 1 + && !lines[ix + 1].is_empty() { nearly_standard_footer = Some(ix); } diff --git a/src/test_utils.rs b/src/test_utils.rs index adcf9a74e..fc1e813e5 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -504,7 +504,7 @@ async fn log_msg(context: &Context, prefix: impl AsRef, msg: &Message) { &contact_name, contact_id, msgtext.unwrap_or_default(), - if msg.get_from_id() == 1 as libc::c_uint { + if msg.get_from_id() == 1u32 { "" } else if msg.get_state() == MessageState::InSeen { "[SEEN]"