moduforge_rules_engine/loader/
closure.rs

1use std::future::Future;
2
3use crate::loader::{DecisionLoader, LoaderResponse};
4
5/// Loads decisions using an async closure
6#[derive(Debug)]
7pub struct ClosureLoader<F>
8where
9    F: Sync + Send,
10{
11    closure: F,
12}
13
14impl<F, O> ClosureLoader<F>
15where
16    F: Fn(String) -> O + Sync + Send,
17    O: Future<Output = LoaderResponse> + Send,
18{
19    pub fn new(closure: F) -> Self {
20        Self { closure }
21    }
22}
23
24impl<F, O> DecisionLoader for ClosureLoader<F>
25where
26    F: Fn(String) -> O + Sync + Send,
27    O: Future<Output = LoaderResponse> + Send,
28{
29    fn load<'a>(
30        &'a self,
31        key: &'a str,
32    ) -> impl Future<Output = LoaderResponse> + 'a {
33        async move {
34            let closure = &self.closure;
35            closure(key.to_string()).await
36        }
37    }
38}