Skip to main content

proto_blue_api/generated/app/bsky/bookmark/
createBookmark.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: app.bsky.bookmark.createBookmark
3
4use serde::{Deserialize, Serialize};
5
6/// Creates a private bookmark for the specified record. Currently, only `app.bsky.feed.post` records are supported. Requires authentication.
7/// XRPC Procedure: app.bsky.bookmark.createBookmark
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Input {
11    pub cid: String,
12    pub uri: String,
13}
14
15/// Errors a `call()` on this method can return.
16#[derive(Debug, thiserror::Error)]
17pub enum CallError {
18    /// The URI to be bookmarked is for an unsupported collection.
19    #[error("UnsupportedCollection")]
20    UnsupportedCollection,
21    #[error("{0}")]
22    Xrpc(proto_blue_xrpc::XrpcError),
23    #[error(transparent)]
24    Transport(#[from] proto_blue_xrpc::Error),
25    #[error(transparent)]
26    Json(#[from] serde_json::Error),
27}
28
29fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
30    match err.error.as_deref() {
31        Some("UnsupportedCollection") => CallError::UnsupportedCollection,
32        _ => CallError::Xrpc(err),
33    }
34}
35
36/// Execute the procedure.
37pub async fn call(
38    client: &proto_blue_xrpc::XrpcClient,
39    input: &Input,
40    opts: Option<&proto_blue_xrpc::CallOptions>,
41) -> Result<serde_json::Value, CallError> {
42    let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
43    let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
44    let response = match client.procedure("app.bsky.bookmark.createBookmark", qp_ref, Some(body), opts).await {
45        Ok(r) => r,
46        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
47        Err(e) => return Err(CallError::Transport(e)),
48    };
49    Ok(response.data)
50}
51
52/// Register a typed handler for this procedure on an [`XrpcServer`].
53#[cfg(feature = "server")]
54pub fn register<F, Fut>(
55server: proto_blue_xrpc::XrpcServer,
56handler: F,
57) -> proto_blue_xrpc::XrpcServer
58where
59    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
60    Fut: std::future::Future<Output = Result<serde_json::Value, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
61{
62    let handler = std::sync::Arc::new(handler);
63    server.procedure("app.bsky.bookmark.createBookmark", move |ctx| {
64        let handler = handler.clone();
65        async move {
66            let input = match ctx.json_body()? {
67                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InvalidRequest, format!("input deserialize: {e}")))?),
68                None => None,
69            };
70            let out = handler(ctx, input).await?;
71            Ok::<_, proto_blue_xrpc::XrpcServerError>(out)
72        }
73    })
74}
75