fusen_common/handler/
mod.rs1use std::{fmt::Debug, sync::Arc};
2
3type HandlerLink<T, E> = Arc<Vec<Arc<Box<dyn ChannelHandler_<T, E>>>>>;
4
5pub struct ProceedingJoinPoint<T, E>
6where
7 E: std::error::Error,
8{
9 index: usize,
10 link: HandlerLink<T, E>,
11 pub context: T,
12}
13
14impl<T, E> ProceedingJoinPoint<T, E>
15where
16 E: std::error::Error,
17{
18 pub fn new(link: HandlerLink<T, E>, context: T) -> Self {
19 Self {
20 index: 0,
21 link,
22 context,
23 }
24 }
25 pub fn into_context(self) -> T {
26 self.context
27 }
28 pub async fn proceed(mut self) -> Result<T, E> {
29 match self.link.get(self.index) {
30 Some(filter) => {
31 self.index += 1;
32 filter.clone().call(self).await
33 }
34 None => Ok(self.into_context()),
35 }
36 }
37}
38
39pub type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
40
41pub trait ChannelHandler_<T, E>: Send + Sync + Debug
42where
43 E: std::error::Error,
44{
45 fn call<'a>(&'a self, join_point: ProceedingJoinPoint<T, E>) -> BoxFuture<'a, Result<T, E>>;
46}
47
48#[allow(async_fn_in_trait)]
49pub trait ChannelHandler<T, E>
50where
51 E: std::error::Error,
52{
53 async fn aroud(&self, join_point: ProceedingJoinPoint<T, E>) -> Result<T, E>;
54}
55
56#[derive(Debug)]
57pub enum HandlerInvoker<T, E> {
58 ChannelHandler(Box<dyn ChannelHandler_<T, E>>),
59}
60
61#[derive(Debug)]
62pub struct Handler<T, E> {
63 pub id: String,
64 pub handler_invoker: HandlerInvoker<T, E>,
65}
66
67pub trait HandlerLoad {
68 fn load<T, E>(self) -> Handler<T, E>;
69}