Convert result to option
This commit is contained in:
parent
615431496b
commit
0addd8dc47
17
src/sbot.rs
17
src/sbot.rs
|
@ -281,18 +281,17 @@ 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) => {
|
||||
.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
|
||||
|
@ -301,9 +300,7 @@ impl Sbot {
|
|||
.and_then(|value| value.as_str())
|
||||
// Option<&str> -> Option<String>
|
||||
.map(|value| value.to_string())
|
||||
}
|
||||
Err(_) => None,
|
||||
};
|
||||
});
|
||||
// return value is either `Ok(Some(String))` or `Ok(None)`
|
||||
Ok(latest_about_value)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue