Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: tools.ozone.set.getValues
3#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
4
5use serde::{Deserialize, Serialize};
6
7/// Get a specific set and its values
8/// XRPC Query: tools.ozone.set.getValues
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Params {
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub cursor: Option<String>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub limit: Option<i64>,
16    pub name: String,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21pub struct Output {
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub cursor: Option<String>,
24    pub set: crate::tools::ozone::set::defs::SetView,
25    pub values: Vec<String>,
26}
27
28/// Errors a `call()` on this method can return.
29#[derive(Debug, thiserror::Error)]
30pub enum CallError {
31    /// set with the given name does not exist
32    #[error("SetNotFound")]
33    SetNotFound,
34    #[error("{0}")]
35    Xrpc(proto_blue_xrpc::XrpcError),
36    #[error(transparent)]
37    Transport(#[from] proto_blue_xrpc::Error),
38    #[error(transparent)]
39    Json(#[from] serde_json::Error),
40}
41
42fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
43    match err.error.as_deref() {
44        Some("SetNotFound") => CallError::SetNotFound,
45        _ => CallError::Xrpc(err),
46    }
47}
48
49fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
50    let mut qp = proto_blue_xrpc::QueryParams::new();
51    if let Some(v) = &p.cursor {
52        qp.insert(
53            "cursor".to_string(),
54            proto_blue_xrpc::QueryValue::String(v.to_string()),
55        );
56    }
57    if let Some(v) = &p.limit {
58        qp.insert(
59            "limit".to_string(),
60            proto_blue_xrpc::QueryValue::Integer(*v),
61        );
62    }
63    {
64        let v = &p.name;
65        qp.insert(
66            "name".to_string(),
67            proto_blue_xrpc::QueryValue::String(v.to_string()),
68        );
69    }
70    qp
71}
72
73/// Execute the query.
74pub async fn call(
75    client: &proto_blue_xrpc::XrpcClient,
76    params: Option<&Params>,
77    opts: Option<&proto_blue_xrpc::CallOptions>,
78) -> Result<Output, CallError> {
79    let qp = params.map(to_query_params);
80    let response = match client
81        .query("tools.ozone.set.getValues", qp.as_ref(), opts)
82        .await
83    {
84        Ok(r) => r,
85        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
86        Err(e) => return Err(CallError::Transport(e)),
87    };
88    Ok(serde_json::from_value(response.data)?)
89}
90
91/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
92#[cfg(feature = "server")]
93pub fn register<F, Fut>(
94    server: proto_blue_xrpc::XrpcServer,
95    handler: F,
96) -> proto_blue_xrpc::XrpcServer
97where
98    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
99    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
100        + Send
101        + 'static,
102{
103    let handler = std::sync::Arc::new(handler);
104    server.query("tools.ozone.set.getValues", move |ctx| {
105        let handler = handler.clone();
106        async move {
107            let params = params_from_ctx(&ctx);
108            let out = handler(ctx, params).await?;
109            let value = serde_json::to_value(&out).map_err(|e| {
110                proto_blue_xrpc::XrpcServerError::new(
111                    proto_blue_xrpc::ResponseType::InternalServerError,
112                    format!("output serialize: {e}"),
113                )
114            })?;
115            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
116        }
117    })
118}
119
120#[cfg(feature = "server")]
121fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
122    // Always construct a `Params` — required fields are
123    // validated upstream by the lexicon validator when enabled;
124    // missing values surface as runtime errors from the handler.
125    Some(Params {
126        cursor: ctx.params.get("cursor").cloned(),
127        limit: ctx.params.get("limit").and_then(|v| v.parse::<i64>().ok()),
128        name: (ctx.params.get("name").cloned())?,
129    })
130}