A synchronous, lightweight crate for creating your own JSON RPC 2.0 protocol.
WARNING: This is very early in development and is subject to significant change.
What is seraphic?
seraphic provides a straightforward way of defining your very own JSON RPC 2.0 based protocol messages using Rust macros.
A quick refresher on JSON RPC
Json rpc messages are structured as follows:
Manually creating these structs as JSON is easy enough, but organizing all the methods, requests and responses can quickly get hectic. seraphic offers a quick an easy way to define all of these!
Getting started
RpcNamespace
A trait for defining how the methods of your RPC protocol are separated
The variants of the namespace enum define the method namespaces of your protocol. They are simply the variants' names in lowercase; so the above code will define your methods to have the namespaces "foo", "bar" and "baz", with methods appearing after a ':'.
If the separator argument isn't passed it defaults to '_'.
RpcRequest & RpcResponse
traits for defining the requests/responses that are used by your protocol
Each method in your namespace maps to a single request you've defined. Method names are defined by the whatever the name of your request is before the word "Request". So, the above struct's corresponding method would be "foo:someFoo". The syntax for mapping a request to a namespace is: <Namespace struct name>:<namespace variant>
NOTE:
Any struct you want to derive
RpcRequeston MUST have a name ending with the word "Request" and all of it's fields MUST be types that implementserde::Serializeandserde::Deserialize
Each RpcRequest should have a corresponding RpcResponse struct. This can be done in two ways:
- Make sure another struct with the same prefix but with the word "Response" instead of "Request" is in scope
- pass a
responseargument in therpc_requestproc macro attribute// If some response isn't the response to some other `RpcRequest` already // This is fine because `RpcResponse` is a flag trait
Keep in mind:
- Both
RpcRequestandRpcResponsestructs MUST implementserde::Serialize,serde::Deserialize,CloneandDebug - mutliple
RpcRequestscan have the same correspondingRpcResponse - If a
responseargument is passed in therpc_requestmacros, the macro assumes the struct already implementsRpcResponse, if not, the proc macros assumes the corresponding Response struct does not implementRpcResponseand will implement it for you.
RequestWrapper and ResponseWrapper
simply enums that include all of the
RpcRequestandRpcResponsestructs included in your protocol.
You do not need to manually define these enums. Instead you can use the wrapper macro, which has the following syntax:
wrapper!(<ResponseWrapper|RequestWrapper>, <Name of your enum>, [<Each variant type of your enum>])
wrapper!;
// expands to:
wrapper!;
// expands to:
These structs need only to implement Debug
Message<Rq,Rs>
The main type you will interact with for passing your messages.
Rqis aRequestWrappertype andRsis aResponseWrappertype.
Referring to the tests might be helpful