zen_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>(&'a self, key: &'a str) -> impl Future<Output = LoaderResponse> + 'a {
30        async move {
31            let closure = &self.closure;
32            closure(key.to_string()).await
33        }
34    }
35}