From 9dedfb51ad36ce17e879d4b917fa23c6743fdec2 Mon Sep 17 00:00:00 2001 From: jikstra Date: Tue, 14 Jan 2020 19:23:23 +0100 Subject: [PATCH] Refactor code to not have mut Lot if possible, implement into_lot method for MessageSummary --- src/chatlist.rs | 34 +++++++++++++++------------------- src/message.rs | 36 ++++++++++++------------------------ 2 files changed, 27 insertions(+), 43 deletions(-) diff --git a/src/chatlist.rs b/src/chatlist.rs index 0599555f4..0bb43dcab 100644 --- a/src/chatlist.rs +++ b/src/chatlist.rs @@ -50,7 +50,7 @@ pub struct Chatlist { /// - dc_lot_t::timestamp: the timestamp of the message. 0 if not applicable. /// - dc_lot_t::state: The state of the message as one of the DC_STATE_* constants (see #dc_msg_get_state()). -#[derive(Debug)] +#[derive(Debug, Default)] pub struct ChatlistSummary { username: Option, chat_type: u32, @@ -303,10 +303,9 @@ impl Chatlist { // "is typing". // Also, sth. as "No messages" would not work if the summary comes from a message. - let mut ret = Lot::new(); if index >= self.ids.len() { - ret.text2 = Some("ErrBadChatlistIndex".to_string()); - return ret; + return Lot { text2: Some("ErrBadChatlistIndex".to_string()), ..Default::default() }; + } let chat_loaded: Chat; @@ -316,18 +315,19 @@ impl Chatlist { chat_loaded = chat; &chat_loaded } else { - return ret; + return Lot::default(); }; let lastmsg_id = self.ids[index].1; - self._get_summary(context, ret, chat, lastmsg_id) + self._get_summary(context, chat, lastmsg_id) } - pub fn get_summary_from_chat_id_json(&self, context: &Context, chat_id: u32) -> + pub fn get_summary_json(&self, context: &Context, chat_id: u32) -> ChatlistSummary { + ChatlistSummary { ..Default::default() } + } - pub fn _get_summary(&self, context: &Context, mut ret: Lot, chat: &Chat, lastmsg_id: MsgId) -> Lot { + pub fn _get_summary(&self, context: &Context, chat: &Chat, lastmsg_id: MsgId) -> Lot { let mut lastcontact = None; - let lastmsg = if let Ok(lastmsg) = Message::load_from_db(context, lastmsg_id) { if lastmsg.from_id != DC_CONTACT_ID_SELF && (chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup) @@ -341,20 +341,16 @@ impl Chatlist { }; if chat.id.is_archived_link() { - ret.text2 = None; + Lot { text2: None, .. Default::default() } } else if lastmsg.is_none() || lastmsg.as_ref().unwrap().from_id == DC_CONTACT_ID_UNDEFINED { - ret.text2 = Some(context.stock_str(StockMessage::NoMessages).to_string()); + Lot { + text2: Some(context.stock_str(StockMessage::NoMessages).to_string()), + .. Default::default() + } } else { - let message_summary = MessageSummary::new(&mut lastmsg.unwrap(), chat, lastcontact.as_ref(), context); - ret.text1 = message_summary.text1; - ret.text1_meaning = message_summary.text1_meaning; - ret.text2 = message_summary.summarytext; - ret.timestamp = message_summary.timestamp; - ret.state = message_summary.state.into(); + MessageSummary::new(&mut lastmsg.unwrap(), chat, lastcontact.as_ref(), context).into_lot() } - - ret } } diff --git a/src/message.rs b/src/message.rs index 6490ca82b..d0851f430 100644 --- a/src/message.rs +++ b/src/message.rs @@ -463,8 +463,6 @@ impl Message { } pub fn get_summary(&mut self, context: &Context, chat: Option<&Chat>) -> Lot { - let mut ret = Lot::new(); - let chat_loaded: Chat; let chat = if let Some(chat) = chat { chat @@ -472,7 +470,7 @@ impl Message { chat_loaded = chat; &chat_loaded } else { - return ret; + return Lot::default(); }; let contact = if self.from_id != DC_CONTACT_ID_SELF as u32 @@ -482,10 +480,8 @@ impl Message { } else { None }; - - ret.fill(self, chat, contact.as_ref(), context); - - ret + + MessageSummary::new(self, chat, contact.as_ref(), context).into_lot() } pub fn get_summarytext(&self, context: &Context, approx_characters: usize) -> String { @@ -776,24 +772,16 @@ impl MessageSummary { message_summary } -} -impl Lot { - /* library-internal */ - /* in practice, the user additionally cuts the string himself pixel-accurate */ - pub fn fill( - &mut self, - msg: &mut Message, - chat: &Chat, - contact: Option<&Contact>, - context: &Context, - ) { - let message_summary = MessageSummary::new(msg, chat, contact, context); - self.text1 = message_summary.text1; - self.text1_meaning = message_summary.text1_meaning; - self.text2 = message_summary.summarytext; - self.timestamp = message_summary.timestamp; - self.state = message_summary.state.into(); + pub fn into_lot(&mut self) -> Lot { + Lot { + text1: self.text1.clone(), + text1_meaning: self.text1_meaning, + text2: self.summarytext.clone(), + timestamp: self.timestamp, + state: self.state.into(), + .. Default::default() + } } }