Skip to main content

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

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