Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion rhai-rusp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Msg, Box<EvalAltResult>> {
try_decode_msg(protobuf).map_err(|e| e.to_string().into())
}

/// Load a [`Msg`] from a Protobuf file
///
/// # Errors
Expand Down