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