Skip to main content

proto_blue_api/generated/com/atproto/server/
updateEmail.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.server.updateEmail
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// Update an account's email.
8/// XRPC Procedure: com.atproto.server.updateEmail
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Input {
12    pub email: String,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub email_auth_factor: Option<bool>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub token: Option<String>,
17}
18
19/// Errors a `call()` on this method can return.
20#[derive(Debug, thiserror::Error)]
21pub enum CallError {
22    #[error("ExpiredToken")]
23    ExpiredToken,
24    #[error("InvalidToken")]
25    InvalidToken,
26    #[error("TokenRequired")]
27    TokenRequired,
28    #[error("{0}")]
29    Xrpc(proto_blue_xrpc::XrpcError),
30    #[error(transparent)]
31    Transport(#[from] proto_blue_xrpc::Error),
32    #[error(transparent)]
33    Json(#[from] serde_json::Error),
34}
35
36fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
37    match err.error.as_deref() {
38        Some("ExpiredToken") => CallError::ExpiredToken,
39        Some("InvalidToken") => CallError::InvalidToken,
40        Some("TokenRequired") => CallError::TokenRequired,
41        _ => CallError::Xrpc(err),
42    }
43}
44
45/// Execute the procedure.
46pub async fn call(
47    client: &proto_blue_xrpc::XrpcClient,
48    input: &Input,
49    opts: Option<&proto_blue_xrpc::CallOptions>,
50) -> Result<serde_json::Value, CallError> {
51    let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
52    let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
53    let response = match client
54        .procedure("com.atproto.server.updateEmail", qp_ref, Some(body), opts)
55        .await
56    {
57        Ok(r) => r,
58        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
59        Err(e) => return Err(CallError::Transport(e)),
60    };
61    Ok(response.data)
62}
63
64/// Register a typed handler for this procedure on an [`proto_blue_xrpc::XrpcServer`].
65#[cfg(feature = "server")]
66pub fn register<F, Fut>(
67    server: proto_blue_xrpc::XrpcServer,
68    handler: F,
69) -> proto_blue_xrpc::XrpcServer
70where
71    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
72    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>>
73        + Send
74        + 'static,
75{
76    let handler = std::sync::Arc::new(handler);
77    server.procedure("com.atproto.server.updateEmail", move |ctx| {
78        let handler = handler.clone();
79        async move {
80            let input = match ctx.json_body()? {
81                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| {
82                    proto_blue_xrpc::XrpcServerError::new(
83                        proto_blue_xrpc::ResponseType::InvalidRequest,
84                        format!("input deserialize: {e}"),
85                    )
86                })?),
87                None => None,
88            };
89            let out = handler(ctx, input).await?;
90            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
91        }
92    })
93}