1use std::future::Future;
2
3use crate::error::Result;
4
5use super::context::{CronContext, FromCronContext};
6
7pub trait CronHandler<Args>: Clone + Send + 'static {
34 fn call(self, ctx: CronContext) -> impl Future<Output = Result<()>> + Send;
36}
37
38impl<F, Fut> CronHandler<()> for F
40where
41 F: FnOnce() -> Fut + Clone + Send + 'static,
42 Fut: Future<Output = Result<()>> + Send,
43{
44 async fn call(self, _ctx: CronContext) -> Result<()> {
45 (self)().await
46 }
47}
48
49macro_rules! impl_cron_handler {
50 ($($T:ident),+) => {
51 impl<F, Fut, $($T),+> CronHandler<($($T,)+)> for F
52 where
53 F: FnOnce($($T),+) -> Fut + Clone + Send + 'static,
54 Fut: Future<Output = Result<()>> + Send,
55 $($T: FromCronContext,)+
56 {
57 #[allow(non_snake_case)]
58 async fn call(self, ctx: CronContext) -> Result<()> {
59 $(let $T = $T::from_cron_context(&ctx)?;)+
60 (self)($($T),+).await
61 }
62 }
63 };
64}
65
66impl_cron_handler!(T1);
67impl_cron_handler!(T1, T2);
68impl_cron_handler!(T1, T2, T3);
69impl_cron_handler!(T1, T2, T3, T4);
70impl_cron_handler!(T1, T2, T3, T4, T5);
71impl_cron_handler!(T1, T2, T3, T4, T5, T6);
72impl_cron_handler!(T1, T2, T3, T4, T5, T6, T7);
73impl_cron_handler!(T1, T2, T3, T4, T5, T6, T7, T8);
74impl_cron_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
75impl_cron_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
76impl_cron_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
77impl_cron_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);