From 3f0f6fd34e8ade3321a8a09bc9c0229ede95daaf Mon Sep 17 00:00:00 2001 From: WofWca Date: Tue, 18 Mar 2025 16:30:00 +0400 Subject: [PATCH] feat: add `RpcSession::handle_incoming_parsed` ...and `RpcSession::process_incoming_parsed`. Use case: in the Tauri version of Delta Chat, messages are already serialized by Tauri, so we don't need to re-serialize them. For reference, see https://github.com/deltachat/deltachat-desktop/pull/4810. --- CHANGELOG.md | 5 +++++ yerpc/src/requests.rs | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3243e44..ec0a600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. +## Unreleased + +- Add `RpcSession::handle_incoming_parsed` + and `RpcSession::process_incoming_parsed` + ## 0.6.0 - 2024-07-06 - ts client: fix memory leak diff --git a/yerpc/src/requests.rs b/yerpc/src/requests.rs index 4616882..a34edf7 100644 --- a/yerpc/src/requests.rs +++ b/yerpc/src/requests.rs @@ -56,6 +56,8 @@ impl RpcSession { /// /// Handles incoming requests and notifications, /// returns a response if any. + /// + /// See also [`process_incoming_parsed`]. pub async fn process_incoming(&self, input: &str) -> Option { let message: Message = match serde_json::from_str(input) { Ok(message) => message, @@ -66,7 +68,11 @@ impl RpcSession { ))); } }; - + self.process_incoming_parsed(message).await + } + /// Same as [`process_incoming`], but accepts a parsed [`Message`] + /// instead of a string. + pub async fn process_incoming_parsed(&self, message: Message) -> Option { match message { Message::Request(request) => { let params = request.params.map(Params::into_value).unwrap_or_default(); @@ -101,11 +107,20 @@ impl RpcSession { /// Blocks until request handler finishes. /// Spawn a task if you want to run the request handler /// concurrently. + /// + /// See also [`handle_incoming_parsed`]. pub async fn handle_incoming(&self, input: &str) { if let Some(response) = self.process_incoming(input).await { let _ = self.client.tx(response).await; } } + /// Same as [`handle_incoming`], but accepts a parsed [`Message`] + /// instead of a string. + pub async fn handle_incoming_parsed(&self, message: Message) { + if let Some(response) = self.process_incoming_parsed(message).await { + let _ = self.client.tx(response).await; + } + } } #[derive(Clone)]