Skip to content

Commit fa7f596

Browse files
Daniel SalinasDaniel Salinas
authored andcommitted
Eliminate some unnecessary .context removals
1 parent d6829bd commit fa7f596

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
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;
@@ -301,8 +301,7 @@ impl Client {
301301
let session_delegate = session_delegate.clone();
302302
Box::new(move |client| {
303303
let session_delegate = session_delegate.clone();
304-
let user_id =
305-
client.user_id().ok_or_else(|| anyhow!("user isn't logged in"))?;
304+
let user_id = client.user_id().context("user isn't logged in")?;
306305
Ok(Self::retrieve_session(session_delegate, user_id)?)
307306
})
308307
},
@@ -877,23 +876,19 @@ impl Client {
877876
}
878877

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

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

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

@@ -931,7 +926,7 @@ impl Client {
931926
}
932927

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

@@ -968,8 +963,7 @@ impl Client {
968963
data: Vec<u8>,
969964
progress_watcher: Option<Box<dyn ProgressWatcher>>,
970965
) -> Result<String, ClientError> {
971-
let mime_type: mime::Mime =
972-
mime_type.parse().map_err(|e| anyhow!("Parsing mime type: {}", e))?;
966+
let mime_type: mime::Mime = mime_type.parse().context("Parsing mime type")?;
973967
let request = self.inner.media().upload(&mime_type, data, None);
974968

975969
if let Some(progress_watcher) = progress_watcher {
@@ -1033,14 +1027,13 @@ impl Client {
10331027
{
10341028
return Ok(Arc::new(session_verification_controller.clone()));
10351029
}
1036-
let user_id =
1037-
self.inner.user_id().ok_or_else(|| anyhow!("Failed retrieving current user_id"))?;
1030+
let user_id = self.inner.user_id().context("Failed retrieving current user_id")?;
10381031
let user_identity = self
10391032
.inner
10401033
.encryption()
10411034
.get_user_identity(user_id)
10421035
.await?
1043-
.ok_or_else(|| anyhow!("Failed retrieving user identity"))?;
1036+
.context("Failed retrieving user identity")?;
10441037

10451038
let session_verification_controller = SessionVerificationController::new(
10461039
self.inner.encryption(),
@@ -1336,14 +1329,13 @@ impl Client {
13361329
room_id: String,
13371330
via_servers: Vec<String>,
13381331
) -> Result<Arc<RoomPreview>, ClientError> {
1339-
let room_id = RoomId::parse(&room_id)
1340-
.map_err(|e| anyhow!("room_id is not a valid room id: {}", e))?;
1332+
let room_id = RoomId::parse(&room_id).context("room_id is not a valid room id")?;
13411333

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

13481340
// The `into()` call below doesn't work if I do `(&room_id).into()`, so I let
13491341
// rustc win that one fight.
@@ -1359,8 +1351,8 @@ impl Client {
13591351
&self,
13601352
room_alias: String,
13611353
) -> Result<Arc<RoomPreview>, ClientError> {
1362-
let room_alias = RoomAliasId::parse(&room_alias)
1363-
.map_err(|e| anyhow!("room_alias is not a valid room alias: {}", e))?;
1354+
let room_alias =
1355+
RoomAliasId::parse(&room_alias).context("room_alias is not a valid room alias")?;
13641356

13651357
// The `into()` call below doesn't work if I do `(&room_id).into()`, so I let
13661358
// 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,

crates/matrix-sdk/src/client/futures.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ impl SendMediaUploadRequest {
182182

183183
/// Get a subscriber to observe the progress of sending the request
184184
/// body.
185-
#[cfg(not(target_family = "wasm"))]
186185
pub fn subscribe_to_send_progress(&self) -> Subscriber<TransmissionProgress> {
187186
self.send_request.send_progress.subscribe()
188187
}

0 commit comments

Comments
 (0)