ddnet_account_client/
unlink_credential.rs

1use ddnet_accounts_shared::{
2    account_server::errors::{AccountServerRequestError, Empty},
3    client::unlink_credential,
4};
5
6use thiserror::Error;
7
8use crate::{
9    errors::{FsLikeError, HttpLikeError},
10    interface::Io,
11    safe_interface::{IoSafe, SafeIo},
12};
13
14/// The result of a [`unlink_credential`] request.
15#[derive(Error, Debug)]
16pub enum UnlinkCredentialResult {
17    /// A http like error occurred.
18    #[error("{0}")]
19    HttpLikeError(HttpLikeError),
20    /// A fs like error occurred.
21    #[error("{0}")]
22    FsLikeError(FsLikeError),
23    /// The account server responded with an error.
24    #[error("{0}")]
25    AccountServerRequstError(AccountServerRequestError<Empty>),
26    /// Errors that are not handled explicitly.
27    #[error("Unlinking credential failed: {0}")]
28    Other(anyhow::Error),
29}
30
31impl From<HttpLikeError> for UnlinkCredentialResult {
32    fn from(value: HttpLikeError) -> Self {
33        Self::HttpLikeError(value)
34    }
35}
36
37impl From<FsLikeError> for UnlinkCredentialResult {
38    fn from(value: FsLikeError) -> Self {
39        Self::FsLikeError(value)
40    }
41}
42
43/// Unlink a credential from an account.
44/// If the credential is the last one linked, this function fails.
45pub async fn unlink_credential(
46    credential_auth_token_hex: String,
47    io: &dyn Io,
48) -> anyhow::Result<(), UnlinkCredentialResult> {
49    unlink_credential_impl(credential_auth_token_hex, io.into()).await
50}
51
52async fn unlink_credential_impl(
53    credential_auth_token_hex: String,
54    io: IoSafe<'_>,
55) -> anyhow::Result<(), UnlinkCredentialResult> {
56    io.request_unlink_credential(
57        unlink_credential::unlink_credential(credential_auth_token_hex)
58            .map_err(UnlinkCredentialResult::Other)?,
59    )
60    .await?
61    .map_err(UnlinkCredentialResult::AccountServerRequstError)?;
62
63    Ok(())
64}