From 4a055284153d933fa996f1c681407b4d079ce464 Mon Sep 17 00:00:00 2001 From: Thales Fragoso Date: Fri, 25 Apr 2025 00:30:31 -0300 Subject: [PATCH] Add functions to extract and parse Msgs from Records Signed-off-by: Thales Fragoso --- rhai-rusp/src/lib.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/rhai-rusp/src/lib.rs b/rhai-rusp/src/lib.rs index e07cb70..c2cb5b5 100644 --- a/rhai-rusp/src/lib.rs +++ b/rhai-rusp/src/lib.rs @@ -8,7 +8,7 @@ use rhai::{ FuncRegistration, ImmutableString, Module, NativeCallContext, PluginFunc, RhaiResult, TypeId, }, - Array, Map, Variant, + Array, Blob, Map, Variant, }; use rusp_lib::usp::{Body, Msg}; use rusp_lib::usp_builder; @@ -3278,6 +3278,34 @@ pub mod rhai_rusp { Ok(()) } + /// Extracts the [`Record`]'s payload. Returning [`Dynamic::UNIT`] for Records without payloads + /// + /// To avoid clones, this function leaves the used [`Record`] with an empty payload + #[rhai_fn(global, name = "extract_msg")] + pub fn record_extract_msg(record: &mut Record) -> Dynamic { + match &mut record.record_type { + OneOfrecord_type::session_context(ctx) => { + let payload = ctx.payload_flatten(); + Dynamic::from_blob(mem::take(payload)) + } + OneOfrecord_type::no_session_context(ctx) => { + Dynamic::from_blob(mem::take(&mut ctx.payload)) + } + _ => Dynamic::UNIT, + } + } + + /// Parses a [`Msg`] from the provided Protobuf bytes + /// + /// # Errors + /// + /// This function will return `Err` when the deserialization of the structure from the Protobuf + /// format fails + #[rhai_fn(global, name = "parse_msg", return_raw)] + pub fn parse_msg_protobuf(protobuf: &mut Blob) -> Result> { + try_decode_msg(protobuf).map_err(|e| e.to_string().into()) + } + /// Load a [`Msg`] from a Protobuf file /// /// # Errors