by_loco/controller/extractor/
shared_store.rs

1use crate::{app::AppContext, Error};
2use axum::{extract::FromRequestParts, http::request::Parts};
3use std::any::Any;
4
5/// An extractor that streamlines the process of getting static Data from the `DiContainer`.
6pub struct SharedStore<T>(pub T);
7
8impl<T> FromRequestParts<AppContext> for SharedStore<T>
9where
10    T: Any + Clone + Send + Sync + 'static,
11{
12    type Rejection = Error;
13
14    async fn from_request_parts(
15        _: &mut Parts,
16        state: &AppContext,
17    ) -> Result<Self, Self::Rejection> {
18        let instance = state.shared_store.get::<T>().ok_or_else(|| {
19            let type_name = std::any::type_name::<T>();
20            tracing::error!(
21                "Could not find service of type `{}` in shared store",
22                type_name
23            );
24            Error::InternalServerError
25        })?;
26
27        Ok(Self(instance))
28    }
29}