Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: com.atproto.sync.getLatestCommit
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// Get the current commit CID & revision of the specified repo. Does not require auth.
8/// XRPC Query: com.atproto.sync.getLatestCommit
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 cid: String,
19    pub rev: proto_blue_syntax::Tid,
20}
21
22/// Errors a `call()` on this method can return.
23#[derive(Debug, thiserror::Error)]
24pub enum CallError {
25    #[error("RepoNotFound")]
26    RepoNotFound,
27    #[error("RepoTakendown")]
28    RepoTakendown,
29    #[error("RepoSuspended")]
30    RepoSuspended,
31    #[error("RepoDeactivated")]
32    RepoDeactivated,
33    #[error("{0}")]
34    Xrpc(proto_blue_xrpc::XrpcError),
35    #[error(transparent)]
36    Transport(#[from] proto_blue_xrpc::Error),
37    #[error(transparent)]
38    Json(#[from] serde_json::Error),
39}
40
41fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
42    match err.error.as_deref() {
43        Some("RepoNotFound") => CallError::RepoNotFound,
44        Some("RepoTakendown") => CallError::RepoTakendown,
45        Some("RepoSuspended") => CallError::RepoSuspended,
46        Some("RepoDeactivated") => CallError::RepoDeactivated,
47        _ => CallError::Xrpc(err),
48    }
49}
50
51fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
52    let mut qp = proto_blue_xrpc::QueryParams::new();
53    {
54        let v = &p.did;
55        qp.insert(
56            "did".to_string(),
57            proto_blue_xrpc::QueryValue::String(v.to_string()),
58        );
59    }
60    qp
61}
62
63/// Execute the query.
64pub async fn call(
65    client: &proto_blue_xrpc::XrpcClient,
66    params: Option<&Params>,
67    opts: Option<&proto_blue_xrpc::CallOptions>,
68) -> Result<Output, CallError> {
69    let qp = params.map(to_query_params);
70    let response = match client
71        .query("com.atproto.sync.getLatestCommit", qp.as_ref(), opts)
72        .await
73    {
74        Ok(r) => r,
75        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
76        Err(e) => return Err(CallError::Transport(e)),
77    };
78    Ok(serde_json::from_value(response.data)?)
79}
80
81/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
82#[cfg(feature = "server")]
83pub fn register<F, Fut>(
84    server: proto_blue_xrpc::XrpcServer,
85    handler: F,
86) -> proto_blue_xrpc::XrpcServer
87where
88    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
89    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
90        + Send
91        + 'static,
92{
93    let handler = std::sync::Arc::new(handler);
94    server.query("com.atproto.sync.getLatestCommit", move |ctx| {
95        let handler = handler.clone();
96        async move {
97            let params = params_from_ctx(&ctx);
98            let out = handler(ctx, params).await?;
99            let value = serde_json::to_value(&out).map_err(|e| {
100                proto_blue_xrpc::XrpcServerError::new(
101                    proto_blue_xrpc::ResponseType::InternalServerError,
102                    format!("output serialize: {e}"),
103                )
104            })?;
105            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
106        }
107    })
108}
109
110#[cfg(feature = "server")]
111fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
112    // Always construct a `Params` — required fields are
113    // validated upstream by the lexicon validator when enabled;
114    // missing values surface as runtime errors from the handler.
115    Some(Params {
116        did: (ctx
117            .params
118            .get("did")
119            .and_then(|v| proto_blue_syntax::Did::new(v).ok()))?,
120    })
121}