Skip to content

Commit fc703a5

Browse files
Daniel SalinasDaniel Salinas
authored andcommitted
Eliminate some unnecessary .context removals
1 parent 9f117ca commit fc703a5

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
time::Duration,
77
};
88

9-
use anyhow::anyhow;
9+
use anyhow::{anyhow, Context};
1010
use futures_util::pin_mut;
1111
#[cfg(not(any(target_family = "wasm", feature = "js")))]
1212
use matrix_sdk::media::MediaFileHandle as SdkMediaFileHandle;
@@ -299,8 +299,7 @@ impl Client {
299299
let session_delegate = session_delegate.clone();
300300
Box::new(move |client| {
301301
let session_delegate = session_delegate.clone();
302-
let user_id =
303-
client.user_id().ok_or_else(|| anyhow!("user isn't logged in"))?;
302+
let user_id = client.user_id().context("user isn't logged in")?;
304303
Ok(Self::retrieve_session(session_delegate, user_id)?)
305304
})
306305
},
@@ -875,23 +874,19 @@ impl Client {
875874
}
876875

877876
pub fn user_id(&self) -> Result<String, ClientError> {
878-
let user_id = self.inner.user_id().ok_or_else(|| anyhow!("No User ID found"))?;
877+
let user_id = self.inner.user_id().context("No User ID found")?;
879878
Ok(user_id.to_string())
880879
}
881880

882881
/// The server name part of the current user ID
883882
pub fn user_id_server_name(&self) -> Result<String, ClientError> {
884-
let user_id = self.inner.user_id().ok_or_else(|| anyhow!("No User ID found"))?;
883+
let user_id = self.inner.user_id().context("No User ID found")?;
885884
Ok(user_id.server_name().to_string())
886885
}
887886

888887
pub async fn display_name(&self) -> Result<String, ClientError> {
889-
let display_name = self
890-
.inner
891-
.account()
892-
.get_display_name()
893-
.await?
894-
.ok_or_else(|| anyhow!("No User ID found"))?;
888+
let display_name =
889+
self.inner.account().get_display_name().await?.context("No User ID found")?;
895890
Ok(display_name)
896891
}
897892

@@ -929,7 +924,7 @@ impl Client {
929924
}
930925

931926
pub fn device_id(&self) -> Result<String, ClientError> {
932-
let device_id = self.inner.device_id().ok_or_else(|| anyhow!("No Device ID found"))?;
927+
let device_id = self.inner.device_id().context("No Device ID found")?;
933928
Ok(device_id.to_string())
934929
}
935930

