rumtk_hl7_v2/hl7_v2_scripting.rs
1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025 Luis M. Santos, M.D.
5 * Copyright (C) 2025 MedicalMasses L.L.C.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22pub mod python {
23 use rumtk_core::core::RUMResult;
24 use rumtk_core::scripting::python_utils::RUMPython;
25 use rumtk_core::scripting::python_utils::{py_extract_any, py_new_args, py_push_arg};
26 use rumtk_core::strings::RUMString;
27 use rumtk_core::{rumtk_python_exec, rumtk_python_exec_module};
28
29 use crate::hl7_v2_parser::v2_parser::V2Message;
30
31 const EXPECTED_PROCESSOR_FUNCTION_NAME: &str = "process";
32
33 ///
34 /// Takes a [V2Message] and pass it to a Python module for processing. After processing, we expect to
35 /// receive a [V2Message] result with the modified copy of the message.
36 ///
37 ///
38 pub fn process_message(module_path: &RUMString, message: &V2Message) -> RUMResult<V2Message> {
39 let closure = |py: RUMPython| -> RUMResult<V2Message> {
40 let mut args = py_new_args(py);
41 py_push_arg(py, &mut args, message)?;
42
43 let result = rumtk_python_exec_module!(
44 py,
45 &module_path,
46 EXPECTED_PROCESSOR_FUNCTION_NAME,
47 &args
48 );
49 let val: V2Message = py_extract_any(py, &result)?;
50 Ok(val)
51 };
52
53 rumtk_python_exec!(closure)
54 }
55}
56
57pub mod python_macros {
58 ///
59 /// Macro for processing V2 message via a Python module loaded from disk.
60 ///This interface attempts to cache the module to avoid repeated loads of the module.
61 ///
62 /// ## Examples
63 ///
64 /// ```
65 /// use rumtk_hl7_v2::{rumtk_v2_parse_message};
66 /// let pattern = "MSH1.1";
67 /// let hl7_v2_message = "MSH|^~\\&|NISTEHRAPP|NISTEHRFAC|NISTIISAPP|NISTIISFAC|20150625072816.601-0500||VXU^V04^VXU_V04|NIST-IZ-AD-10.1_Send_V04_Z22|P|2.5.1|||ER|AL|||||Z22^CDCPHINVS|NISTEHRFAC|NISTIISFAC\n";
68 /// let message = rumtk_v2_parse_message!(&hl7_v2_message).unwrap();
69 /// ```
70 ///
71 #[macro_export]
72 macro_rules! rumtk_v2_python_exec {
73 ( $mod_path:expr, $message:expr ) => {{
74 use $crate::hl7_v2_scripting::python::process_message;
75
76 process_message($mod_path, $message)
77 }};
78 }
79}