From 6be96d3eba068afc4d963c635767b7676c54a317 Mon Sep 17 00:00:00 2001 From: link2xt Date: Wed, 27 Nov 2024 18:56:39 +0000 Subject: [PATCH] refactor: remove some .unwrap() calls --- src/configure/auto_mozilla.rs | 18 +++++++----------- src/context.rs | 4 ++-- src/location.rs | 16 ++++++---------- src/log.rs | 4 ++-- src/mimeparser.rs | 7 ++++--- 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/configure/auto_mozilla.rs b/src/configure/auto_mozilla.rs index 1e007e9018..c679d62155 100644 --- a/src/configure/auto_mozilla.rs +++ b/src/configure/auto_mozilla.rs @@ -67,19 +67,15 @@ fn parse_server( let typ = server_event .attributes() - .find(|attr| { - attr.as_ref() - .map(|a| { - String::from_utf8_lossy(a.key.as_ref()) - .trim() - .to_lowercase() - == "type" - }) - .unwrap_or_default() + .find_map(|attr| { + attr.ok().filter(|a| { + String::from_utf8_lossy(a.key.as_ref()) + .trim() + .eq_ignore_ascii_case("type") + }) }) .map(|typ| { - typ.unwrap() - .decode_and_unescape_value(reader.decoder()) + typ.decode_and_unescape_value(reader.decoder()) .unwrap_or_default() .to_lowercase() }) diff --git a/src/context.rs b/src/context.rs index 93f43c21a4..7cc1e929ea 100644 --- a/src/context.rs +++ b/src/context.rs @@ -276,7 +276,7 @@ pub struct InnerContext { /// The text of the last error logged and emitted as an event. /// If the ui wants to display an error after a failure, /// `last_error` should be used to avoid races with the event thread. - pub(crate) last_error: std::sync::RwLock, + pub(crate) last_error: parking_lot::RwLock, /// If debug logging is enabled, this contains all necessary information /// @@ -446,7 +446,7 @@ impl Context { metadata: RwLock::new(None), creation_time: tools::Time::now(), last_full_folder_scan: Mutex::new(None), - last_error: std::sync::RwLock::new("".to_string()), + last_error: parking_lot::RwLock::new("".to_string()), debug_logging: std::sync::RwLock::new(None), push_subscriber, push_subscribed: AtomicBool::new(false), diff --git a/src/location.rs b/src/location.rs index 5dcc9a258b..81a23a5886 100644 --- a/src/location.rs +++ b/src/location.rs @@ -244,18 +244,14 @@ impl Kml { self.tag = KmlTag::PlacemarkPoint; } else if tag == "coordinates" && self.tag == KmlTag::PlacemarkPoint { self.tag = KmlTag::PlacemarkPointCoordinates; - if let Some(acc) = event.attributes().find(|attr| { - attr.as_ref() - .map(|a| { - String::from_utf8_lossy(a.key.as_ref()) - .trim() - .to_lowercase() - == "accuracy" - }) - .unwrap_or_default() + if let Some(acc) = event.attributes().find_map(|attr| { + attr.ok().filter(|a| { + String::from_utf8_lossy(a.key.as_ref()) + .trim() + .eq_ignore_ascii_case("accuracy") + }) }) { let v = acc - .unwrap() .decode_and_unescape_value(reader.decoder()) .unwrap_or_default(); diff --git a/src/log.rs b/src/log.rs index 78e41039f6..b665cf242d 100644 --- a/src/log.rs +++ b/src/log.rs @@ -50,13 +50,13 @@ impl Context { /// Set last error string. /// Implemented as blocking as used from macros in different, not always async blocks. pub fn set_last_error(&self, error: &str) { - let mut last_error = self.last_error.write().unwrap(); + let mut last_error = self.last_error.write(); *last_error = error.to_string(); } /// Get last error string. pub fn get_last_error(&self) -> String { - let last_error = &*self.last_error.read().unwrap(); + let last_error = &*self.last_error.read(); last_error.clone() } } diff --git a/src/mimeparser.rs b/src/mimeparser.rs index e2c7583aa4..7130cb512b 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -1667,10 +1667,11 @@ impl MimeMessage { { let mut to_list = get_all_addresses_from_header(&report.headers, "x-failed-recipients"); - let to = if to_list.len() == 1 { - Some(to_list.pop().unwrap()) + let to = if to_list.len() != 1 { + // We do not know which recipient failed + None } else { - None // We do not know which recipient failed + to_list.pop() }; return Ok(Some(DeliveryReport {