#![allow(non_camel_case_types)]
use std::ffi::{c_char, c_void};
#[rustfmt::skip]
mod generated;
pub use generated::*;
pub const OBAPI_OK: i32 = 0;
pub const OBAPI_ERR_EXCEPTION: i32 = -1;
pub const OBAPI_ERR_UNKNOWN: i32 = -2;
pub const OBAPI_ERR_WIRE: i32 = -3;
pub const OBAPI_ERR_INVALID: i32 = -4;
pub type obapi_dispatch_fn =
unsafe extern "C" fn(ctx: *mut c_void, op: u32, data: *const u8, len: usize);
#[repr(C)]
#[derive(Debug)]
pub struct obapi_vtable {
pub module: *const c_char,
pub role: *const c_char,
pub create: unsafe extern "C" fn(
domain: i32,
name: *const c_char,
name_len: usize,
ctx: *mut c_void,
dispatch: Option<obapi_dispatch_fn>,
out: *mut *mut c_void,
) -> i32,
pub send:
unsafe extern "C" fn(handle: *mut c_void, op: u32, data: *const u8, len: usize) -> i32,
pub destroy: unsafe extern "C" fn(handle: *mut c_void),
pub is_valid: unsafe extern "C" fn(handle: *mut c_void) -> bool,
pub wait_until_connected: Option<unsafe extern "C" fn(handle: *mut c_void) -> i32>,
pub set_name: Option<
unsafe extern "C" fn(handle: *mut c_void, name: *const c_char, name_len: usize) -> i32,
>,
}
unsafe impl Sync for obapi_vtable {}
unsafe extern "C" {
pub fn obapi_last_error(buf: *mut c_char, cap: usize) -> usize;
}
pub fn last_error() -> String {
let mut buf = vec![0u8; 512];
let n = unsafe { obapi_last_error(buf.as_mut_ptr().cast(), buf.len()) };
if n > buf.len() {
buf.resize(n, 0);
unsafe { obapi_last_error(buf.as_mut_ptr().cast(), buf.len()) };
}
buf.truncate(n);
String::from_utf8_lossy(&buf).into_owned()
}