Skip to main content

hyperlane_plugin_rpc/
struct.rs

1use super::*;
2
3/// JSON-RPC 2.0 error object returned inside [`RpcResponse`].
4#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
5pub struct RpcError {
6    /// Numeric JSON-RPC 2.0 error code.
7    #[get(type(copy))]
8    pub(crate) code: i64,
9    /// Human-readable error summary.
10    pub(crate) message: String,
11    /// Optional structured error details.
12    #[serde(skip_serializing_if = "Option::is_none")]
13    #[get(type(clone))]
14    pub(crate) data: Option<Value>,
15}
16
17/// JSON-RPC 2.0 incoming request envelope.
18#[derive(Clone, Data, Debug, Deserialize, PartialEq, Serialize)]
19pub struct RpcRequest {
20    /// Protocol version string (must equal `"2.0"`).
21    pub(crate) jsonrpc: String,
22    /// Target RPC method name.
23    pub(crate) method: String,
24    /// Optional positional array or named object parameters.
25    #[serde(default)]
26    #[get(type(clone))]
27    pub(crate) params: Option<Value>,
28    /// Correlation identifier (`None` marks a JSON-RPC notification expecting no reply).
29    #[serde(default)]
30    #[get(type(clone))]
31    pub(crate) id: Option<RpcId>,
32}
33
34/// JSON-RPC 2.0 outgoing response envelope.
35#[derive(Clone, Data, Debug, Deserialize, PartialEq, Serialize)]
36pub struct RpcResponse {
37    /// Protocol version string (`"2.0"`).
38    pub(crate) jsonrpc: String,
39    /// Success result value (`None` on error).
40    #[serde(skip_serializing_if = "Option::is_none")]
41    #[get(type(clone))]
42    pub(crate) result: Option<Value>,
43    /// Structured error object (`None` on success).
44    #[serde(skip_serializing_if = "Option::is_none")]
45    #[get(type(clone))]
46    pub(crate) error: Option<RpcError>,
47    /// Echoed correlation identifier (`RpcId::Null` for parse/invalid-request errors).
48    pub(crate) id: RpcId,
49}
50
51/// Configuration for [`RpcRouter`] dispatch and [`RpcHook`] HTTP endpoint handling.
52#[derive(Clone, Data, Debug, Eq, PartialEq)]
53pub struct RpcConfig {
54    /// HTTP path matched by [`RpcHook`] (default `"/rpc"`).
55    pub(crate) endpoint: String,
56    /// Maximum incoming JSON body size in bytes.
57    #[get(type(copy))]
58    pub(crate) max_body_size: usize,
59    /// Maximum batch array length allowed per HTTP request.
60    #[get(type(copy))]
61    pub(crate) max_batch_size: usize,
62}
63
64/// Zero-allocation multiplicative 64-bit hasher for JSON-RPC method table lookups.
65#[derive(Clone, Copy, Default)]
66pub(crate) struct FxHasher {
67    /// Running 64-bit hash accumulator.
68    pub(crate) state: u64,
69}
70
71/// Thread-safe registry mapping JSON-RPC method names to async handlers with zero-allocation lookup.
72#[derive(Clone)]
73pub struct RpcRouter {
74    /// Registered method handlers keyed by method name.
75    pub(crate) methods: RpcMethodMap,
76    /// Registered interceptors invoked before each method call.
77    pub(crate) interceptors: Vec<Arc<dyn RpcInterceptor>>,
78}
79
80/// Process-wide [`RpcPlugin`] registry holding [`RpcConfig`] and an immutable [`RpcRouter`].
81#[derive(Clone)]
82pub struct RpcPlugin {
83    /// Active JSON-RPC server configuration.
84    pub(crate) config: RpcConfig,
85    /// Registered JSON-RPC method router.
86    pub(crate) router: RpcRouter,
87}
88
89/// Zero-copy visitor decoding [`RpcFieldKey`] without string allocations.
90pub(crate) struct RpcFieldKeyVisitor;
91
92/// Zero-copy visitor decoding [`LenientStr`] without `serde` content buffering.
93pub(crate) struct LenientStrVisitor;
94
95/// Zero-copy visitor decoding [`LenientId`] without `#[serde(untagged)]` buffering.
96pub(crate) struct LenientIdVisitor;
97
98/// Zero-copy streaming visitor decoding [`BorrowedBatchItem`] in a single pass.
99pub(crate) struct BorrowedBatchItemVisitor;
100
101/// Zero-copy borrowed JSON-RPC 2.0 request envelope avoiding intermediate `Value` and `String` allocations.
102pub(crate) struct BorrowedRpcRequest<'a> {
103    /// Borrowed protocol version string (must equal `"2.0"`).
104    pub(crate) jsonrpc: Option<Cow<'a, str>>,
105    /// Borrowed target RPC method name.
106    pub(crate) method: Option<Cow<'a, str>>,
107    /// Optional positional array or named object parameters.
108    pub(crate) params: Option<Value>,
109    /// Correlation identifier (`None` marks a notification).
110    pub(crate) id: Option<RpcId>,
111}
112
113/// Compact outgoing JSON-RPC 2.0 response serialized directly with static envelope framing.
114pub(crate) struct CompactRpcResponse {
115    /// Response body payload (`result`, dynamic `error`, or pre-serialized static `error`).
116    pub(crate) body: CompactRpcBody,
117    /// Echoed correlation identifier.
118    pub(crate) id: RpcId,
119}
120
121/// Opt-in [`ServerHook`] dispatching HTTP `POST` requests on the configured JSON-RPC endpoint.
122#[derive(Clone, Copy, Debug, Default)]
123#[hyperlane::request_middleware(-70)]
124pub struct RpcHook;