LCOV - code coverage report
Current view: top level - compute_tools/src/http/extract - json.rs (source / functions) Coverage Total Hit
Test: f2bfe5dc5ab550768e936d6bc7b94d9b2e2d4cc9.info Lines: 0.0 % 16 0
Test Date: 2025-01-27 20:39:28 Functions: 0.0 % 13 0

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

Generated by: LCOV version 2.1-beta