Convert result to option

This commit is contained in:
notplants 2022-01-12 15:01:45 -05:00
parent 615431496b
commit 0addd8dc47
1 changed files with 15 additions and 18 deletions

View File

@ -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<SsbMessageValue, GolgiError> = about_message_stream
let latest_about_message_res: Option<Result<SsbMessageValue, GolgiError>> = 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> -> <Option<&str>
.and_then(|value| value.as_str())
// Option<&str> -> Option<String>
.map(|value| value.to_string())
}
Err(_) => None,
};
.await;
// Option<Result<SsbMessageValue, GolgiError>> -> Option<SsbMessageValue>
let latest_about_message = latest_about_message_res.and_then(|msg| msg.ok());
// Option<SsbMessageValue> -> Option<String>
let latest_about_value = latest_about_message.and_then(|msg| {
msg
// SsbMessageValue -> Option<&Value>
.content
.get(key)
// Option<&Value> -> <Option<&str>
.and_then(|value| value.as_str())
// Option<&str> -> Option<String>
.map(|value| value.to_string())
});
// return value is either `Ok(Some(String))` or `Ok(None)`
Ok(latest_about_value)
}