Use ok_or_else

This commit is contained in:
notplants 2022-01-12 12:42:50 -05:00
parent 6c6413c1b4
commit 615431496b
2 changed files with 10 additions and 14 deletions

View File

@ -47,10 +47,10 @@ impl SsbMessageValue {
let msg_type = self let msg_type = self
.content .content
.get("type") .get("type")
.ok_or(GolgiError::ContentType("type field not found".to_string()))?; .ok_or_else(|| GolgiError::ContentType("type field not found".to_string()))?;
let mtype_str: &str = msg_type.as_str().ok_or(GolgiError::ContentType( let mtype_str: &str = msg_type.as_str().ok_or_else(|| {
"type field value is not a string as expected".to_string(), GolgiError::ContentType("type field value is not a string as expected".to_string())
))?; })?;
let enum_type = match mtype_str { let enum_type = match mtype_str {
"about" => SsbMessageContentType::About, "about" => SsbMessageContentType::About,
"post" => SsbMessageContentType::Post, "post" => SsbMessageContentType::Post,

View File

@ -3,7 +3,7 @@ use async_std::{
net::TcpStream, net::TcpStream,
stream::{Stream, StreamExt}, stream::{Stream, StreamExt},
}; };
use futures::{pin_mut, TryStreamExt}; use futures::pin_mut;
use kuska_handshake::async_std::BoxStream; use kuska_handshake::async_std::BoxStream;
use kuska_sodiumoxide::crypto::{auth, sign::ed25519}; use kuska_sodiumoxide::crypto::{auth, sign::ed25519};
@ -162,13 +162,9 @@ impl Sbot {
let id = result let id = result
.get("id") .get("id")
.ok_or(GolgiError::Sbot( .ok_or_else(|| GolgiError::Sbot("id key not found on whoami call".to_string()))?
"id key not found on whoami call".to_string(),
))?
.as_str() .as_str()
.ok_or(GolgiError::Sbot( .ok_or_else(|| GolgiError::Sbot("whoami returned non-string value".to_string()))?;
"whoami returned non-string value".to_string(),
))?;
Ok(id.to_string()) Ok(id.to_string())
} }
@ -292,9 +288,9 @@ impl Sbot {
Err(_) => false, Err(_) => false,
}) })
.await .await
.ok_or(GolgiError::Sbot( .ok_or_else(|| {
"error while looking for about message with given key".to_string(), 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