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    {
53        let v = &p.did;
54        qp.insert(
55            "did".to_string(),
56            proto_blue_xrpc::QueryValue::String(v.clone()),
57        );
58    }
59    qp
60}
61
62/// Execute the query.
63pub async fn call(
64    client: &proto_blue_xrpc::XrpcClient,
65    params: Option<&Params>,
66    opts: Option<&proto_blue_xrpc::CallOptions>,
67) -> Result<Output, CallError> {
68    let qp = params.map(to_query_params);
69    let response = match client
70        .query("com.atproto.sync.getLatestCommit", qp.as_ref(), opts)
71        .await
72    {
73        Ok(r) => r,
74        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
75        Err(e) => return Err(CallError::Transport(e)),
76    };
77    Ok(serde_json::from_value(response.data)?)
78}
79
80/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
81#[cfg(feature = "server")]
82pub fn register<F, Fut>(
83    server: proto_blue_xrpc::XrpcServer,
84    handler: F,
85) -> proto_blue_xrpc::XrpcServer
86where
87    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
88    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
89        + Send
90        + 'static,
91{
92    let handler = std::sync::Arc::new(handler);
93    server.query("com.atproto.sync.getLatestCommit", move |ctx| {
94        let handler = handler.clone();
95        async move {
96            let params = params_from_ctx(&ctx);
97            let out = handler(ctx, params).await?;
98            let value = serde_json::to_value(&out).map_err(|e| {
99                proto_blue_xrpc::XrpcServerError::new(
100                    proto_blue_xrpc::ResponseType::InternalServerError,
101                    format!("output serialize: {e}"),
102                )
103            })?;
104            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
105        }
106    })
107}
108
109#[cfg(feature = "server")]
110fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
111    // Always construct a `Params` — required fields are
112    // validated upstream by the lexicon validator when enabled;
113    // missing values surface as runtime errors from the handler.
114    Some(Params {
115        did: (ctx.params.get("did").cloned())?,
116    })
117}