diff options
author | hozan23 <hozan23@proton.me> | 2023-11-20 22:19:02 +0300 |
---|---|---|
committer | hozan23 <hozan23@proton.me> | 2023-11-20 22:19:02 +0300 |
commit | 2ee34b432e7652a34ee64a706b5ebc1bce867dce (patch) | |
tree | ec5b3611eddcfa4da4c33e8b1f2c8103079f1786 /jsonrpc/src | |
parent | dc7137f03f589967adf38d4a1b45f81f94732776 (diff) |
jsonrpc: move RPCService to separate module
Diffstat (limited to 'jsonrpc/src')
-rw-r--r-- | jsonrpc/src/lib.rs | 4 | ||||
-rw-r--r-- | jsonrpc/src/server.rs | 60 | ||||
-rw-r--r-- | jsonrpc/src/service.rs | 60 |
3 files changed, 65 insertions, 59 deletions
diff --git a/jsonrpc/src/lib.rs b/jsonrpc/src/lib.rs index 8a547d9..2ac89c9 100644 --- a/jsonrpc/src/lib.rs +++ b/jsonrpc/src/lib.rs @@ -50,6 +50,7 @@ mod client; mod error; pub mod message; mod server; +mod service; mod utils; pub const JSONRPC_VERSION: &str = "2.0"; @@ -58,4 +59,5 @@ use error::{Error, Result}; pub use client::Client; pub use error::Error as JsonRPCError; -pub use server::{RPCMethod, RPCService, Server}; +pub use server::Server; +pub use service::{RPCMethod, RPCService}; diff --git a/jsonrpc/src/server.rs b/jsonrpc/src/server.rs index 9642381..6c01a96 100644 --- a/jsonrpc/src/server.rs +++ b/jsonrpc/src/server.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc}; +use std::{collections::HashMap, sync::Arc}; use log::{debug, error, warn}; use smol::lock::RwLock; @@ -11,6 +11,7 @@ use karyons_net::{listen, Conn, Endpoint, Listener}; use crate::{ message, + service::RPCService, utils::{read_until, write_all}, Error, Result, JSONRPC_VERSION, }; @@ -204,60 +205,3 @@ impl<'a> Server<'a> { } } } - -/// Represents the RPC method -pub type RPCMethod<'a> = Box<dyn Fn(serde_json::Value) -> RPCMethodOutput<'a> + Send + 'a>; -type RPCMethodOutput<'a> = - Pin<Box<dyn Future<Output = Result<serde_json::Value>> + Send + Sync + 'a>>; - -/// Defines the interface for an RPC service. -pub trait RPCService: Sync + Send { - fn get_method<'a>(&'a self, name: &'a str) -> Option<RPCMethod>; - fn name(&self) -> String; -} - -/// Implements the `RPCService` trait for a provided type. -/// -/// # Example -/// -/// ``` -/// use serde_json::Value; -/// -/// use karyons_jsonrpc::{JsonRPCError, register_service}; -/// -/// struct Hello {} -/// -/// impl Hello { -/// async fn say_hello(&self, params: Value) -> Result<Value, JsonRPCError> { -/// Ok(serde_json::json!("hello!")) -/// } -/// } -/// -/// register_service!(Hello, say_hello); -/// -/// ``` -#[macro_export] -macro_rules! register_service { - ($t:ty, $($m:ident),*) => { - impl karyons_jsonrpc::RPCService for $t { - fn get_method<'a>( - &'a self, - name: &'a str - ) -> Option<karyons_jsonrpc::RPCMethod> { - match name { - $( - stringify!($m) => { - Some(Box::new(move |params: serde_json::Value| Box::pin(self.$m(params)))) - } - )* - _ => None, - } - - - } - fn name(&self) -> String{ - stringify!($t).to_string() - } - } - }; -} diff --git a/jsonrpc/src/service.rs b/jsonrpc/src/service.rs new file mode 100644 index 0000000..f0e2828 --- /dev/null +++ b/jsonrpc/src/service.rs @@ -0,0 +1,60 @@ +use std::{future::Future, pin::Pin}; + +use crate::Result; + +/// Represents the RPC method +pub type RPCMethod<'a> = Box<dyn Fn(serde_json::Value) -> RPCMethodOutput<'a> + Send + 'a>; +type RPCMethodOutput<'a> = + Pin<Box<dyn Future<Output = Result<serde_json::Value>> + Send + Sync + 'a>>; + +/// Defines the interface for an RPC service. +pub trait RPCService: Sync + Send { + fn get_method<'a>(&'a self, name: &'a str) -> Option<RPCMethod>; + fn name(&self) -> String; +} + +/// Implements the `RPCService` trait for a provided type. +/// +/// # Example +/// +/// ``` +/// use serde_json::Value; +/// +/// use karyons_jsonrpc::{JsonRPCError, register_service}; +/// +/// struct Hello {} +/// +/// impl Hello { +/// async fn say_hello(&self, params: Value) -> Result<Value, JsonRPCError> { +/// Ok(serde_json::json!("hello!")) +/// } +/// } +/// +/// register_service!(Hello, say_hello); +/// +/// ``` +#[macro_export] +macro_rules! register_service { + ($t:ty, $($m:ident),*) => { + impl karyons_jsonrpc::RPCService for $t { + fn get_method<'a>( + &'a self, + name: &'a str + ) -> Option<karyons_jsonrpc::RPCMethod> { + match name { + $( + stringify!($m) => { + Some(Box::new(move |params: serde_json::Value| Box::pin(self.$m(params)))) + } + )* + _ => None, + } + + + } + fn name(&self) -> String{ + stringify!($t).to_string() + } + } + }; +} |