use crate::{Error, Result};
use std::ffi::CStr;
use super::ffi;
pub(crate) fn map_error(code: i32) -> Error {
match code {
ffi::RACK_AU_ERROR_GENERIC => Error::Other("Generic AudioUnit scanner error".to_string()),
ffi::RACK_AU_ERROR_NOT_FOUND => Error::PluginNotFound("AudioUnit not found".to_string()),
ffi::RACK_AU_ERROR_INVALID_PARAM => Error::Other("Invalid parameter".to_string()),
ffi::RACK_AU_ERROR_NOT_INITIALIZED => Error::NotInitialized,
_ => Error::from_os_status(code),
}
}
pub(crate) unsafe fn c_array_to_string(arr: &[i8], field_name: &str) -> Result<String> {
let bytes = std::slice::from_raw_parts(arr.as_ptr() as *const u8, arr.len());
let cstr = CStr::from_bytes_until_nul(bytes).map_err(|_| {
Error::Other(format!(
"{} not null-terminated within buffer (potential C++ bug)",
field_name
))
})?;
cstr.to_str()
.map_err(|e| Error::Other(format!("Invalid UTF-8 in {}: {}", field_name, e)))
.map(|s| s.to_string())
}