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/Cargo.toml | 3 -- jsonrpc/jsonrpc_internal/Cargo.toml | 18 ---------- jsonrpc/jsonrpc_internal/src/error.rs | 40 --------------------- jsonrpc/jsonrpc_internal/src/lib.rs | 65 ----------------------------------- jsonrpc/jsonrpc_macro/Cargo.toml | 9 ++--- jsonrpc/jsonrpc_macro/src/lib.rs | 2 +- jsonrpc/src/error.rs | 40 +++++++++++++++++++++ jsonrpc/src/lib.rs | 6 ++-- jsonrpc/src/service.rs | 64 ++++++++++++++++++++++++++++++++++ 9 files changed, 112 insertions(+), 135 deletions(-) delete mode 100644 jsonrpc/jsonrpc_internal/Cargo.toml delete mode 100644 jsonrpc/jsonrpc_internal/src/error.rs delete mode 100644 jsonrpc/jsonrpc_internal/src/lib.rs create mode 100644 jsonrpc/src/error.rs create mode 100644 jsonrpc/src/service.rs (limited to 'jsonrpc') diff --git a/jsonrpc/Cargo.toml b/jsonrpc/Cargo.toml index e81ec10..cf49d6e 100644 --- a/jsonrpc/Cargo.toml +++ b/jsonrpc/Cargo.toml @@ -8,7 +8,6 @@ default = ["smol"] smol = [ "karyon_core/smol", "karyon_net/smol", - "karyon_jsonrpc_internal/smol", "karyon_jsonrpc_macro/smol", "dep:futures-rustls", "async-tungstenite/async-std-runtime", @@ -16,7 +15,6 @@ smol = [ tokio = [ "karyon_core/tokio", "karyon_net/tokio", - "karyon_jsonrpc_internal/tokio", "karyon_jsonrpc_macro/tokio", "async-tungstenite/tokio-runtime", "dep:tokio-rustls", @@ -27,7 +25,6 @@ karyon_core = { workspace = true, default-features = false } karyon_net = { workspace = true, default-features = false } karyon_jsonrpc_macro = { path = "jsonrpc_macro", default-features = false } -karyon_jsonrpc_internal = { path = "jsonrpc_internal", default-features = false } log = "0.4.21" rand = "0.8.5" diff --git a/jsonrpc/jsonrpc_internal/Cargo.toml b/jsonrpc/jsonrpc_internal/Cargo.toml deleted file mode 100644 index 5a3acc4..0000000 --- a/jsonrpc/jsonrpc_internal/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -name = "karyon_jsonrpc_internal" -version.workspace = true -edition.workspace = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[features] -default = ["smol"] -smol = ["karyon_core/smol", "karyon_net/smol"] -tokio = ["karyon_core/tokio", "karyon_net/tokio"] - -[dependencies] -karyon_core = { workspace = true, default-features = false } -karyon_net = { workspace = true, default-features = false } - -serde_json = "1.0.114" -thiserror = "1.0.58" diff --git a/jsonrpc/jsonrpc_internal/src/error.rs b/jsonrpc/jsonrpc_internal/src/error.rs deleted file mode 100644 index 7f89729..0000000 --- a/jsonrpc/jsonrpc_internal/src/error.rs +++ /dev/null @@ -1,40 +0,0 @@ -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/jsonrpc_internal/src/lib.rs b/jsonrpc/jsonrpc_internal/src/lib.rs deleted file mode 100644 index 95af82a..0000000 --- a/jsonrpc/jsonrpc_internal/src/lib.rs +++ /dev/null @@ -1,65 +0,0 @@ -mod error; -use std::{future::Future, pin::Pin}; - -pub use error::{Error, 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_internal::{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_internal::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() - } - } - }; -} diff --git a/jsonrpc/jsonrpc_macro/Cargo.toml b/jsonrpc/jsonrpc_macro/Cargo.toml index 17140c5..f0af550 100644 --- a/jsonrpc/jsonrpc_macro/Cargo.toml +++ b/jsonrpc/jsonrpc_macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "karyon_jsonrpc_macro" -version.workspace = true +version.workspace = true edition.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -10,15 +10,12 @@ proc-macro = true [features] default = ["smol"] -smol = ["karyon_jsonrpc_internal/smol"] -tokio = ["karyon_jsonrpc_internal/tokio"] +smol = [] +tokio = [] [dependencies] -karyon_jsonrpc_internal = { path = "../jsonrpc_internal", default-features = false } - proc-macro2 = "1.0" quote = "1.0" syn = { version = "1.0", features = ["full"] } serde_json = "1.0.114" - diff --git a/jsonrpc/jsonrpc_macro/src/lib.rs b/jsonrpc/jsonrpc_macro/src/lib.rs index f2015d4..c3d51e8 100644 --- a/jsonrpc/jsonrpc_macro/src/lib.rs +++ b/jsonrpc/jsonrpc_macro/src/lib.rs @@ -39,7 +39,7 @@ pub fn rpc_impl(_attr: TokenStream, item: TokenStream) -> TokenStream { let item2: TokenStream2 = item.into(); let quoted = quote! { - karyon_jsonrpc_internal::impl_rpc_service!(#self_ty, #(#methods),*); + karyon_jsonrpc::impl_rpc_service!(#self_ty, #(#methods),*); #item2 }; 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