composable_tower_http/extract/
extracted.rs

1#[derive(Debug, Clone)]
2pub struct Extracted<T>(pub T);
3
4#[cfg(feature = "axum")]
5mod axum {
6    use axum::{async_trait, extract::FromRequestParts, http::request::Parts, Extension};
7    use http::StatusCode;
8
9    use crate::extract::sealed_extracted::SealedExtracted;
10
11    use super::Extracted;
12
13    #[async_trait]
14    impl<T, S> FromRequestParts<S> for Extracted<T>
15    where
16        T: Clone + Send + Sync + 'static,
17        S: Send + Sync,
18    {
19        type Rejection = StatusCode;
20
21        async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
22            let extracted = Extension::<SealedExtracted<T>>::from_request_parts(parts, state).await;
23
24            match extracted {
25                Ok(Extension(SealedExtracted(extracted))) => Ok(Extracted(extracted)),
26                Err(_) => {
27                    tracing::error!(
28                        "Requested extracted extension was not found. Did you use `Extractor` with `ExtensionLayer`?"
29                    );
30
31                    Err(StatusCode::INTERNAL_SERVER_ERROR)
32                }
33            }
34        }
35    }
36}