aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-06-30 04:42:07 +0200
committerhozan23 <hozan23@karyontech.net>2024-06-30 04:42:07 +0200
commita45bbec25952a15cacb105b536432d6fbe3fb7b1 (patch)
tree280072dee7350b8a8deca081906f39f50e4dadba /jsonrpc
parentf842b0fc733b48d83d127bf06b264a781e725116 (diff)
jsonrpc: remove unwrap() and use expect() for examples, docs, and tests
Diffstat (limited to 'jsonrpc')
-rw-r--r--jsonrpc/src/client/builder.rs44
-rw-r--r--jsonrpc/src/client/message_dispatcher.rs11
-rw-r--r--jsonrpc/src/client/mod.rs2
-rw-r--r--jsonrpc/src/server/builder.rs36
-rw-r--r--jsonrpc/tests/impl_rpc_service.rs7
-rw-r--r--jsonrpc/tests/rpc_impl.rs7
6 files changed, 72 insertions, 35 deletions
diff --git a/jsonrpc/src/client/builder.rs b/jsonrpc/src/client/builder.rs
index d1e3b67..9adaa51 100644
--- a/jsonrpc/src/client/builder.rs
+++ b/jsonrpc/src/client/builder.rs
@@ -28,8 +28,10 @@ impl Client {
/// use karyon_jsonrpc::Client;
///
/// async {
- /// let builder = Client::builder("ws://127.0.0.1:3000").unwrap();
- /// let client = builder.build().await.unwrap();
+ /// let builder = Client::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new client builder");
+ /// let client = builder.build().await
+ /// .expect("Build a new client");
/// };
/// ```
pub fn builder(endpoint: impl ToEndpoint) -> Result<ClientBuilder> {
@@ -63,9 +65,11 @@ impl ClientBuilder {
/// use karyon_jsonrpc::Client;
///
/// async {
- /// let client = Client::builder("ws://127.0.0.1:3000").unwrap()
+ /// let client = Client::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new client builder")
/// .set_timeout(5000)
- /// .build().await.unwrap();
+ /// .build().await
+ /// .expect("Build a new client");
/// };
/// ```
pub fn set_timeout(mut self, timeout: u64) -> Self {
@@ -87,9 +91,11 @@ impl ClientBuilder {
/// use karyon_jsonrpc::Client;
///
/// async {
- /// let client = Client::builder("ws://127.0.0.1:3000").unwrap()
+ /// let client = Client::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new client builder")
/// .set_max_subscription_buffer_size(10000)
- /// .build().await.unwrap();
+ /// .build().await
+ /// .expect("Build a new client");
/// };
/// ```
pub fn set_max_subscription_buffer_size(mut self, size: usize) -> Self {
@@ -107,8 +113,12 @@ impl ClientBuilder {
/// async {
/// let tcp_config = TcpConfig::default();
///
- /// let client = Client::builder("ws://127.0.0.1:3000").unwrap()
- /// .tcp_config(tcp_config).unwrap().build().await.unwrap();
+ /// let client = Client::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new client builder")
+ /// .tcp_config(tcp_config)
+ /// .expect("Add tcp config")
+ /// .build().await
+ /// .expect("Build a new client");
/// };
/// ```
///
@@ -135,9 +145,12 @@ impl ClientBuilder {
/// async {
/// let tls_config = rustls::ClientConfig::new(...);
///
- /// let client_builder = Client::builder("ws://127.0.0.1:3000").unwrap()
- /// .tls_config(tls_config, "example.com").unwrap()
- /// .build().await.unwrap();
+ /// let client_builder = Client::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new client builder")
+ /// .tls_config(tls_config, "example.com")
+ /// .expect("Add tls config")
+ /// .build().await
+ /// .expect("Build a new client");
/// };
/// ```
///
@@ -168,10 +181,13 @@ impl ClientBuilder {
///
/// async {
/// let tcp_config = TcpConfig::default();
- /// let client = Client::builder("ws://127.0.0.1:3000").unwrap()
- /// .tcp_config(tcp_config).unwrap()
+ /// let client = Client::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new client builder")
+ /// .tcp_config(tcp_config)
+ /// .expect("Add tcp config")
/// .set_timeout(5000)
- /// .build().await.unwrap();
+ /// .build().await
+ /// .expect("Build a new client");
/// };
///
/// ```
diff --git a/jsonrpc/src/client/message_dispatcher.rs b/jsonrpc/src/client/message_dispatcher.rs
index f370985..53dd9e5 100644
--- a/jsonrpc/src/client/message_dispatcher.rs
+++ b/jsonrpc/src/client/message_dispatcher.rs
@@ -48,10 +48,13 @@ impl MessageDispatcher {
/// If a channel is registered for the response's ID, the response is sent
/// through that channel. If no channel is found for the ID, returns an error.
pub(super) async fn dispatch(&self, res: message::Response) -> Result<()> {
- if res.id.is_none() {
- return Err(Error::InvalidMsg("Response id is none"));
- }
- let id: RequestID = serde_json::from_value(res.id.clone().unwrap())?;
+ let res_id = match res.id {
+ Some(ref rid) => rid.clone(),
+ None => {
+ return Err(Error::InvalidMsg("Response id is none"));
+ }
+ };
+ let id: RequestID = serde_json::from_value(res_id)?;
let val = self.chans.lock().await.remove(&id);
match val {
Some(tx) => tx.send(res).await.map_err(Error::from),
diff --git a/jsonrpc/src/client/mod.rs b/jsonrpc/src/client/mod.rs
index 6f6bd97..b54656b 100644
--- a/jsonrpc/src/client/mod.rs
+++ b/jsonrpc/src/client/mod.rs
@@ -164,7 +164,7 @@ impl Client {
// It should be OK to unwrap here, as the message dispatcher checks
// for the response id.
- if *response.id.as_ref().unwrap() != id {
+ if *response.id.as_ref().expect("Get response id") != id {
return Err(Error::InvalidMsg("Invalid response id"));
}
diff --git a/jsonrpc/src/server/builder.rs b/jsonrpc/src/server/builder.rs
index 01daf8e..feb51ec 100644
--- a/jsonrpc/src/server/builder.rs
+++ b/jsonrpc/src/server/builder.rs
@@ -41,9 +41,11 @@ impl ServerBuilder {
/// }
///
/// async {
- /// let server = Server::builder("ws://127.0.0.1:3000").unwrap()
+ /// let server = Server::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new server builder")
/// .service(Arc::new(Ping{}))
- /// .build().await.unwrap();
+ /// .build().await
+ /// .expect("Build the server");
/// };
///
/// ```
@@ -100,10 +102,12 @@ impl ServerBuilder {
///
/// async {
/// let ping_service = Arc::new(Ping{});
- /// let server = Server::builder("ws://127.0.0.1:3000").unwrap()
+ /// let server = Server::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new server builder")
/// .service(ping_service.clone())
/// .pubsub_service(ping_service)
- /// .build().await.unwrap();
+ /// .build().await
+ /// .expect("Build the server");
/// };
///
/// ```
@@ -121,9 +125,12 @@ impl ServerBuilder {
///
/// async {
/// let tcp_config = TcpConfig::default();
- /// let server = Server::builder("ws://127.0.0.1:3000").unwrap()
- /// .tcp_config(tcp_config).unwrap()
- /// .build().await.unwrap();
+ /// let server = Server::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new server builder")
+ /// .tcp_config(tcp_config)
+ /// .expect("Add tcp config")
+ /// .build().await
+ /// .expect("Build the server");
/// };
/// ```
///
@@ -149,9 +156,12 @@ impl ServerBuilder {
///
/// async {
/// let tls_config = rustls::ServerConfig::new(...);
- /// let server = Server::builder("ws://127.0.0.1:3000").unwrap()
- /// .tls_config(tls_config).unwrap()
- /// .build().await.unwrap();
+ /// let server = Server::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new server builder")
+ /// .tls_config(tls_config)
+ /// .expect("Add tls config")
+ /// .build().await
+ /// .expect("Build the server");
/// };
/// ```
///
@@ -191,8 +201,10 @@ impl Server {
/// ```
/// use karyon_jsonrpc::Server;
/// async {
- /// let server = Server::builder("ws://127.0.0.1:3000").unwrap()
- /// .build().await.unwrap();
+ /// let server = Server::builder("ws://127.0.0.1:3000")
+ /// .expect("Create a new server builder")
+ /// .build().await
+ /// .expect("Build the server");
/// };
/// ```
pub fn builder(endpoint: impl ToEndpoint) -> Result<ServerBuilder> {
diff --git a/jsonrpc/tests/impl_rpc_service.rs b/jsonrpc/tests/impl_rpc_service.rs
index bb42679..52c5c31 100644
--- a/jsonrpc/tests/impl_rpc_service.rs
+++ b/jsonrpc/tests/impl_rpc_service.rs
@@ -21,7 +21,10 @@ fn service() {
let params = serde_json::json!("params");
smol::block_on(async {
- let foo_method = f.get_method("foo").unwrap();
- assert_eq!(foo_method(params.clone()).await.unwrap(), params);
+ let foo_method = f.get_method("foo").expect("Get method foo");
+ assert_eq!(
+ foo_method(params.clone()).await.expect("Call foo method"),
+ params
+ );
});
}
diff --git a/jsonrpc/tests/rpc_impl.rs b/jsonrpc/tests/rpc_impl.rs
index 64e3bb1..2a24bbf 100644
--- a/jsonrpc/tests/rpc_impl.rs
+++ b/jsonrpc/tests/rpc_impl.rs
@@ -20,7 +20,10 @@ fn rpc_impl_service() {
let params = serde_json::json!("params");
smol::block_on(async {
- let foo_method = f.get_method("foo").unwrap();
- assert_eq!(foo_method(params.clone()).await.unwrap(), params);
+ let foo_method = f.get_method("foo").expect("Get method foo");
+ assert_eq!(
+ foo_method(params.clone()).await.expect("Call foo method"),
+ params
+ );
});
}