Skip to main content

Crate alux_jsonrpc_direct

Crate alux_jsonrpc_direct 

Source
Expand description

§alux-jsonrpc-direct

alux-jsonrpc-direct interprets an alux-jsonrpc program as a JSON-RPC 2.0 message handler. It performs method-name dispatch, parameter decoding, response rendering, batch handling, notification handling, and protocol error reporting without a framework. Its only dependencies are serde and serde_json.

use alux_jsonrpc::JsonRpcProgramExt;
use alux_jsonrpc_direct::DirectImpl;

let rpc = DirectImpl::new(App(40));
let methods = rpc.compile_jsonrpc(rpc.example_rpc::<App>())?;

let answer = methods.dispatch(r#"{"jsonrpc":"2.0","method":"status_current","id":1}"#).await;

assert_eq!(answer.unwrap(), r#"{"jsonrpc":"2.0","result":40,"id":1}"#);

dispatch answers with nothing when the document asks for nothing — a notification, or a batch of them. Everything else answers with one response document.

A method marked fallible converts its error into a protocol error through RpcErrorAlg, exactly as alux-jsonrpc-jsonrpsee does, so the same program and the same domain compile through either interpretation unchanged.

§What it owns, and what it does not

It owns the message layer: a MethodTable maps each method name to its decoded, applied, and rendered answer. Two tables can be combined with merge when they contain different method names.

It owns no transport and no runtime. A surface answers one request document and says nothing about how that document arrived, so serving it over HTTP, a WebSocket, a pipe, or a test harness is a separate decision — and an HTTP surface can be declared with alux-http rather than bundled in here.

§Differences from the jsonrpsee interpretation

  • A method’s output needs Serialize, not Serialize + Clone, because no answer is ever duplicated.
  • Protocol errors are stated by this crate rather than by a framework, so their codes and messages are part of its observable behavior and are covered by its tests.
  • merge reports a duplicated method name as DuplicateMethod rather than a framework error.

Interprets typed JSON-RPC programs as a JSON-RPC 2.0 message handler.

This interpretation implements the specification itself rather than delegating to a framework: it decodes a request document, dispatches by method name, decodes parameters, and renders the response — including the protocol’s own errors for a malformed document, an unknown method, and a parameter a method cannot read. It carries no transport and no runtime, so whatever moves bytes decides how a request arrives.

Structs§

DirectImpl
Interprets typed JSON-RPC programs as a dispatchable JSON-RPC 2.0 surface.
DuplicateMethod
Names a method that two composed programs both declared.
MethodTable
A JSON-RPC surface: every method a program declared, keyed by the name it answers to.
RpcError
What a JSON-RPC response states in its error member.

Constants§

INTERNAL_ERROR
The code a failure of the interpretation itself carries.
INVALID_PARAMS
The code a parameter the method cannot read carries.
INVALID_REQUEST
The code a document that is not a JSON-RPC request carries.
METHOD_NOT_FOUND
The code an unknown method name carries.
PARSE_ERROR
The code a malformed JSON document carries.

Traits§

DirectArgs
Parses the parameter product consumed by a JSON-RPC operation.

Type Aliases§

Answer
The answer one dispatched method produces.