lexa_framework/extract/
cli.rs1use axum::http;
12
13use crate::ApplicationCLIInterface;
14
15pub struct CLI<C>(pub C);
20
21#[derive(Debug)]
26#[derive(thiserror::Error)]
27#[error("\n\t{}: {0}", std::any::type_name::<Self>())]
28pub enum MissingCLIError {
29 Extension(#[from] axum::extract::rejection::ExtensionRejection),
30}
31
32#[async_trait::async_trait]
37impl<S, C> axum::extract::FromRequestParts<S> for CLI<C>
38where
39 C: 'static,
40 C: ApplicationCLIInterface,
41 S: Send + Sync,
42{
43 type Rejection = MissingCLIError;
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(MissingCLIError::Extension)
53 }
54}
55
56impl axum::response::IntoResponse for MissingCLIError {
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}