refactor: remove some .unwrap() calls

This commit is contained in:
link2xt
2024-11-27 18:56:39 +00:00
committed by l
parent d1537095e4
commit 6be96d3eba
5 changed files with 21 additions and 28 deletions

View File

@@ -67,19 +67,15 @@ fn parse_server<B: BufRead>(
let typ = server_event let typ = server_event
.attributes() .attributes()
.find(|attr| { .find_map(|attr| {
attr.as_ref() attr.ok().filter(|a| {
.map(|a| { String::from_utf8_lossy(a.key.as_ref())
String::from_utf8_lossy(a.key.as_ref()) .trim()
.trim() .eq_ignore_ascii_case("type")
.to_lowercase() })
== "type"
})
.unwrap_or_default()
}) })
.map(|typ| { .map(|typ| {
typ.unwrap() typ.decode_and_unescape_value(reader.decoder())
.decode_and_unescape_value(reader.decoder())
.unwrap_or_default() .unwrap_or_default()
.to_lowercase() .to_lowercase()
}) })

View File

@@ -276,7 +276,7 @@ pub struct InnerContext {
/// The text of the last error logged and emitted as an event. /// The text of the last error logged and emitted as an event.
/// If the ui wants to display an error after a failure, /// If the ui wants to display an error after a failure,
/// `last_error` should be used to avoid races with the event thread. /// `last_error` should be used to avoid races with the event thread.
pub(crate) last_error: std::sync::RwLock<String>, pub(crate) last_error: parking_lot::RwLock<String>,
/// If debug logging is enabled, this contains all necessary information /// If debug logging is enabled, this contains all necessary information
/// ///
@@ -446,7 +446,7 @@ impl Context {
metadata: RwLock::new(None), metadata: RwLock::new(None),
creation_time: tools::Time::now(), creation_time: tools::Time::now(),
last_full_folder_scan: Mutex::new(None), 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), debug_logging: std::sync::RwLock::new(None),
push_subscriber, push_subscriber,
push_subscribed: AtomicBool::new(false), push_subscribed: AtomicBool::new(false),

View File

@@ -244,18 +244,14 @@ impl Kml {
self.tag = KmlTag::PlacemarkPoint; self.tag = KmlTag::PlacemarkPoint;
} else if tag == "coordinates" && self.tag == KmlTag::PlacemarkPoint { } else if tag == "coordinates" && self.tag == KmlTag::PlacemarkPoint {
self.tag = KmlTag::PlacemarkPointCoordinates; self.tag = KmlTag::PlacemarkPointCoordinates;
if let Some(acc) = event.attributes().find(|attr| { if let Some(acc) = event.attributes().find_map(|attr| {
attr.as_ref() attr.ok().filter(|a| {
.map(|a| { String::from_utf8_lossy(a.key.as_ref())
String::from_utf8_lossy(a.key.as_ref()) .trim()
.trim() .eq_ignore_ascii_case("accuracy")
.to_lowercase() })
== "accuracy"
})
.unwrap_or_default()
}) { }) {
let v = acc let v = acc
.unwrap()
.decode_and_unescape_value(reader.decoder()) .decode_and_unescape_value(reader.decoder())
.unwrap_or_default(); .unwrap_or_default();

View File

@@ -50,13 +50,13 @@ impl Context {
/// Set last error string. /// Set last error string.
/// Implemented as blocking as used from macros in different, not always async blocks. /// Implemented as blocking as used from macros in different, not always async blocks.
pub fn set_last_error(&self, error: &str) { 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(); *last_error = error.to_string();
} }
/// Get last error string. /// Get last error string.
pub fn get_last_error(&self) -> 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() last_error.clone()
} }
} }

View File

@@ -1667,10 +1667,11 @@ impl MimeMessage {
{ {
let mut to_list = let mut to_list =
get_all_addresses_from_header(&report.headers, "x-failed-recipients"); get_all_addresses_from_header(&report.headers, "x-failed-recipients");
let to = if to_list.len() == 1 { let to = if to_list.len() != 1 {
Some(to_list.pop().unwrap()) // We do not know which recipient failed
None
} else { } else {
None // We do not know which recipient failed to_list.pop()
}; };
return Ok(Some(DeliveryReport { return Ok(Some(DeliveryReport {