Skip to main content

mina_node_native/http_server/
macros.rs

1//! Macros for HTTP handlers.
2//!
3//! These macros simplify the common pattern of sending an RPC request and
4//! converting the response to an HTTP response.
5//!
6//! # Type flow
7//!
8//! ```text
9//! oneshot_request(req)  -> Option<T>
10//! rpc_request!(...)     -> AppResult<T>      (= Result<T, AppError>)
11//! jsonify_rpc!(...)     -> AppResult<Json<T>> (= Result<Json<T>, AppError>)
12//! ```
13//!
14//! The `Option<T>` from `oneshot_request` represents channel status:
15//! - `Some(T)` - response received
16//! - `None` - channel dropped (converted to `AppError::ChannelDropped`)
17//!
18//! Note: `T` is often itself an `Option<U>`, where `None` means "no data
19//! available" (serialized as `null`). This is distinct from channel failure.
20
21/// Sends an RPC request, returning an error if the channel was dropped.
22///
23/// Returns `Result<T, AppError>` where `T` is the RPC response type.
24#[macro_export]
25macro_rules! rpc_request {
26    ($state:expr, $request:expr) => {
27        $state
28            .rpc_sender()
29            .oneshot_request($request)
30            .await
31            .ok_or($crate::http_server::AppError::ChannelDropped)
32    };
33}
34
35/// Sends an RPC request and wraps the response in JSON.
36///
37/// Returns `Result<Json<T>, AppError>` where `T` is the RPC response type.
38#[macro_export]
39macro_rules! jsonify_rpc {
40    ($state:expr, $request:expr) => {
41        rpc_request!($state, $request).map(axum::Json)
42    };
43}