#[inline]
pub(crate) fn guard(name: &'static str, body: impl FnOnce() -> i32) -> i32 {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(body)).unwrap_or_else(|_| {
log::error!("{name} panicked; reporting PF_Err_INTERNAL_STRUCT_DAMAGED to the plugin");
crate::sys::PF_Err_INTERNAL_STRUCT_DAMAGED
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn passes_results_through() {
assert_eq!(guard("ok", || 7), 7);
}
#[test]
fn reports_a_panic_as_an_error() {
let previous = std::panic::take_hook();
std::panic::set_hook(Box::new(|_| {}));
let err = guard("boom", || panic!("boom"));
std::panic::set_hook(previous);
assert_eq!(
i64::from(err),
i64::from(after_effects_sys::PF_Err_INTERNAL_STRUCT_DAMAGED)
);
}
}