Skip to main content

hyperlane_plugin_rpc/
enum.rs

1use super::*;
2
3/// JSON-RPC 2.0 request/response correlation identifier.
4#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
5#[serde(untagged)]
6pub enum RpcId {
7    /// Integer identifier.
8    Number(i64),
9    /// String identifier.
10    Text(String),
11    /// Explicit null identifier.
12    #[default]
13    Null,
14}
15
16/// Recognized JSON-RPC 2.0 request object keys parsed zero-copy without heap allocation.
17pub(crate) enum RpcFieldKey {
18    /// `"jsonrpc"` protocol version field.
19    Jsonrpc,
20    /// `"method"` handler name field.
21    Method,
22    /// `"params"` parameter payload field.
23    Params,
24    /// `"id"` correlation identifier field.
25    Id,
26    /// Any unrecognized field ignored cleanly.
27    Other,
28}
29
30/// Zero-copy string field deserialization outcome tolerating wrong JSON types without buffering.
31pub(crate) enum LenientStr<'a> {
32    /// Valid JSON string borrowed directly when escape-free.
33    Valid(Cow<'a, str>),
34    /// Explicit JSON `null` leaving the optional field absent while preserving correlation `id`.
35    Null,
36    /// Non-string JSON value marking the enclosing request invalid.
37    Invalid,
38}
39
40/// Zero-copy JSON-RPC `id` deserialization outcome avoiding `#[serde(untagged)]` buffering.
41pub(crate) enum LenientId {
42    /// Valid correlation id (`None` for explicit JSON `null`).
43    Valid(Option<RpcId>),
44    /// Invalid JSON type for `id` (array/object/boolean/float).
45    Invalid,
46}
47
48/// Zero-copy streaming batch/single JSON-RPC element parsed in one pass without `serde` content buffering.
49pub(crate) enum BorrowedBatchItem<'a> {
50    /// Syntactically valid JSON-RPC candidate object.
51    Request(BorrowedRpcRequest<'a>),
52    /// Non-object or type-mismatched JSON element producing `Invalid Request`.
53    Invalid,
54}
55
56/// Compact JSON-RPC 2.0 response body variant avoiding heap allocations for standard errors.
57pub(crate) enum CompactRpcBody {
58    /// Successful JSON result payload.
59    Result(Value),
60    /// Dynamic error object returned by handler or interceptor.
61    Error(RpcError),
62    /// Pre-serialized static JSON error object (e.g. `Invalid Request` or `Method not found`).
63    StaticError(&'static [u8]),
64}
65
66/// Internal per-item batch execution slot avoiding `tokio::spawn` overhead.
67pub(crate) enum BatchDispatchSlot {
68    /// Immediately resolved static response (for example validation or method-not-found error).
69    Ready(CompactRpcResponse),
70    /// Async handler future executing concurrently inside `poll_fn`.
71    Pending {
72        /// Running boxed handler future.
73        future: RpcFuture,
74        /// Optional correlation id (`None` for notifications).
75        id: Option<RpcId>,
76    },
77}