use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Params {
pub dids: Vec<proto_blue_syntax::Did>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "$type")]
pub enum OutputReposItemRefs {
#[serde(rename = "tools.ozone.moderation.defs#repoViewDetail")]
OzoneModerationDefsRepoViewDetail(Box<crate::tools::ozone::moderation::defs::RepoViewDetail>),
#[serde(rename = "tools.ozone.moderation.defs#repoViewNotFound")]
OzoneModerationDefsRepoViewNotFound(
Box<crate::tools::ozone::moderation::defs::RepoViewNotFound>,
),
#[serde(other)]
Other,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Output {
pub repos: Vec<OutputReposItemRefs>,
}
#[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();
{
let v = &p.dids;
qp.insert(
"dids".to_string(),
proto_blue_xrpc::QueryValue::Array(
v.iter()
.map(|x| proto_blue_xrpc::QueryValue::String(x.to_string()))
.collect(),
),
);
}
qp
}
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.moderation.getRepos", 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)?)
}
#[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.moderation.getRepos", 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> {
Some(Params {
dids: (ctx.params.get("dids").and_then(|v| {
v.split(',')
.map(proto_blue_syntax::Did::new)
.collect::<Result<Vec<_>, _>>()
.ok()
}))?,
})
}