use std::sync::Arc;
use crate::context::InjectionContext;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ContextLevel {
Root,
Current,
}
pub struct ResolveContext {
pub root: Arc<InjectionContext>,
pub current: Arc<InjectionContext>,
}
tokio::task_local! {
pub static RESOLVE_CTX: ResolveContext;
}
pub fn get_di_context(level: ContextLevel) -> Arc<InjectionContext> {
RESOLVE_CTX.with(|ctx| match level {
ContextLevel::Root => Arc::clone(&ctx.root),
ContextLevel::Current => Arc::clone(&ctx.current),
})
}
pub fn try_get_di_context(level: ContextLevel) -> Option<Arc<InjectionContext>> {
RESOLVE_CTX
.try_with(|ctx| match level {
ContextLevel::Root => Arc::clone(&ctx.root),
ContextLevel::Current => Arc::clone(&ctx.current),
})
.ok()
}
#[cfg(test)]
#[path = "resolve_context/tests.rs"]
mod tests;