modo/extractor/
service.rs1use std::sync::Arc;
2
3use axum::extract::{FromRef, FromRequestParts};
4use http::request::Parts;
5
6use crate::service::AppState;
7
8pub struct Service<T>(pub Arc<T>);
25
26impl<S, T> FromRequestParts<S> for Service<T>
27where
28 S: Send + Sync,
29 T: Send + Sync + 'static,
30 AppState: FromRef<S>,
31{
32 type Rejection = crate::error::Error;
33
34 async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
35 let app_state = AppState::from_ref(state);
36
37 app_state.get::<T>().map(Service).ok_or_else(|| {
38 crate::error::Error::internal(format!(
39 "service not found in registry: {}",
40 std::any::type_name::<T>()
41 ))
42 })
43 }
44}