dumbo_web 0.1.0

helper functions for web development
Documentation
use actix_web::HttpResponse;
use serde::Serialize;
use serde_json::json;

/// Constructs a standard success JSON response for dumbo series libraries.
///
/// # Arguments
/// * `data` - The data payload to be serialized in the response body
///
/// # Returns
/// `HttpResponse` with 200 OK status and JSON body containing:
/// - `data`: serialized input data
/// - `success`: boolean true indicating successful operation
pub fn json_response<T: Serialize>(data: T) -> HttpResponse {
    HttpResponse::Ok().json(serde_json::json!({ "data": data, "success": true }))
}

/// Constructs a standard error JSON response for dumbo series libraries.
///
/// Logs the error internally before returning formatted response.
///
/// # Arguments
/// * `err_msg` - Human-readable error description
/// * `e` - Original error object for debugging (logged with Debug format)
/// * `error_code` - Optional numeric error code (defaults to 0 if None)
///
/// # Returns
/// `HttpResponse` with 500 Internal Server Error status and JSON body containing:
/// - `success`: boolean false indicating operation failure
/// - `error`: provided error message string
/// - `errorCode`: numeric error code
pub fn json_error<E: std::fmt::Debug>(err_msg: &str, e: E, error_code: Option<u64>) -> HttpResponse {
    log::error!("{}: {:?}", err_msg, e);
    HttpResponse::InternalServerError().json(json!({
        "success": false,
        "error": err_msg.to_string(),
        "errorCode": error_code.unwrap_or_default()
    }))
}