proto-blue-api 0.2.2

AT Protocol high-level API: agent, rich text, moderation, generated types
Documentation
// Generated by atproto-codegen. Do not edit.
//! Lexicon: tools.ozone.verification.listVerifications

use serde::{Deserialize, Serialize};

/// List verifications
/// XRPC Query: tools.ozone.verification.listVerifications
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Params {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_after: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_before: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_revoked: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub issuers: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort_direction: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subjects: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Output {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
    pub verifications: Vec<crate::tools::ozone::verification::defs::VerificationView>,
}

/// Errors a `call()` on this method can return.
#[derive(Debug, thiserror::Error)]
pub enum CallError {
    #[error("{0}")]
    Xrpc(proto_blue_xrpc::XrpcError),
    #[error(transparent)]
    Transport(#[from] proto_blue_xrpc::Error),
    #[error(transparent)]
    Json(#[from] serde_json::Error),
}

fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
    CallError::Xrpc(err)
}

fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
    let mut qp = proto_blue_xrpc::QueryParams::new();
    if let Some(v) = &p.created_after { qp.insert("createdAfter".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
    if let Some(v) = &p.created_before { qp.insert("createdBefore".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
    if let Some(v) = &p.cursor { qp.insert("cursor".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
    if let Some(v) = &p.is_revoked { qp.insert("isRevoked".to_string(), proto_blue_xrpc::QueryValue::Boolean(*v)); }
    if let Some(v) = &p.issuers { qp.insert("issuers".to_string(), proto_blue_xrpc::QueryValue::Array(v.iter().map(|x| proto_blue_xrpc::QueryValue::String(x.clone())).collect())); }
    if let Some(v) = &p.limit { qp.insert("limit".to_string(), proto_blue_xrpc::QueryValue::Integer(*v)); }
    if let Some(v) = &p.sort_direction { qp.insert("sortDirection".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
    if let Some(v) = &p.subjects { qp.insert("subjects".to_string(), proto_blue_xrpc::QueryValue::Array(v.iter().map(|x| proto_blue_xrpc::QueryValue::String(x.clone())).collect())); }
    qp
}

/// Execute the query.
pub async fn call(
    client: &proto_blue_xrpc::XrpcClient,
    params: Option<&Params>,
    opts: Option<&proto_blue_xrpc::CallOptions>,
) -> Result<Output, CallError> {
    let qp = params.map(to_query_params);
    let response = match client.query("tools.ozone.verification.listVerifications", qp.as_ref(), opts).await {
        Ok(r) => r,
        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
        Err(e) => return Err(CallError::Transport(e)),
    };
    Ok(serde_json::from_value(response.data)?)
}

/// Register a typed handler for this method on an [`XrpcServer`].
#[cfg(feature = "server")]
pub fn register<F, Fut>(
server: proto_blue_xrpc::XrpcServer,
handler: F,
) -> proto_blue_xrpc::XrpcServer
where
    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
{
    let handler = std::sync::Arc::new(handler);
    server.query("tools.ozone.verification.listVerifications", move |ctx| {
        let handler = handler.clone();
        async move {
            let params = params_from_ctx(&ctx);
            let out = handler(ctx, params).await?;
            let value = serde_json::to_value(&out)
                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
        }
    })
}

#[cfg(feature = "server")]
fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
    // Always construct a `Params` — required fields are
    // validated upstream by the lexicon validator when enabled;
    // missing values surface as runtime errors from the handler.
    Some(Params {
        created_after: ctx.params.get("createdAfter").cloned(),
        created_before: ctx.params.get("createdBefore").cloned(),
        cursor: ctx.params.get("cursor").cloned(),
        is_revoked: ctx.params.get("isRevoked").and_then(|v| v.parse::<bool>().ok()),
        issuers: Some(ctx.params.get("issuers").map(|v| v.split(',').map(String::from).collect::<Vec<_>>()).unwrap_or_default()),
        limit: ctx.params.get("limit").and_then(|v| v.parse::<i64>().ok()),
        sort_direction: ctx.params.get("sortDirection").cloned(),
        subjects: Some(ctx.params.get("subjects").map(|v| v.split(',').map(String::from).collect::<Vec<_>>()).unwrap_or_default()),
    })
}