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
4use serde::{Deserialize, Serialize};
5
6/// Get the current commit CID & revision of the specified repo. Does not require auth.
7/// XRPC Query: com.atproto.sync.getLatestCommit
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 cid: String,
18    pub rev: String,
19}
20
21/// Errors a `call()` on this method can return.
22#[derive(Debug, thiserror::Error)]
23pub enum CallError {
24    #[error("RepoNotFound")]
25    RepoNotFound,
26    #[error("RepoTakendown")]
27    RepoTakendown,
28    #[error("RepoSuspended")]
29    RepoSuspended,
30    #[error("RepoDeactivated")]
31    RepoDeactivated,
32    #[error("{0}")]
33    Xrpc(proto_blue_xrpc::XrpcError),
34    #[error(transparent)]
35    Transport(#[from] proto_blue_xrpc::Error),
36    #[error(transparent)]
37    Json(#[from] serde_json::Error),
38}
39
40fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
41    match err.error.as_deref() {
42        Some("RepoNotFound") => CallError::RepoNotFound,
43        Some("RepoTakendown") => CallError::RepoTakendown,
44        Some("RepoSuspended") => CallError::RepoSuspended,
45        Some("RepoDeactivated") => CallError::RepoDeactivated,
46        _ => CallError::Xrpc(err),
47    }
48}
49
50fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
51    let mut qp = proto_blue_xrpc::QueryParams::new();
52    { let v = &p.did; qp.insert("did".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
53    qp
54}
55
56/// Execute the query.
57pub async fn call(
58    client: &proto_blue_xrpc::XrpcClient,
59    params: Option<&Params>,
60    opts: Option<&proto_blue_xrpc::CallOptions>,
61) -> Result<Output, CallError> {
62    let qp = params.map(to_query_params);
63    let response = match client.query("com.atproto.sync.getLatestCommit", qp.as_ref(), opts).await {
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 [`XrpcServer`].
72#[cfg(feature = "server")]
73pub fn register<F, Fut>(
74server: proto_blue_xrpc::XrpcServer,
75handler: 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>> + Send + 'static,
80{
81    let handler = std::sync::Arc::new(handler);
82    server.query("com.atproto.sync.getLatestCommit", move |ctx| {
83        let handler = handler.clone();
84        async move {
85            let params = params_from_ctx(&ctx);
86            let out = handler(ctx, params).await?;
87            let value = serde_json::to_value(&out)
88                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
89            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
90        }
91    })
92}
93
94#[cfg(feature = "server")]
95fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
96    // Always construct a `Params` — required fields are
97    // validated upstream by the lexicon validator when enabled;
98    // missing values surface as runtime errors from the handler.
99    Some(Params {
100        did: (ctx.params.get("did").cloned())?,
101    })
102}
103