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