proto-blue-api 0.3.3

AT Protocol high-level API: agent, rich text, moderation, generated types
Documentation
// Generated by atproto-codegen. Do not edit.
//! Lexicon: com.atproto.server.confirmEmail
#![allow(clippy::pedantic, clippy::nursery, clippy::all)]

use serde::{Deserialize, Serialize};

/// Confirm an email using a token from com.atproto.server.requestEmailConfirmation.
/// XRPC Procedure: com.atproto.server.confirmEmail
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Input {
    pub email: String,
    pub token: String,
}

/// Errors a `call()` on this method can return.
#[derive(Debug, thiserror::Error)]
pub enum CallError {
    #[error("AccountNotFound")]
    AccountNotFound,
    #[error("ExpiredToken")]
    ExpiredToken,
    #[error("InvalidToken")]
    InvalidToken,
    #[error("InvalidEmail")]
    InvalidEmail,
    #[error("{0}")]
    Xrpc(proto_blue_xrpc::XrpcError),
    #[error(transparent)]
    Transport(#[from] proto_blue_xrpc::Error),
    #[error(transparent)]
    Json(#[from] serde_json::Error),
}

fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
    match err.error.as_deref() {
        Some("AccountNotFound") => CallError::AccountNotFound,
        Some("ExpiredToken") => CallError::ExpiredToken,
        Some("InvalidToken") => CallError::InvalidToken,
        Some("InvalidEmail") => CallError::InvalidEmail,
        _ => CallError::Xrpc(err),
    }
}

/// Execute the procedure.
pub async fn call(
    client: &proto_blue_xrpc::XrpcClient,
    input: &Input,
    opts: Option<&proto_blue_xrpc::CallOptions>,
) -> Result<serde_json::Value, CallError> {
    let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
    let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
    let response = match client
        .procedure("com.atproto.server.confirmEmail", qp_ref, Some(body), opts)
        .await
    {
        Ok(r) => r,
        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
        Err(e) => return Err(CallError::Transport(e)),
    };
    Ok(response.data)
}

/// Register a typed handler for this procedure on an [`proto_blue_xrpc::XrpcServer`].
#[cfg(feature = "server")]
pub fn register<F, Fut>(
    server: proto_blue_xrpc::XrpcServer,
    handler: F,
) -> proto_blue_xrpc::XrpcServer
where
    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>>
        + Send
        + 'static,
{
    let handler = std::sync::Arc::new(handler);
    server.procedure("com.atproto.server.confirmEmail", move |ctx| {
        let handler = handler.clone();
        async move {
            let input = match ctx.json_body()? {
                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| {
                    proto_blue_xrpc::XrpcServerError::new(
                        proto_blue_xrpc::ResponseType::InvalidRequest,
                        format!("input deserialize: {e}"),
                    )
                })?),
                None => None,
            };
            let out = handler(ctx, input).await?;
            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
        }
    })
}