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