Skip to main content

zen_engine/loader/
mod.rs

1use downcast_rs::{impl_downcast, DowncastSync};
2use std::fmt::Debug;
3use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6use thiserror::Error;
7
8pub use cached::CachedLoader;
9pub use closure::ClosureLoader;
10pub use config::LoaderConfig;
11pub use filesystem::{FilesystemLoader, FilesystemLoaderOptions};
12pub use memory::MemoryLoader;
13pub use noop::NoopLoader;
14
15use crate::model::DecisionContent;
16
17mod cached;
18mod closure;
19mod config;
20mod filesystem;
21mod memory;
22mod noop;
23
24pub type DynamicLoader = Arc<dyn DecisionLoader>;
25
26pub type LoaderResult<T> = Result<T, LoaderError>;
27pub type LoaderResponse = LoaderResult<Arc<DecisionContent>>;
28
29/// Trait used for implementing a loader for decisions
30pub trait DecisionLoader: Debug + Send + Sync + DowncastSync {
31    fn load<'a>(
32        &'a self,
33        key: &'a str,
34    ) -> Pin<Box<dyn Future<Output = LoaderResponse> + 'a + Send>>;
35
36    fn keys(&self) -> Option<Vec<Arc<str>>> {
37        None
38    }
39
40    fn load_sync(&self, _key: &str) -> Option<LoaderResponse> {
41        None
42    }
43}
44
45impl_downcast!(sync DecisionLoader);
46
47#[derive(Error, Debug)]
48pub enum LoaderError {
49    #[error("Loader did not find item with key {0}")]
50    NotFound(String),
51    #[error("Loader failed internally on key {key}: {source}.")]
52    Internal {
53        key: String,
54        #[source]
55        source: anyhow::Error,
56    },
57}