rok-core 0.6.0

Core primitives for the rok ecosystem — errors, crypto, i18n, config, DI, and more
Documentation
/// Dump value(s) to stderr and halt by returning an error HTTP response.
///
/// Requires `debug` + `axum` features on `rok-core`.
#[macro_export]
macro_rules! rok_dd {
    ($($val:expr),* $(,)?) => {{
        eprintln!("┌─────────────────────────────────────────────────────────────");
        eprintln!("{}:{}  rok_dd!", file!(), line!());
        eprintln!("├─────────────────────────────────────────────────────────────");
        $(
            eprintln!("{:?}", $val);
        )*
        eprintln!("└─────────────────────────────────────────────────────────────");
        return $crate::debug::__rok_dd_response(file!(), line!());
    }};
}

/// Dump value(s) to stderr without halting execution.
#[macro_export]
macro_rules! rok_dump {
    ($($val:expr),* $(,)?) => {{
        eprintln!("┌─────────────────────────────────────────────────────────────");
        eprintln!("{}:{}  rok_dump!", file!(), line!());
        eprintln!("├─────────────────────────────────────────────────────────────");
        $(
            eprintln!("{:?}", $val);
        )*
        eprintln!("└─────────────────────────────────────────────────────────────");
    }};
}

#[doc(hidden)]
#[cfg(feature = "axum")]
pub fn __rok_dd_response(file: &str, line: u32) -> axum::response::Response {
    use axum::response::IntoResponse;
    (
        axum::http::StatusCode::INTERNAL_SERVER_ERROR,
        axum::Json(serde_json::json!({
            "error": "E_DEBUG_DUMP",
            "message": "Request halted by rok_dd!()",
            "file": file,
            "line": line,
        })),
    )
        .into_response()
}

#[cfg(test)]
mod tests {
    #[test]
    fn rok_dump_compiles() {
        let x = 42;
        rok_dump!(x, "hello");
    }
}

#[cfg(all(test, feature = "axum"))]
mod axum_tests {
    use axum::response::Response;

    /// Verify `rok_dd!` type-checks in an async fn returning `Response`.
    #[allow(dead_code)]
    async fn handler() -> Response {
        rok_dd!("test");
    }
}