@@ -966,8 +961,7 @@ impl Client {
966961
data: Vec<u8>,
967962
progress_watcher: Option<Box<dyn ProgressWatcher>>,
968963
) -> Result<String, ClientError> {
969-
let mime_type: mime::Mime =
970-
mime_type.parse().map_err(|e| anyhow!("Parsing mime type: {}", e))?;
964+
let mime_type: mime::Mime = mime_type.parse().context("Parsing mime type")?;
971965
let request = self.inner.media().upload(&mime_type, data, None);
972966

973967
if let Some(progress_watcher) = progress_watcher {
@@ -1031,14 +1025,13 @@ impl Client {
10311025
{
10321026
return Ok(Arc::new(session_verification_controller.clone()));
10331027
}
1034-
let user_id =
1035-
self.inner.user_id().ok_or_else(|| anyhow!("Failed retrieving current user_id"))?;
1028+
let user_id = self.inner.user_id().context("Failed retrieving current user_id")?;
10361029
let user_identity = self
10371030
.inner
10381031
.encryption()
10391032
.get_user_identity(user_id)
10401033
.await?
1041-
.ok_or_else(|| anyhow!("Failed retrieving user identity"))?;
1034+
.context("Failed retrieving user identity")?;
10421035

10431036
let session_verification_controller = SessionVerificationController::new(
10441037
self.inner.encryption(),
@@ -1334,14 +1327,13 @@ impl Client {
13341327
room_id: String,
13351328
via_servers: Vec<String>,
13361329
) -> Result<Arc<RoomPreview>, ClientError> {
1337-
let room_id = RoomId::parse(&room_id)
1338-
.map_err(|e| anyhow!("room_id is not a valid room id: {}", e))?;
1330+
let room_id = RoomId::parse(&room_id).context("room_id is not a valid room id")?;
13391331

13401332
let via_servers = via_servers
13411333
.into_iter()
13421334
.map(ServerName::parse)
13431335
.collect::<Result<Vec<_>, _>>()
1344-
.map_err(|e| anyhow!("at least one `via` server name is invalid: {}", e))?;
1336+
.context("at least one `via` server name is invalid")?;
13451337

13461338
// The `into()` call below doesn't work if I do `(&room_id).into()`, so I let
13471339
// rustc win that one fight.
@@ -1357,8 +1349,8 @@ impl Client {
13571349
&self,
13581350
room_alias: String,
13591351
) -> Result<Arc<RoomPreview>, ClientError> {
1360-
let room_alias = RoomAliasId::parse(&room_alias)
1361-
.map_err(|e| anyhow!("room_alias is not a valid room alias: {}", e))?;
1352+
let room_alias =
1353+
RoomAliasId::parse(&room_alias).context("room_alias is not a valid room alias")?;
13621354

13631355
// The `into()` call below doesn't work if I do `(&room_id).into()`, so I let
13641356
// rustc win that one fight.
@@ -1737,7 +1729,7 @@ impl Client {
17371729
}
17381730

17391731
fn session_inner(client: matrix_sdk::Client) -> Result<Session, ClientError> {
1740-
let auth_api = client.auth_api().ok_or_else(|| anyhow!("Missing authentication API"))?;
1732+
let auth_api = client.auth_api().context("Missing authentication API")?;
17411733

17421734
let homeserver_url = client.homeserver().into();
17431735
let sliding_sync_version = client.sliding_sync_version();
@@ -2055,7 +2047,7 @@ impl Session {
20552047
let matrix_sdk::authentication::matrix::MatrixSession {
20562048
meta: matrix_sdk::SessionMeta { user_id, device_id },
20572049
tokens: matrix_sdk::SessionTokens { access_token, refresh_token },
2058-
} = a.session().ok_or_else(|| anyhow!("Missing session"))?;
2050+
} = a.session().context("Missing session")?;
20592051

20602052
Ok(Session {
20612053
access_token,
@@ -2072,9 +2064,8 @@ impl Session {
20722064
let matrix_sdk::authentication::oauth::UserSession {
20732065
meta: matrix_sdk::SessionMeta { user_id, device_id },
20742066
tokens: matrix_sdk::SessionTokens { access_token, refresh_token },
2075-
} = api.user_session().ok_or_else(|| anyhow!("Missing session"))?;
2076-
let client_id =
2077-
api.client_id().ok_or_else(|| anyhow!("OIDC client ID is missing."))?.clone();
2067+
} = api.user_session().context("Missing session")?;
2068+
let client_id = api.client_id().context("OIDC client ID is missing.")?.clone();
20782069
let oidc_data = OidcSessionData { client_id };
20792070

20802071
let oidc_data = serde_json::to_string(&oidc_data).ok();
@@ -2204,7 +2195,7 @@ impl MediaFileHandle {
22042195
.read()
22052196
.unwrap()
22062197
.as_ref()
2207-
.ok_or_else(|| anyhow!("MediaFileHandle must not be used after calling persist"))?
2198+
.context("MediaFileHandle must not be used after calling persist")?
22082199
.path()
22092200
.to_str()
22102201
.unwrap()
@@ -2223,7 +2214,7 @@ impl MediaFileHandle {
22232214
return Ok(
22242215
match guard
22252216
.take()
2226-
.ok_or_else(|| anyhow!("MediaFileHandle was already persisted"))?
2217+
.context("MediaFileHandle was already persisted")?
22272218
.persist(path.as_ref())
22282219
{
22292220
Ok(_) => true,

0 commit comments

Comments
 (0)