use std::fmt::Debug;
use std::marker::PhantomData;
use async_trait::async_trait;
use bonsaidb_core::api::{self, Api, ApiError, Infallible};
use bonsaidb_core::arc_bytes::serde::Bytes;
use bonsaidb_core::permissions::PermissionDenied;
use bonsaidb_core::schema::{InsertError, InvalidNameError};
use crate::{Backend, ConnectedClient, CustomServer, Error, NoBackend};
#[async_trait]
pub trait Handler<Api: api::Api, B: Backend = NoBackend>: Send + Sync {
async fn handle(session: HandlerSession<'_, B>, request: Api) -> HandlerResult<Api>;
}
pub struct HandlerSession<'a, B: Backend = NoBackend> {
pub server: &'a CustomServer<B>,
pub as_client: CustomServer<B>,
pub client: &'a ConnectedClient<B>,
}
#[async_trait]
pub(crate) trait AnyHandler<B: Backend>: Send + Sync + Debug {
async fn handle(&self, session: HandlerSession<'_, B>, request: &[u8]) -> Result<Bytes, Error>;
}
pub(crate) struct AnyWrapper<D: Handler<A, B>, B: Backend, A: Api>(
pub(crate) PhantomData<(D, B, A)>,
);
impl<D, B, A> Debug for AnyWrapper<D, B, A>
where
D: Handler<A, B>,
B: Backend,
A: Api,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("AnyWrapper").finish()
}
}
#[async_trait]
impl<T, B, A> AnyHandler<B> for AnyWrapper<T, B, A>
where
B: Backend,
T: Handler<A, B>,
A: Api,
{
async fn handle(&self, client: HandlerSession<'_, B>, request: &[u8]) -> Result<Bytes, Error> {
let request = pot::from_slice(request)?;
let response = match T::handle(client, request).await {
Ok(response) => Ok(response),
Err(HandlerError::Api(err)) => Err(err),
Err(HandlerError::Server(err)) => return Err(err),
};
Ok(Bytes::from(pot::to_vec(&response)?))
}
}
#[derive(thiserror::Error, Debug)]
pub enum HandlerError<E: ApiError = Infallible> {
#[error("api error: {0}")]
Api(E),
#[error("server error: {0}")]
Server(#[from] Error),
}
impl<E: ApiError> From<PermissionDenied> for HandlerError<E> {
fn from(permission_denied: PermissionDenied) -> Self {
Self::Server(Error::from(permission_denied))
}
}
impl<E: ApiError> From<bonsaidb_core::Error> for HandlerError<E> {
fn from(err: bonsaidb_core::Error) -> Self {
Self::Server(Error::from(err))
}
}
impl<E: ApiError> From<bonsaidb_local::Error> for HandlerError<E> {
fn from(err: bonsaidb_local::Error) -> Self {
Self::Server(Error::from(err))
}
}
impl<E: ApiError> From<InvalidNameError> for HandlerError<E> {
fn from(err: InvalidNameError) -> Self {
Self::Server(Error::from(err))
}
}
#[cfg(feature = "websockets")]
impl<E: ApiError> From<bincode::Error> for HandlerError<E> {
fn from(other: bincode::Error) -> Self {
Self::Server(Error::from(bonsaidb_local::Error::from(other)))
}
}
impl<E: ApiError> From<pot::Error> for HandlerError<E> {
fn from(other: pot::Error) -> Self {
Self::Server(Error::from(other))
}
}
impl<E: ApiError> From<std::io::Error> for HandlerError<E> {
fn from(err: std::io::Error) -> Self {
Self::Server(Error::from(err))
}
}
impl<T, E> From<InsertError<T>> for HandlerError<E>
where
E: ApiError,
{
fn from(error: InsertError<T>) -> Self {
Self::Server(Error::from(error.error))
}
}
pub type HandlerResult<Api> =
Result<<Api as api::Api>::Response, HandlerError<<Api as api::Api>::Error>>;