Change to use and_then

This commit is contained in:
notplants 2022-01-12 12:37:15 -05:00
parent a3852d89c9
commit 6c6413c1b4
1 changed files with 19 additions and 21 deletions

View File

@ -137,10 +137,13 @@ impl Sbot {
pub async fn get_subset_stream(
&mut self,
query: SubsetQuery,
options: Option<SubsetQueryOptions>
options: Option<SubsetQueryOptions>,
) -> Result<impl Stream<Item = Result<SsbMessageKVT, GolgiError>>, GolgiError> {
let mut sbot_connection = self.get_sbot_connection().await?;
let req_id = sbot_connection.client.getsubset_req_send(query, options).await?;
let req_id = sbot_connection
.client
.getsubset_req_send(query, options)
.await?;
let get_subset_stream =
get_source_stream(sbot_connection.rpc_reader, req_id, utils::kvt_res_parse).await;
Ok(get_subset_stream)
@ -251,7 +254,7 @@ impl Sbot {
let query_options = SubsetQueryOptions {
descending: Some(true),
keys: None,
page_limit: None
page_limit: None,
};
let get_subset_kvt_stream = self.get_subset_stream(query, Some(query_options)).await?;
// map into Stream<Item=Result<SsbMessageValue, GolgiError>>
@ -284,31 +287,26 @@ impl Sbot {
// iterate through the vector looking for most recent about message with the given key
let latest_about_message: 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(GolgiError::Sbot("error while looking for about message with given key".to_string()))?;
.find(|res| match res {
Ok(msg) => msg.content.get(key).is_some(),
Err(_) => false,
})
.await
.ok_or(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<Option<&str>>
.map(|value| value.as_str())
// Option<Option<&str>> -> Option<&str>
.flatten()
.content
.get(key)
// Option<&Value> -> <Option<&str>
.and_then(|value| value.as_str())
// Option<&str> -> Option<String>
.map(|value| value.to_string())
}
Err(_) => {
None
}
Err(_) => None,
};
// return value is either `Ok(Some(String))` or `Ok(None)`
Ok(latest_about_value)