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