LCOV - code coverage report
Current view: top level - compute_tools/src/http/extract - path.rs (source / functions) Coverage Total Hit
Test: 07bee600374ccd486c69370d0972d9035964fe68.info Lines: 0.0 % 16 0
Test Date: 2025-02-20 13:11:02 Functions: 0.0 % 4 0

            Line data    Source code
       1              : use std::ops::{Deref, DerefMut};
       2              : 
       3              : use axum::extract::{rejection::PathRejection, FromRequestParts};
       4              : use compute_api::responses::GenericAPIError;
       5              : use http::{request::Parts, StatusCode};
       6              : 
       7              : /// Custom `Path` extractor, so that we can format errors into
       8              : /// `JsonResponse<GenericAPIError>`.
       9              : #[derive(Debug, Clone, Copy, Default)]
      10              : pub(crate) struct Path<T>(pub T);
      11              : 
      12              : impl<S, T> FromRequestParts<S> for Path<T>
      13              : where
      14              :     axum::extract::Path<T>: FromRequestParts<S, Rejection = PathRejection>,
      15              :     S: Send + Sync,
      16              : {
      17              :     type Rejection = (StatusCode, axum::Json<GenericAPIError>);
      18              : 
      19            0 :     async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
      20            0 :         match axum::extract::Path::<T>::from_request_parts(parts, state).await {
      21            0 :             Ok(value) => Ok(Self(value.0)),
      22            0 :             Err(rejection) => Err((
      23            0 :                 rejection.status(),
      24            0 :                 axum::Json(GenericAPIError {
      25            0 :                     error: rejection.body_text().to_ascii_lowercase(),
      26            0 :                 }),
      27            0 :             )),
      28              :         }
      29            0 :     }
      30              : }
      31              : 
      32              : impl<T> Deref for Path<T> {
      33              :     type Target = T;
      34              : 
      35            0 :     fn deref(&self) -> &Self::Target {
      36            0 :         &self.0
      37            0 :     }
      38              : }
      39              : 
      40              : impl<T> DerefMut for Path<T> {
      41            0 :     fn deref_mut(&mut self) -> &mut Self::Target {
      42            0 :         &mut self.0
      43            0 :     }
      44              : }
        

Generated by: LCOV version 2.1-beta