rubyx-py 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ use crate::python_api::PythonApi;
2
+ use crate::python_guard::PyGuard;
3
+ use crate::ruby_helpers::runtime_error;
4
+ use crate::rubyx_object::RubyxObject;
5
+ use magnus::{Error, IntoValue, Ruby, Value};
6
+
7
+ pub(crate) fn rubyx_import(module_name: String) -> Result<Value, Error> {
8
+ let api = crate::api();
9
+
10
+ // Lock python gil
11
+ let gil = api.ensure_gil();
12
+
13
+ let result = (|| -> Result<Value, Error> {
14
+ let ruby = Ruby::get()
15
+ .map_err(|e| Error::new(runtime_error(), format!("Ruby VM unavailable: {e}")))?;
16
+ let module = match api.import_module(module_name.as_str()) {
17
+ Ok(module) => module,
18
+ Err(msg) => {
19
+ if let Some(err) = PythonApi::extract_exception(api) {
20
+ return Err(Error::from(err));
21
+ }
22
+ return Err(Error::new(runtime_error(), msg));
23
+ }
24
+ };
25
+ let py_module_guard = PyGuard::new(module, api)
26
+ .ok_or_else(|| Error::new(runtime_error(), "Python returned null result"))?;
27
+ let wrapper = RubyxObject::new(py_module_guard.ptr(), api)
28
+ .ok_or_else(|| Error::new(runtime_error(), "Failed to create RubyxObject"))?;
29
+ Ok(wrapper.into_value_with(&ruby))
30
+ })();
31
+
32
+ api.release_gil(gil);
33
+ result
34
+ }