proto_blue_api/generated/com/atproto/server/
createAccount.rs1use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Input {
11 #[serde(skip_serializing_if = "Option::is_none")]
12 pub did: Option<String>,
13 #[serde(skip_serializing_if = "Option::is_none")]
14 pub email: Option<String>,
15 pub handle: String,
16 #[serde(skip_serializing_if = "Option::is_none")]
17 pub invite_code: Option<String>,
18 #[serde(skip_serializing_if = "Option::is_none")]
19 pub password: Option<String>,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 pub plc_op: Option<serde_json::Value>,
22 #[serde(skip_serializing_if = "Option::is_none")]
23 pub recovery_key: Option<String>,
24 #[serde(skip_serializing_if = "Option::is_none")]
25 pub verification_code: Option<String>,
26 #[serde(skip_serializing_if = "Option::is_none")]
27 pub verification_phone: Option<String>,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(rename_all = "camelCase")]
33pub struct Output {
34 pub access_jwt: String,
35 pub did: String,
36 #[serde(skip_serializing_if = "Option::is_none")]
37 pub did_doc: Option<serde_json::Value>,
38 pub handle: String,
39 pub refresh_jwt: String,
40}
41
42#[derive(Debug, thiserror::Error)]
44pub enum CallError {
45 #[error("InvalidHandle")]
46 InvalidHandle,
47 #[error("InvalidPassword")]
48 InvalidPassword,
49 #[error("InvalidInviteCode")]
50 InvalidInviteCode,
51 #[error("HandleNotAvailable")]
52 HandleNotAvailable,
53 #[error("UnsupportedDomain")]
54 UnsupportedDomain,
55 #[error("UnresolvableDid")]
56 UnresolvableDid,
57 #[error("IncompatibleDidDoc")]
58 IncompatibleDidDoc,
59 #[error("{0}")]
60 Xrpc(proto_blue_xrpc::XrpcError),
61 #[error(transparent)]
62 Transport(#[from] proto_blue_xrpc::Error),
63 #[error(transparent)]
64 Json(#[from] serde_json::Error),
65}
66
67fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
68 match err.error.as_deref() {
69 Some("InvalidHandle") => CallError::InvalidHandle,
70 Some("InvalidPassword") => CallError::InvalidPassword,
71 Some("InvalidInviteCode") => CallError::InvalidInviteCode,
72 Some("HandleNotAvailable") => CallError::HandleNotAvailable,
73 Some("UnsupportedDomain") => CallError::UnsupportedDomain,
74 Some("UnresolvableDid") => CallError::UnresolvableDid,
75 Some("IncompatibleDidDoc") => CallError::IncompatibleDidDoc,
76 _ => CallError::Xrpc(err),
77 }
78}
79
80pub async fn call(
82 client: &proto_blue_xrpc::XrpcClient,
83 input: &Input,
84 opts: Option<&proto_blue_xrpc::CallOptions>,
85) -> Result<Output, CallError> {
86 let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
87 let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
88 let response = match client
89 .procedure("com.atproto.server.createAccount", qp_ref, Some(body), opts)
90 .await
91 {
92 Ok(r) => r,
93 Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
94 Err(e) => return Err(CallError::Transport(e)),
95 };
96 Ok(serde_json::from_value(response.data)?)
97}
98
99#[cfg(feature = "server")]
101pub fn register<F, Fut>(
102 server: proto_blue_xrpc::XrpcServer,
103 handler: F,
104) -> proto_blue_xrpc::XrpcServer
105where
106 F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
107 Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
108 + Send
109 + 'static,
110{
111 let handler = std::sync::Arc::new(handler);
112 server.procedure("com.atproto.server.createAccount", move |ctx| {
113 let handler = handler.clone();
114 async move {
115 let input = match ctx.json_body()? {
116 Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| {
117 proto_blue_xrpc::XrpcServerError::new(
118 proto_blue_xrpc::ResponseType::InvalidRequest,
119 format!("input deserialize: {e}"),
120 )
121 })?),
122 None => None,
123 };
124 let out = handler(ctx, input).await?;
125 let value = serde_json::to_value(&out).map_err(|e| {
126 proto_blue_xrpc::XrpcServerError::new(
127 proto_blue_xrpc::ResponseType::InternalServerError,
128 format!("output serialize: {e}"),
129 )
130 })?;
131 Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
132 }
133 })
134}