From 1c520b20f70ddbdab885ec6c4bf5c87893a26eb4 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Sun, 19 May 2024 22:18:06 +0200 Subject: jsonrpc: remove jsonrpc_internal crate --- jsonrpc/src/error.rs | 40 +++++++++++++++++++++++++++++++ jsonrpc/src/lib.rs | 6 +++-- jsonrpc/src/service.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 jsonrpc/src/error.rs create mode 100644 jsonrpc/src/service.rs (limited to 'jsonrpc/src') diff --git a/jsonrpc/src/error.rs b/jsonrpc/src/error.rs new file mode 100644 index 0000000..7f89729 --- /dev/null +++ b/jsonrpc/src/error.rs @@ -0,0 +1,40 @@ +use thiserror::Error as ThisError; + +pub type Result = std::result::Result; + +/// Represents karyon's jsonrpc Error. +#[derive(ThisError, Debug)] +pub enum Error { + #[error(transparent)] + IO(#[from] std::io::Error), + + #[error("Call Error: code: {0} msg: {1}")] + CallError(i32, String), + + #[error("RPC Method Error: code: {0} msg: {1}")] + RPCMethodError(i32, &'static str), + + #[error("Invalid Params: {0}")] + InvalidParams(&'static str), + + #[error("Invalid Request: {0}")] + InvalidRequest(&'static str), + + #[error(transparent)] + ParseJSON(#[from] serde_json::Error), + + #[error("Invalid Message Error: {0}")] + InvalidMsg(&'static str), + + #[error("Unsupported protocol: {0}")] + UnsupportedProtocol(String), + + #[error("Unexpected Error: {0}")] + General(&'static str), + + #[error(transparent)] + KaryonCore(#[from] karyon_core::error::Error), + + #[error(transparent)] + KaryonNet(#[from] karyon_net::Error), +} diff --git a/jsonrpc/src/lib.rs b/jsonrpc/src/lib.rs index 4ec03cc..3062512 100644 --- a/jsonrpc/src/lib.rs +++ b/jsonrpc/src/lib.rs @@ -68,13 +68,15 @@ mod client; mod codec; +mod error; pub mod message; mod server; +mod service; pub use client::Client; pub use server::Server; -pub use karyon_jsonrpc_internal::{impl_rpc_service, RPCMethod, RPCService}; -pub use karyon_jsonrpc_internal::{Error, Result}; +pub use error::{Error, Result}; pub use karyon_jsonrpc_macro::rpc_impl; pub use karyon_net::Endpoint; +pub use service::{RPCMethod, RPCService}; diff --git a/jsonrpc/src/service.rs b/jsonrpc/src/service.rs new file mode 100644 index 0000000..4c8c4b8 --- /dev/null +++ b/jsonrpc/src/service.rs @@ -0,0 +1,64 @@ +use std::{future::Future, pin::Pin}; + +use crate::Result; + +/// Represents the RPC method +pub type RPCMethod<'a> = Box RPCMethodOutput<'a> + Send + 'a>; +type RPCMethodOutput<'a> = + Pin> + 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; + fn name(&self) -> String; +} + +/// Implements the [`RPCService`] trait for a provided type. +/// +/// # Example +/// +/// ``` +/// use serde_json::Value; +/// +/// use karyon_jsonrpc::{Error, impl_rpc_service}; +/// +/// struct Hello {} +/// +/// impl Hello { +/// async fn foo(&self, params: Value) -> Result { +/// Ok(serde_json::json!("foo!")) +/// } +/// +/// async fn bar(&self, params: Value) -> Result { +/// Ok(serde_json::json!("bar!")) +/// } +/// } +/// +/// impl_rpc_service!(Hello, foo, bar); +/// +/// ``` +#[macro_export] +macro_rules! impl_rpc_service { + ($t:ty, $($m:ident),*) => { + impl karyon_jsonrpc::RPCService for $t { + fn get_method<'a>( + &'a self, + name: &'a str + ) -> Option { + 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() + } + } + }; +} -- cgit v1.2.3