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