From a45bbec25952a15cacb105b536432d6fbe3fb7b1 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Sun, 30 Jun 2024 04:42:07 +0200 Subject: jsonrpc: remove unwrap() and use expect() for examples, docs, and tests --- jsonrpc/src/client/builder.rs | 44 ++++++++++++++++++++++---------- jsonrpc/src/client/message_dispatcher.rs | 11 +++++--- jsonrpc/src/client/mod.rs | 2 +- 3 files changed, 38 insertions(+), 19 deletions(-) (limited to 'jsonrpc/src/client') 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 { @@ -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")); } -- cgit v1.2.3