use std::fmt::{Debug, Display};
use std::ops::Deref;
pub use bonsaidb_macros::Api;
use serde::{Deserialize, Serialize};
use crate::schema::{Authority, Name, Qualified, QualifiedName};
pub trait Api: Serialize + for<'de> Deserialize<'de> + Send + Sync + Debug + 'static {
type Response: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + Debug;
type Error: ApiError;
fn name() -> ApiName;
}
#[derive(thiserror::Error, Debug, Clone, Serialize, Deserialize)]
#[error("an unreachable error")]
pub enum Infallible {}
pub trait ApiError:
std::fmt::Display + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + Debug
{
}
impl<T> ApiError for T where
T: std::fmt::Display + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + Debug
{
}
pub type ApiResult<Api> = Result<<Api as self::Api>::Response, <Api as self::Api>::Error>;
#[derive(Hash, PartialEq, Eq, Deserialize, Serialize, Debug, Clone, Ord, PartialOrd)]
#[serde(transparent)]
pub struct ApiName(QualifiedName);
impl Qualified for ApiName {
fn new<A: Into<Authority>, N: Into<Name>>(authority: A, name: N) -> Self {
Self(QualifiedName::new(authority, name))
}
}
impl Display for ApiName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
impl Deref for ApiName {
type Target = QualifiedName;
fn deref(&self) -> &Self::Target {
&self.0
}
}