roa_jsonrpc/
lib.rs

1#![cfg_attr(feature = "docs", feature(doc_cfg, external_doc))]
2#![cfg_attr(feature = "docs", doc(include = "../README.md"))]
3#![cfg_attr(feature = "docs", warn(missing_docs))]
4
5use bytes::Bytes;
6use roa::preload::*;
7use roa::{async_trait, Context, Endpoint, Result, State};
8
9#[doc(no_inline)]
10pub use jsonrpc_v2::*;
11
12/// A wrapper for [`jsonrpc_v2::Server`], implemented [`roa::Endpoint`].
13///
14/// [`jsonrpc_v2::Server`]: https://docs.rs/jsonrpc-v2/0.5.2/jsonrpc_v2/struct.Server.html
15/// [`roa::Endpoint`]: https://docs.rs/roa/0.5.2/roa/trait.Endpoint.html
16pub struct RpcEndpoint<R>(pub Server<R>);
17
18#[async_trait(? Send)]
19impl<'a, S, R> Endpoint<'a, S> for RpcEndpoint<R>
20where
21    S: State,
22    R: Router + Sync + Send + 'static,
23{
24    #[inline]
25    async fn call(&'a self, ctx: &'a mut Context<S>) -> Result {
26        let data = ctx.read().await?;
27        let resp = self.0.handle(Bytes::from(data)).await;
28        ctx.write_json(&resp)
29    }
30}