use axum::http;
use crate::ApplicationCLIInterface;
pub struct CLI<C>(pub C);
#[derive(Debug)]
#[derive(thiserror::Error)]
#[error("\n\t{}: {0}", std::any::type_name::<Self>())]
pub enum MissingCLIError {
Extension(#[from] axum::extract::rejection::ExtensionRejection),
}
#[async_trait::async_trait]
impl<S, C> axum::extract::FromRequestParts<S> for CLI<C>
where
C: 'static,
C: ApplicationCLIInterface,
S: Send + Sync,
{
type Rejection = MissingCLIError;
async fn from_request_parts(
parts: &mut axum::http::request::Parts,
state: &S,
) -> Result<Self, Self::Rejection> {
axum::Extension::<C>::from_request_parts(parts, state)
.await
.map(|axum::Extension(ext)| Self(ext))
.map_err(MissingCLIError::Extension)
}
}
impl axum::response::IntoResponse for MissingCLIError {
fn into_response(self) -> axum::response::Response {
let err_status = http::StatusCode::INTERNAL_SERVER_ERROR;
let err_body = self.to_string();
(err_status, err_body).into_response()
}
}