LCOV - code coverage report
Current view: top level - compute_tools/src/http - mod.rs (source / functions) Coverage Total Hit
Test: 727bdccc1d7d53837da843959afb612f56da4e79.info Lines: 0.0 % 29 0
Test Date: 2025-01-30 15:18:43 Functions: 0.0 % 22 0

            Line data    Source code
       1              : use axum::{body::Body, response::Response};
       2              : use compute_api::responses::{ComputeStatus, GenericAPIError};
       3              : use http::{header::CONTENT_TYPE, StatusCode};
       4              : use serde::Serialize;
       5              : use tracing::error;
       6              : 
       7              : pub use server::launch_http_server;
       8              : 
       9              : mod extract;
      10              : mod routes;
      11              : mod server;
      12              : 
      13              : /// Convenience response builder for JSON responses
      14              : struct JsonResponse;
      15              : 
      16              : impl JsonResponse {
      17              :     /// Helper for actually creating a response
      18            0 :     fn create_response(code: StatusCode, body: impl Serialize) -> Response {
      19            0 :         Response::builder()
      20            0 :             .status(code)
      21            0 :             .header(CONTENT_TYPE.as_str(), "application/json")
      22            0 :             .body(Body::from(serde_json::to_string(&body).unwrap()))
      23            0 :             .unwrap()
      24            0 :     }
      25              : 
      26              :     /// Create a successful error response
      27            0 :     pub(self) fn success(code: StatusCode, body: impl Serialize) -> Response {
      28            0 :         assert!({
      29            0 :             let code = code.as_u16();
      30            0 : 
      31            0 :             (200..300).contains(&code)
      32            0 :         });
      33              : 
      34            0 :         Self::create_response(code, body)
      35            0 :     }
      36              : 
      37              :     /// Create an error response
      38            0 :     pub(self) fn error(code: StatusCode, error: impl ToString) -> Response {
      39            0 :         assert!(code.as_u16() >= 400);
      40              : 
      41            0 :         let message = error.to_string();
      42            0 :         error!(message);
      43              : 
      44            0 :         Self::create_response(code, &GenericAPIError { error: message })
      45            0 :     }
      46              : 
      47              :     /// Create an error response related to the compute being in an invalid state
      48            0 :     pub(self) fn invalid_status(status: ComputeStatus) -> Response {
      49            0 :         Self::create_response(
      50            0 :             StatusCode::PRECONDITION_FAILED,
      51            0 :             &GenericAPIError {
      52            0 :                 error: format!("invalid compute status: {status}"),
      53            0 :             },
      54            0 :         )
      55            0 :     }
      56              : }
        

Generated by: LCOV version 2.1-beta