use anyhow::{anyhow, Result};
use async_trait::async_trait;
use crate::tenant::TenantContext;
#[macro_export]
macro_rules! custom_methods {
($($method:literal),*) => {
$(
paste::paste! {
async fn [<$method:lower>](&self, _ctx: &TenantContext, _data: R, _params: P) -> Result<R> {
Err(anyhow!(concat!("Custom method not implemented: ", $method)))
}
}
)*
};
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ServiceMethodKind {
Find,
Get,
Create,
Update,
Patch,
Remove,
Custom(&'static str),
}
#[derive(Debug, Clone)]
pub struct ServiceCapabilities {
pub allowed_methods: Vec<ServiceMethodKind>,
}
impl ServiceCapabilities {
pub fn standard_crud() -> Self {
use ServiceMethodKind::*;
Self {
allowed_methods: vec![Find, Get, Create, Update, Patch, Remove],
}
}
pub fn minimal() -> Self {
use ServiceMethodKind::*;
Self {
allowed_methods: vec![Find, Create],
}
}
pub fn from_methods(methods: Vec<ServiceMethodKind>) -> Self {
Self {
allowed_methods: methods,
}
}
}
#[async_trait]
pub trait DogService<R, P = ()>: Send + Sync
where
R: Send + 'static,
P: Send + 'static,
{
fn capabilities(&self) -> ServiceCapabilities {
ServiceCapabilities::standard_crud()
}
async fn find(&self, _ctx: &TenantContext, _params: P) -> Result<Vec<R>> {
Err(anyhow!("Method not implemented: find"))
}
async fn get(&self, _ctx: &TenantContext, _id: &str, _params: P) -> Result<R> {
Err(anyhow!("Method not implemented: get"))
}
async fn create(&self, _ctx: &TenantContext, _data: R, _params: P) -> Result<R> {
Err(anyhow!("Method not implemented: create"))
}
async fn update(&self, _ctx: &TenantContext, _id: &str, _data: R, _params: P) -> Result<R> {
Err(anyhow!("Method not implemented: update"))
}
async fn patch(
&self,
_ctx: &TenantContext,
_id: Option<&str>,
_data: R,
_params: P,
) -> Result<R> {
Err(anyhow!("Method not implemented: patch"))
}
async fn remove(&self, _ctx: &TenantContext, _id: Option<&str>, _params: P) -> Result<R> {
Err(anyhow!("Method not implemented: remove"))
}
async fn custom(
&self,
_ctx: &TenantContext,
_method: &str,
_data: Option<R>,
_params: P,
) -> Result<R> {
Err(anyhow!("Custom methods not implemented by this service"))
}
}