Skip to main content

proto_blue_api/generated/com/atproto/temp/
checkHandleAvailability.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.temp.checkHandleAvailability
3
4use serde::{Deserialize, Serialize};
5
6/// Checks whether the provided handle is available. If the handle is not available, available suggestions will be returned. Optional inputs will be used to generate suggestions.
7/// XRPC Query: com.atproto.temp.checkHandleAvailability
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Params {
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub birth_date: Option<String>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub email: Option<String>,
15    pub handle: String,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19#[serde(rename_all = "camelCase")]
20pub struct Output {
21    pub handle: String,
22    pub result: serde_json::Value,
23}
24
25/// Errors a `call()` on this method can return.
26#[derive(Debug, thiserror::Error)]
27pub enum CallError {
28    /// An invalid email was provided.
29    #[error("InvalidEmail")]
30    InvalidEmail,
31    #[error("{0}")]
32    Xrpc(proto_blue_xrpc::XrpcError),
33    #[error(transparent)]
34    Transport(#[from] proto_blue_xrpc::Error),
35    #[error(transparent)]
36    Json(#[from] serde_json::Error),
37}
38
39fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
40    match err.error.as_deref() {
41        Some("InvalidEmail") => CallError::InvalidEmail,
42        _ => CallError::Xrpc(err),
43    }
44}
45
46fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
47    let mut qp = proto_blue_xrpc::QueryParams::new();
48    if let Some(v) = &p.birth_date { qp.insert("birthDate".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
49    if let Some(v) = &p.email { qp.insert("email".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
50    { let v = &p.handle; qp.insert("handle".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
51    qp
52}
53
54/// Execute the query.
55pub async fn call(
56    client: &proto_blue_xrpc::XrpcClient,
57    params: Option<&Params>,
58    opts: Option<&proto_blue_xrpc::CallOptions>,
59) -> Result<Output, CallError> {
60    let qp = params.map(to_query_params);
61    let response = match client.query("com.atproto.temp.checkHandleAvailability", qp.as_ref(), opts).await {
62        Ok(r) => r,
63        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
64        Err(e) => return Err(CallError::Transport(e)),
65    };
66    Ok(serde_json::from_value(response.data)?)
67}
68
69/// Register a typed handler for this method on an [`XrpcServer`].
70#[cfg(feature = "server")]
71pub fn register<F, Fut>(
72server: proto_blue_xrpc::XrpcServer,
73handler: F,
74) -> proto_blue_xrpc::XrpcServer
75where
76    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
77    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
78{
79    let handler = std::sync::Arc::new(handler);
80    server.query("com.atproto.temp.checkHandleAvailability", move |ctx| {
81        let handler = handler.clone();
82        async move {
83            let params = params_from_ctx(&ctx);
84            let out = handler(ctx, params).await?;
85            let value = serde_json::to_value(&out)
86                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
87            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
88        }
89    })
90}
91
92#[cfg(feature = "server")]
93fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
94    // Always construct a `Params` — required fields are
95    // validated upstream by the lexicon validator when enabled;
96    // missing values surface as runtime errors from the handler.
97    Some(Params {
98        birth_date: ctx.params.get("birthDate").cloned(),
99        email: ctx.params.get("email").cloned(),
100        handle: (ctx.params.get("handle").cloned())?,
101    })
102}
103
104/// Indicates the provided handle is available.
105#[derive(Debug, Clone, Serialize, Deserialize)]
106#[serde(rename_all = "camelCase")]
107pub struct ResultAvailable {
108}
109
110/// Indicates the provided handle is unavailable and gives suggestions of available handles.
111#[derive(Debug, Clone, Serialize, Deserialize)]
112#[serde(rename_all = "camelCase")]
113pub struct ResultUnavailable {
114    pub suggestions: Vec<Suggestion>,
115}
116
117#[derive(Debug, Clone, Serialize, Deserialize)]
118#[serde(rename_all = "camelCase")]
119pub struct Suggestion {
120    pub handle: String,
121    pub method: String,
122}
123