#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
mod generated {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
pub use generated::*;
pub mod send_safe;
pub use send_safe::SendSafePtr;
use libc::c_char;
use std::ffi::CStr;
use std::str::Utf8Error;
pub unsafe fn c_str_to_string(ptr: *const c_char) -> Result<String, Utf8Error> {
if ptr.is_null() {
return Ok(String::new());
}
let c_str = CStr::from_ptr(ptr);
c_str.to_str().map(|s| s.to_string())
}
pub fn string_to_c_string(s: &str) -> *mut c_char {
std::ffi::CString::new(s).unwrap().into_raw()
}
pub unsafe fn free_c_string(ptr: *mut c_char) {
if !ptr.is_null() {
let _ = std::ffi::CString::from_raw(ptr);
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CallbackReturn {
Ok = 0,
Error = 1,
Progress = 3,
}
impl From<i32> for CallbackReturn {
fn from(value: i32) -> Self {
match value {
0 => CallbackReturn::Ok,
1 => CallbackReturn::Error,
3 => CallbackReturn::Progress,
_ => CallbackReturn::Error,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_c_str_conversion() {
let rust_str = "Hello, World!";
let c_str = string_to_c_string(rust_str);
unsafe {
let converted = c_str_to_string(c_str).unwrap();
assert_eq!(rust_str, converted);
free_c_string(c_str);
}
}
}