Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.temp.dereferenceScope
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// Allows finding the oauth permission scope from a reference
8/// XRPC Query: com.atproto.temp.dereferenceScope
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Params {
12    pub scope: String,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct Output {
18    pub scope: String,
19}
20
21/// Errors a `call()` on this method can return.
22#[derive(Debug, thiserror::Error)]
23pub enum CallError {
24    /// An invalid scope reference was provided.
25    #[error("InvalidScopeReference")]
26    InvalidScopeReference,
27    #[error("{0}")]
28    Xrpc(proto_blue_xrpc::XrpcError),
29    #[error(transparent)]
30    Transport(#[from] proto_blue_xrpc::Error),
31    #[error(transparent)]
32    Json(#[from] serde_json::Error),
33}
34
35fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
36    match err.error.as_deref() {
37        Some("InvalidScopeReference") => CallError::InvalidScopeReference,
38        _ => CallError::Xrpc(err),
39    }
40}
41
42fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
43    let mut qp = proto_blue_xrpc::QueryParams::new();
44    {
45        let v = &p.scope;
46        qp.insert(
47            "scope".to_string(),
48            proto_blue_xrpc::QueryValue::String(v.to_string()),
49        );
50    }
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
62        .query("com.atproto.temp.dereferenceScope", qp.as_ref(), opts)
63        .await
64    {
65        Ok(r) => r,
66        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
67        Err(e) => return Err(CallError::Transport(e)),
68    };
69    Ok(serde_json::from_value(response.data)?)
70}
71
72/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
73#[cfg(feature = "server")]
74pub fn register<F, Fut>(
75    server: proto_blue_xrpc::XrpcServer,
76    handler: F,
77) -> proto_blue_xrpc::XrpcServer
78where
79    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
80    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
81        + Send
82        + 'static,
83{
84    let handler = std::sync::Arc::new(handler);
85    server.query("com.atproto.temp.dereferenceScope", move |ctx| {
86        let handler = handler.clone();
87        async move {
88            let params = params_from_ctx(&ctx);
89            let out = handler(ctx, params).await?;
90            let value = serde_json::to_value(&out).map_err(|e| {
91                proto_blue_xrpc::XrpcServerError::new(
92                    proto_blue_xrpc::ResponseType::InternalServerError,
93                    format!("output serialize: {e}"),
94                )
95            })?;
96            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
97        }
98    })
99}
100
101#[cfg(feature = "server")]
102fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
103    // Always construct a `Params` — required fields are
104    // validated upstream by the lexicon validator when enabled;
105    // missing values surface as runtime errors from the handler.
106    Some(Params {
107        scope: (ctx.params.get("scope").cloned())?,
108    })
109}