lexa_framework/extract/
env.rs1use axum::http;
12
13use crate::ApplicationEnvInterface;
14
15pub struct Env<E>(pub E);
20
21#[derive(Debug)]
26#[derive(thiserror::Error)]
27pub enum MissingEnvError {
28 #[error("\n\t{}: {0}", std::any::type_name::<Self>())]
29 Extension(#[from] axum::extract::rejection::ExtensionRejection),
30}
31
32#[async_trait::async_trait]
37impl<S, C> axum::extract::FromRequestParts<S> for Env<C>
38where
39 C: 'static,
40 C: ApplicationEnvInterface,
41 S: Send + Sync,
42{
43 type Rejection = MissingEnvError;
44
45 async fn from_request_parts(
46 parts: &mut axum::http::request::Parts,
47 state: &S,
48 ) -> Result<Self, Self::Rejection> {
49 axum::Extension::<C>::from_request_parts(parts, state)
50 .await
51 .map(|axum::Extension(ext)| Self(ext))
52 .map_err(MissingEnvError::Extension)
53 }
54}
55
56impl axum::response::IntoResponse for MissingEnvError {
57 fn into_response(self) -> axum::response::Response {
58 let err_status = http::StatusCode::INTERNAL_SERVER_ERROR;
59 let err_body = self.to_string();
60 (err_status, err_body).into_response()
61 }
62}