From 0addd8dc472c96874aad611011bc9b640345df54 Mon Sep 17 00:00:00 2001 From: notplants Date: Wed, 12 Jan 2022 15:01:45 -0500 Subject: [PATCH] Convert result to option --- src/sbot.rs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/sbot.rs b/src/sbot.rs index 2c1abb3..f935e80 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -281,29 +281,26 @@ impl Sbot { // now we have a stream of about messages with most recent at the front of the vector pin_mut!(about_message_stream); // iterate through the vector looking for most recent about message with the given key - let latest_about_message: Result = about_message_stream + let latest_about_message_res: Option> = about_message_stream // find the first msg that contains the field `key` .find(|res| match res { Ok(msg) => msg.content.get(key).is_some(), Err(_) => false, }) - .await - .ok_or_else(|| { - GolgiError::Sbot("error while looking for about message with given key".to_string()) - })?; - let latest_about_value = match latest_about_message { - Ok(msg) => { - msg - // SsbMessageValue -> Option<&Value> - .content - .get(key) - // Option<&Value> -> - .and_then(|value| value.as_str()) - // Option<&str> -> Option - .map(|value| value.to_string()) - } - Err(_) => None, - }; + .await; + // Option> -> Option + let latest_about_message = latest_about_message_res.and_then(|msg| msg.ok()); + // Option -> Option + let latest_about_value = latest_about_message.and_then(|msg| { + msg + // SsbMessageValue -> Option<&Value> + .content + .get(key) + // Option<&Value> -> + .and_then(|value| value.as_str()) + // Option<&str> -> Option + .map(|value| value.to_string()) + }); // return value is either `Ok(Some(String))` or `Ok(None)` Ok(latest_about_value) }