use std::path::PathBuf;
use anyhow::{Context, Result};
use ext_php_rs::describe::Description;
use libloading::os::unix::{Library, RTLD_LAZY, RTLD_LOCAL, Symbol};
#[allow(improper_ctypes_definitions)]
pub struct Ext {
#[allow(dead_code)]
ext_lib: Library,
describe_fn: Symbol<extern "C" fn() -> Description>,
}
impl Ext {
pub fn load(ext_path: PathBuf) -> Result<Self> {
#[cfg(target_os = "macos")]
let ext_lib =
unsafe { Library::open(Some(ext_path), RTLD_LAZY | RTLD_LOCAL | libc::RTLD_FIRST) }
.with_context(|| "Failed to load extension library")?;
#[cfg(not(target_os = "macos"))]
let ext_lib = unsafe { Library::open(Some(ext_path), RTLD_LAZY | RTLD_LOCAL) }
.with_context(|| "Failed to load extension library")?;
let describe_fn = unsafe {
ext_lib
.get(b"ext_php_rs_describe_module")
.with_context(|| "Failed to load describe function symbol from extension library")?
};
Ok(Self {
ext_lib,
describe_fn,
})
}
pub fn describe(&self) -> Description {
(self.describe_fn)()
}
}