Skip to main content

proto_blue_api/generated/tools/ozone/set/
upsertSet.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: tools.ozone.set.upsertSet
3
4/// Create or update set metadata
5/// XRPC Procedure: tools.ozone.set.upsertSet
6pub type Input = crate::tools::ozone::set::defs::Set;
7
8pub type Output = crate::tools::ozone::set::defs::SetView;
9
10/// Errors a `call()` on this method can return.
11#[derive(Debug, thiserror::Error)]
12pub enum CallError {
13    #[error("{0}")]
14    Xrpc(proto_blue_xrpc::XrpcError),
15    #[error(transparent)]
16    Transport(#[from] proto_blue_xrpc::Error),
17    #[error(transparent)]
18    Json(#[from] serde_json::Error),
19}
20
21fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
22    CallError::Xrpc(err)
23}
24
25/// Execute the procedure.
26pub async fn call(
27    client: &proto_blue_xrpc::XrpcClient,
28    input: &Input,
29    opts: Option<&proto_blue_xrpc::CallOptions>,
30) -> Result<Output, CallError> {
31    let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
32    let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
33    let response = match client
34        .procedure("tools.ozone.set.upsertSet", qp_ref, Some(body), opts)
35        .await
36    {
37        Ok(r) => r,
38        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
39        Err(e) => return Err(CallError::Transport(e)),
40    };
41    Ok(serde_json::from_value(response.data)?)
42}
43
44/// Register a typed handler for this procedure on an [`proto_blue_xrpc::XrpcServer`].
45#[cfg(feature = "server")]
46pub fn register<F, Fut>(
47    server: proto_blue_xrpc::XrpcServer,
48    handler: F,
49) -> proto_blue_xrpc::XrpcServer
50where
51    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
52    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
53        + Send
54        + 'static,
55{
56    let handler = std::sync::Arc::new(handler);
57    server.procedure("tools.ozone.set.upsertSet", move |ctx| {
58        let handler = handler.clone();
59        async move {
60            let input = match ctx.json_body()? {
61                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| {
62                    proto_blue_xrpc::XrpcServerError::new(
63                        proto_blue_xrpc::ResponseType::InvalidRequest,
64                        format!("input deserialize: {e}"),
65                    )
66                })?),
67                None => None,
68            };
69            let out = handler(ctx, input).await?;
70            let value = serde_json::to_value(&out).map_err(|e| {
71                proto_blue_xrpc::XrpcServerError::new(
72                    proto_blue_xrpc::ResponseType::InternalServerError,
73                    format!("output serialize: {e}"),
74                )
75            })?;
76            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
77        }
78    })
79}