diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-12-15 19:47:42 +0200 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-12-15 19:47:42 +0200 |
commit | 8f6357258450d84b9d572866ccb6c259ebdeae08 (patch) | |
tree | 866700f1c79562b5821050674c13184d7c7d28fb /ui | |
parent | 020124178683d00d328cb50079cc921a9622317b (diff) |
Small refactors to avoid implicit unwrap() panics
Diffstat (limited to 'ui')
-rw-r--r-- | ui/src/components/mail/listing.rs | 34 | ||||
-rw-r--r-- | ui/src/components/mail/listing/compact.rs | 30 | ||||
-rw-r--r-- | ui/src/components/mail/listing/conversations.rs | 30 | ||||
-rw-r--r-- | ui/src/components/mail/listing/plain.rs | 18 | ||||
-rw-r--r-- | ui/src/components/mail/listing/thread.rs | 28 | ||||
-rw-r--r-- | ui/src/components/mail/view/thread.rs | 12 | ||||
-rw-r--r-- | ui/src/types/helpers.rs | 6 |
7 files changed, 81 insertions, 77 deletions
diff --git a/ui/src/components/mail/listing.rs b/ui/src/components/mail/listing.rs index c78a0731..d2f7dd16 100644 --- a/ui/src/components/mail/listing.rs +++ b/ui/src/components/mail/listing.rs @@ -257,7 +257,7 @@ impl fmt::Display for Listing { impl Component for Listing { fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) { for i in 0..context.accounts.len() { - context.is_online(i); + let _ = context.is_online(i); } if !self.is_dirty() { @@ -405,21 +405,25 @@ impl Component for Listing { } _ => return false, } - let folder_hash = - context.accounts[self.cursor_pos.0].folders_order[self.cursor_pos.1]; - /* Check if per-folder configuration overrides general configuration */ - if let Some(index_style) = context - .accounts - .get(self.cursor_pos.0) - .and_then(|account| account.folder_confs(folder_hash).conf_override.index_style) - { - self.component.set_style(index_style); - } else if let Some(index_style) = context - .accounts - .get(self.cursor_pos.0) - .and_then(|account| Some(account.settings.conf.index_style())) + /* Account might have no folders yet if it's offline */ + if let Some(&folder_hash) = context.accounts[self.cursor_pos.0] + .folders_order + .get(self.cursor_pos.1) { - self.component.set_style(index_style); + /* Check if per-folder configuration overrides general configuration */ + if let Some(index_style) = + context.accounts.get(self.cursor_pos.0).and_then(|account| { + account.folder_confs(folder_hash).conf_override.index_style + }) + { + self.component.set_style(index_style); + } else if let Some(index_style) = context + .accounts + .get(self.cursor_pos.0) + .and_then(|account| Some(account.settings.conf.index_style())) + { + self.component.set_style(index_style); + } } context .replies diff --git a/ui/src/components/mail/listing/compact.rs b/ui/src/components/mail/listing/compact.rs index e4965fc8..0d4df95a 100644 --- a/ui/src/components/mail/listing/compact.rs +++ b/ui/src/components/mail/listing/compact.rs @@ -708,15 +708,13 @@ impl CompactListing { for (idx, root_idx) in threads_iter.enumerate() { self.length += 1; let thread_node = &threads.thread_nodes()[&root_idx]; - let i = if let Some(i) = thread_node.message() { - i - } else { + let i = thread_node.message().unwrap_or_else(|| { let mut iter_ptr = thread_node.children()[0]; while threads.thread_nodes()[&iter_ptr].message().is_none() { iter_ptr = threads.thread_nodes()[&iter_ptr].children()[0]; } threads.thread_nodes()[&iter_ptr].message().unwrap() - }; + }); if !context.accounts[self.cursor_pos.0].contains_key(i) { debug!("key = {}", i); debug!( @@ -808,15 +806,13 @@ impl CompactListing { for ((idx, root_idx), strings) in threads_iter.enumerate().zip(rows) { let thread_node = &threads.thread_nodes()[&root_idx]; - let i = if let Some(i) = thread_node.message() { - i - } else { + let i = thread_node.message().unwrap_or_else(|| { let mut iter_ptr = thread_node.children()[0]; while threads.thread_nodes()[&iter_ptr].message().is_none() { iter_ptr = threads.thread_nodes()[&iter_ptr].children()[0]; } threads.thread_nodes()[&iter_ptr].message().unwrap() - }; + }); if !context.accounts[self.cursor_pos.0].contains_key(i) { //debug!("key = {}", i); //debug!( @@ -1327,19 +1323,21 @@ impl Component for CompactListing { } match *event { UIEvent::MailboxUpdate((ref idxa, ref idxf)) - if (*idxa, *idxf) - == ( - self.new_cursor_pos.0, - context.accounts[self.new_cursor_pos.0].folders_order - [self.new_cursor_pos.1], - ) => + if context.accounts[self.new_cursor_pos.0] + .folders_order + .get(self.new_cursor_pos.1) + .map(|&folder_hash| (*idxa, *idxf) == (self.new_cursor_pos.0, folder_hash)) + .unwrap_or(false) => { self.refresh_mailbox(context); self.set_dirty(true); } UIEvent::StartupCheck(ref f) - if *f - == context.accounts[self.cursor_pos.0].folders_order[self.new_cursor_pos.1] => + if context.accounts[self.new_cursor_pos.0] + .folders_order + .get(self.new_cursor_pos.1) + .map(|&folder_hash| *f == folder_hash) + .unwrap_or(false) => { self.refresh_mailbox(context); self.set_dirty(true); diff --git a/ui/src/components/mail/listing/conversations.rs b/ui/src/components/mail/listing/conversations.rs index 92c18848..edc357eb 100644 --- a/ui/src/components/mail/listing/conversations.rs +++ b/ui/src/components/mail/listing/conversations.rs @@ -670,15 +670,13 @@ impl ConversationsListing { for (idx, root_idx) in threads_iter.enumerate() { self.length += 1; let thread_node = &threads.thread_nodes()[&root_idx]; - let i = if let Some(i) = thread_node.message() { - i - } else { + let i = thread_node.message().unwrap_or_else(|| { let mut iter_ptr = thread_node.children()[0]; while threads.thread_nodes()[&iter_ptr].message().is_none() { iter_ptr = threads.thread_nodes()[&iter_ptr].children()[0]; } threads.thread_nodes()[&iter_ptr].message().unwrap() - }; + }); if !context.accounts[self.cursor_pos.0].contains_key(i) { debug!("key = {}", i); debug!( @@ -771,15 +769,13 @@ impl ConversationsListing { for ((idx, root_idx), strings) in threads_iter.enumerate().zip(rows) { let thread_node = &threads.thread_nodes()[&root_idx]; - let i = if let Some(i) = thread_node.message() { - i - } else { + let i = thread_node.message().unwrap_or_else(|| { let mut iter_ptr = thread_node.children()[0]; while threads.thread_nodes()[&iter_ptr].message().is_none() { iter_ptr = threads.thread_nodes()[&iter_ptr].children()[0]; } threads.thread_nodes()[&iter_ptr].message().unwrap() - }; + }); if !context.accounts[self.cursor_pos.0].contains_key(i) { panic!(); } @@ -1345,19 +1341,21 @@ impl Component for ConversationsListing { } match *event { UIEvent::MailboxUpdate((ref idxa, ref idxf)) - if (*idxa, *idxf) - == ( - self.new_cursor_pos.0, - context.accounts[self.new_cursor_pos.0].folders_order - [self.new_cursor_pos.1], - ) => + if context.accounts[self.new_cursor_pos.0] + .folders_order + .get(self.new_cursor_pos.1) + .map(|&folder_hash| (*idxa, *idxf) == (self.new_cursor_pos.0, folder_hash)) + .unwrap_or(false) => { self.refresh_mailbox(context); self.set_dirty(true); } UIEvent::StartupCheck(ref f) - if *f - == context.accounts[self.cursor_pos.0].folders_order[self.new_cursor_pos.1] => + if context.accounts[self.new_cursor_pos.0] + .folders_order + .get(self.new_cursor_pos.1) + .map(|&folder_hash| *f == folder_hash) + .unwrap_or(false) => { self.refresh_mailbox(context); self.set_dirty(true); diff --git a/ui/src/components/mail/listing/plain.rs b/ui/src/components/mail/listing/plain.rs index e18b85f8..3f0f0f74 100644 --- a/ui/src/components/mail/listing/plain.rs +++ b/ui/src/components/mail/listing/plain.rs @@ -1104,19 +1104,21 @@ impl Component for PlainListing { } match *event { UIEvent::MailboxUpdate((ref idxa, ref idxf)) - if (*idxa, *idxf) - == ( - self.new_cursor_pos.0, - context.accounts[self.new_cursor_pos.0].folders_order - [self.new_cursor_pos.1], - ) => + if context.accounts[self.new_cursor_pos.0] + .folders_order + .get(self.new_cursor_pos.1) + .map(|&folder_hash| (*idxa, *idxf) == (self.new_cursor_pos.0, folder_hash)) + .unwrap_or(false) => { self.refresh_mailbox(context); self.set_dirty(true); } UIEvent::StartupCheck(ref f) - if *f - == context.accounts[self.cursor_pos.0].folders_order[self.new_cursor_pos.1] => + if context.accounts[self.new_cursor_pos.0] + .folders_order + .get(self.new_cursor_pos.1) + .map(|&folder_hash| *f == folder_hash) + .unwrap_or(false) => { self.refresh_mailbox(context); self.set_dirty(true); diff --git a/ui/src/components/mail/listing/thread.rs b/ui/src/components/mail/listing/thread.rs index 3754dd23..503d8a05 100644 --- a/ui/src/components/mail/listing/thread.rs +++ b/ui/src/components/mail/listing/thread.rs @@ -264,7 +264,14 @@ impl ThreadListing { } self.cursor_pos.1 = self.new_cursor_pos.1; self.cursor_pos.0 = self.new_cursor_pos.0; - let folder_hash = context.accounts[self.cursor_pos.0].folders_order[self.cursor_pos.1]; + let folder_hash = if let Some(h) = context.accounts[self.cursor_pos.0] + .folders_order + .get(self.cursor_pos.1) + { + *h + } else { + return; + }; // Inform State that we changed the current folder view. context @@ -620,20 +627,21 @@ impl Component for ThreadListing { self.view = None; } UIEvent::MailboxUpdate((ref idxa, ref idxf)) - if (*idxa, *idxf) - == ( - self.new_cursor_pos.0, - context.accounts[self.new_cursor_pos.0].folders_order - [self.new_cursor_pos.1], - ) => + if context.accounts[self.new_cursor_pos.0] + .folders_order + .get(self.new_cursor_pos.1) + .map(|&folder_hash| (*idxa, *idxf) == (self.new_cursor_pos.0, folder_hash)) + .unwrap_or(false) => { self.refresh_mailbox(context); self.set_dirty(true); } UIEvent::StartupCheck(ref f) - if *f - == context.accounts[self.new_cursor_pos.0].folders_order - [self.new_cursor_pos.1] => + if context.accounts[self.new_cursor_pos.0] + .folders_order + .get(self.new_cursor_pos.1) + .map(|&folder_hash| *f == folder_hash) + .unwrap_or(false) => { self.refresh_mailbox(context); self.set_dirty(true); diff --git a/ui/src/components/mail/view/thread.rs b/ui/src/components/mail/view/thread.rs index dc4f341a..9cdb3110 100644 --- a/ui/src/components/mail/view/thread.rs +++ b/ui/src/components/mail/view/thread.rs @@ -634,15 +634,13 @@ impl ThreadView { let mailbox = &account[self.coordinates.1].unwrap(); let threads = &account.collection.threads[&mailbox.folder.hash()]; let thread_node = &threads.thread_nodes()[&threads.root_set(self.coordinates.2)]; - let i = if let Some(i) = thread_node.message() { - i - } else { + let i = thread_node.message().unwrap_or_else(|| { let mut iter_ptr = thread_node.children()[0]; while threads.thread_nodes()[&iter_ptr].message().is_none() { iter_ptr = threads.thread_nodes()[&iter_ptr].children()[0]; } threads.thread_nodes()[&iter_ptr].message().unwrap() - }; + }); let envelope: EnvelopeRef = account.collection.get_env(i); let (x, y) = write_string_to_grid( @@ -726,15 +724,13 @@ impl ThreadView { let mailbox = &account[self.coordinates.1].unwrap(); let threads = &account.collection.threads[&mailbox.folder.hash()]; let thread_node = &threads.thread_nodes()[&threads.root_set(self.coordinates.2)]; - let i = if let Some(i) = thread_node.message() { - i - } else { + let i = thread_node.message().unwrap_or_else(|| { let mut iter_ptr = thread_node.children()[0]; while threads.thread_nodes()[&iter_ptr].message().is_none() { iter_ptr = threads.thread_nodes()[&iter_ptr].children()[0]; } threads.thread_nodes()[&iter_ptr].message().unwrap() - }; + }); let envelope: EnvelopeRef = account.collection.get_env(i); let (x, y) = write_string_to_grid( diff --git a/ui/src/types/helpers.rs b/ui/src/types/helpers.rs index 58b94540..0cb6f372 100644 --- a/ui/src/types/helpers.rs +++ b/ui/src/types/helpers.rs @@ -75,9 +75,7 @@ pub fn create_temp_file( ) -> File { let mut dir = std::env::temp_dir(); - let path = if let Some(p) = path { - p - } else { + let path = path.unwrap_or_else(|| { dir.push("meli"); std::fs::DirBuilder::new() .recursive(true) @@ -90,7 +88,7 @@ pub fn create_temp_file( dir.push(u.to_hyphenated().to_string()); } &dir - }; + }); let mut f = std::fs::File::create(path).unwrap(); let metadata = f.metadata().unwrap(); |