ddnet_account_client/
link_credential.rs

1use ddnet_accounts_shared::{
2    account_server::errors::{AccountServerRequestError, Empty},
3    client::link_credential::{self},
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 [`link_credential`] request.
15#[derive(Error, Debug)]
16pub enum LinkCredentialResult {
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("Linking credential failed: {0}")]
28    Other(anyhow::Error),
29}
30
31impl From<HttpLikeError> for LinkCredentialResult {
32    fn from(value: HttpLikeError) -> Self {
33        Self::HttpLikeError(value)
34    }
35}
36
37impl From<FsLikeError> for LinkCredentialResult {
38    fn from(value: FsLikeError) -> Self {
39        Self::FsLikeError(value)
40    }
41}
42
43/// Link another crendential to an account.
44pub async fn link_credential(
45    account_token_hex: String,
46    credential_auth_token_hex: String,
47    io: &dyn Io,
48) -> anyhow::Result<(), LinkCredentialResult> {
49    link_credential_impl(account_token_hex, credential_auth_token_hex, io.into()).await
50}
51
52async fn link_credential_impl(
53    account_token_hex: String,
54    credential_auth_token_hex: String,
55    io: IoSafe<'_>,
56) -> anyhow::Result<(), LinkCredentialResult> {
57    io.request_link_credential(
58        link_credential::link_credential(account_token_hex, credential_auth_token_hex)
59            .map_err(LinkCredentialResult::Other)?,
60    )
61    .await?
62    .map_err(LinkCredentialResult::AccountServerRequstError)?;
63
64    Ok(())
65}