Skip to main content

proto_blue_api/generated/com/atproto/sync/
getHead.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.sync.getHead
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// DEPRECATED - please use com.atproto.sync.getLatestCommit instead
8/// XRPC Query: com.atproto.sync.getHead
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Params {
12    pub did: proto_blue_syntax::Did,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct Output {
18    pub root: String,
19}
20
21/// Errors a `call()` on this method can return.
22#[derive(Debug, thiserror::Error)]
23pub enum CallError {
24    #[error("HeadNotFound")]
25    HeadNotFound,
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("HeadNotFound") => CallError::HeadNotFound,
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    {
44        let v = &p.did;
45        qp.insert(
46            "did".to_string(),
47            proto_blue_xrpc::QueryValue::String(v.to_string()),
48        );
49    }
50    qp
51}
52
53/// Execute the query.
54pub async fn call(
55    client: &proto_blue_xrpc::XrpcClient,
56    params: Option<&Params>,
57    opts: Option<&proto_blue_xrpc::CallOptions>,
58) -> Result<Output, CallError> {
59    let qp = params.map(to_query_params);
60    let response = match client
61        .query("com.atproto.sync.getHead", qp.as_ref(), opts)
62        .await
63    {
64        Ok(r) => r,
65        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
66        Err(e) => return Err(CallError::Transport(e)),
67    };
68    Ok(serde_json::from_value(response.data)?)
69}
70
71/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
72#[cfg(feature = "server")]
73pub fn register<F, Fut>(
74    server: proto_blue_xrpc::XrpcServer,
75    handler: F,
76) -> proto_blue_xrpc::XrpcServer
77where
78    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
79    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
80        + Send
81        + 'static,
82{
83    let handler = std::sync::Arc::new(handler);
84    server.query("com.atproto.sync.getHead", move |ctx| {
85        let handler = handler.clone();
86        async move {
87            let params = params_from_ctx(&ctx);
88            let out = handler(ctx, params).await?;
89            let value = serde_json::to_value(&out).map_err(|e| {
90                proto_blue_xrpc::XrpcServerError::new(
91                    proto_blue_xrpc::ResponseType::InternalServerError,
92                    format!("output serialize: {e}"),
93                )
94            })?;
95            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
96        }
97    })
98}
99
100#[cfg(feature = "server")]
101fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
102    // Always construct a `Params` — required fields are
103    // validated upstream by the lexicon validator when enabled;
104    // missing values surface as runtime errors from the handler.
105    Some(Params {
106        did: (ctx
107            .params
108            .get("did")
109            .and_then(|v| proto_blue_syntax::Did::new(v).ok()))?,
110    })
111}