| 80 | /// ``` |
| 81 | #[no_mangle] |
| 82 | pub unsafe extern "C" fn graphlite_open( |
| 83 | path: *const c_char, |
| 84 | error_out: *mut GraphLiteErrorCode, |
| 85 | ) -> *mut GraphLiteDB { |
| 86 | let result = panic::catch_unwind(AssertUnwindSafe(|| { |
| 87 | // Check for null pointer |
| 88 | if path.is_null() { |
| 89 | set_error(error_out, GraphLiteErrorCode::NullPointer); |
| 90 | return ptr::null_mut(); |
| 91 | } |
| 92 | |
| 93 | // Convert C string to Rust string |
| 94 | let c_str = unsafe { CStr::from_ptr(path) }; |
| 95 | let path_str = match c_str.to_str() { |
| 96 | Ok(s) => s, |
| 97 | Err(_) => { |
| 98 | set_error(error_out, GraphLiteErrorCode::InvalidUtf8); |
| 99 | return ptr::null_mut(); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | // Create QueryCoordinator |
| 104 | match QueryCoordinator::from_path(path_str) { |
| 105 | Ok(coordinator) => { |
| 106 | set_error(error_out, GraphLiteErrorCode::Success); |
| 107 | Box::into_raw(Box::new(GraphLiteDB { coordinator })) |
| 108 | } |
| 109 | Err(_) => { |
| 110 | set_error(error_out, GraphLiteErrorCode::DatabaseOpenError); |
| 111 | ptr::null_mut() |
| 112 | } |
| 113 | } |
| 114 | })); |
| 115 | |
| 116 | match result { |
| 117 | Ok(ptr) => ptr, |
| 118 | Err(_) => { |
| 119 | set_error(error_out, GraphLiteErrorCode::PanicError); |
| 120 | ptr::null_mut() |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// Create a simple session for the given username |
| 126 | /// |