use std::future::Future;
use std::pin::Pin;
use axum_core::body::Body;
use http::{Request, StatusCode};
#[derive(Debug)]
#[non_exhaustive]
pub enum ProtectedRequestOutcome {
Continue,
GrantAccess,
Abort {
status: StatusCode,
body: Option<String>,
},
}
pub trait PaygateHooks: Send + Sync {
fn on_protected_request<'a>(
&'a self,
_req: &'a Request<Body>,
) -> impl Future<Output = ProtectedRequestOutcome> + Send + 'a {
async { ProtectedRequestOutcome::Continue }
}
fn on_payment_verified<'a>(
&'a self,
_req: &'a mut Request<Body>,
) -> impl Future<Output = ()> + Send + 'a {
async {}
}
}
pub trait DynPaygateHooks: Send + Sync {
fn on_protected_request<'a>(
&'a self,
req: &'a Request<Body>,
) -> Pin<Box<dyn Future<Output = ProtectedRequestOutcome> + Send + 'a>>;
fn on_payment_verified<'a>(
&'a self,
req: &'a mut Request<Body>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
}
impl<T> DynPaygateHooks for T
where
T: PaygateHooks + ?Sized,
{
fn on_protected_request<'a>(
&'a self,
req: &'a Request<Body>,
) -> Pin<Box<dyn Future<Output = ProtectedRequestOutcome> + Send + 'a>> {
Box::pin(<T as PaygateHooks>::on_protected_request(self, req))
}
fn on_payment_verified<'a>(
&'a self,
req: &'a mut Request<Body>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
Box::pin(<T as PaygateHooks>::on_payment_verified(self, req))
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct NoopPaygateHooks;
impl PaygateHooks for NoopPaygateHooks {}