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