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 { qp.insert("cursor".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
51    if let Some(v) = &p.limit { qp.insert("limit".to_string(), proto_blue_xrpc::QueryValue::Integer(*v)); }
52    { let v = &p.name; qp.insert("name".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
53    qp
54}
55
56/// Execute the query.
57pub async fn call(
58    client: &proto_blue_xrpc::XrpcClient,
59    params: Option<&Params>,
60    opts: Option<&proto_blue_xrpc::CallOptions>,
61) -> Result<Output, CallError> {
62    let qp = params.map(to_query_params);
63    let response = match client.query("tools.ozone.set.getValues", qp.as_ref(), opts).await {
64        Ok(r) => r,
65        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
66        Err(e) => return Err(CallError::Transport(e)),
67    };
68    Ok(serde_json::from_value(response.data)?)
69}
70
71/// Register a typed handler for this method on an [`XrpcServer`].
72#[cfg(feature = "server")]
73pub fn register<F, Fut>(
74server: proto_blue_xrpc::XrpcServer,
75handler: F,
76) -> proto_blue_xrpc::XrpcServer
77where
78    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
79    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
80{
81    let handler = std::sync::Arc::new(handler);
82    server.query("tools.ozone.set.getValues", move |ctx| {
83        let handler = handler.clone();
84        async move {
85            let params = params_from_ctx(&ctx);
86            let out = handler(ctx, params).await?;
87            let value = serde_json::to_value(&out)
88                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
89            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
90        }
91    })
92}
93
94#[cfg(feature = "server")]
95fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
96    // Always construct a `Params` — required fields are
97    // validated upstream by the lexicon validator when enabled;
98    // missing values surface as runtime errors from the handler.
99    Some(Params {
100        cursor: ctx.params.get("cursor").cloned(),
101        limit: ctx.params.get("limit").and_then(|v| v.parse::<i64>().ok()),
102        name: (ctx.params.get("name").cloned())?,
103    })
104}
105