hyperlane-plugin-rpc 23.2.0

A JSON-RPC 2.0 server plugin for the Hyperlane framework with async method routing, batch dispatch, notification handling, and configurable payload and batch DoS bounds.
Documentation
use super::*;

/// JSON-RPC 2.0 request/response correlation identifier.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(untagged)]
pub enum RpcId {
    /// Integer identifier.
    Number(i64),
    /// String identifier.
    Text(String),
    /// Explicit null identifier.
    #[default]
    Null,
}

/// Recognized JSON-RPC 2.0 request object keys parsed zero-copy without heap allocation.
pub(crate) enum RpcFieldKey {
    /// `"jsonrpc"` protocol version field.
    Jsonrpc,
    /// `"method"` handler name field.
    Method,
    /// `"params"` parameter payload field.
    Params,
    /// `"id"` correlation identifier field.
    Id,
    /// Any unrecognized field ignored cleanly.
    Other,
}

/// Zero-copy string field deserialization outcome tolerating wrong JSON types without buffering.
pub(crate) enum LenientStr<'a> {
    /// Valid JSON string borrowed directly when escape-free.
    Valid(Cow<'a, str>),
    /// Explicit JSON `null` leaving the optional field absent while preserving correlation `id`.
    Null,
    /// Non-string JSON value marking the enclosing request invalid.
    Invalid,
}

/// Zero-copy JSON-RPC `id` deserialization outcome avoiding `#[serde(untagged)]` buffering.
pub(crate) enum LenientId {
    /// Valid correlation id (`None` for explicit JSON `null`).
    Valid(Option<RpcId>),
    /// Invalid JSON type for `id` (array/object/boolean/float).
    Invalid,
}

/// Zero-copy streaming batch/single JSON-RPC element parsed in one pass without `serde` content buffering.
pub(crate) enum BorrowedBatchItem<'a> {
    /// Syntactically valid JSON-RPC candidate object.
    Request(BorrowedRpcRequest<'a>),
    /// Non-object or type-mismatched JSON element producing `Invalid Request`.
    Invalid,
}

/// Compact JSON-RPC 2.0 response body variant avoiding heap allocations for standard errors.
pub(crate) enum CompactRpcBody {
    /// Successful JSON result payload.
    Result(Value),
    /// Dynamic error object returned by handler or interceptor.
    Error(RpcError),
    /// Pre-serialized static JSON error object (e.g. `Invalid Request` or `Method not found`).
    StaticError(&'static [u8]),
}

/// Internal per-item batch execution slot avoiding `tokio::spawn` overhead.
pub(crate) enum BatchDispatchSlot {
    /// Immediately resolved static response (for example validation or method-not-found error).
    Ready(CompactRpcResponse),
    /// Async handler future executing concurrently inside `poll_fn`.
    Pending {
        /// Running boxed handler future.
        future: RpcFuture,
        /// Optional correlation id (`None` for notifications).
        id: Option<RpcId>,
    },
}