{% if source_cfg -%}
#[cfg({{ source_cfg }})]
{% endif -%}
/// Serialize a `{{ enum_name }}` to a JSON string. Returns null on failure.
/// # Safety
/// `handle` must be a valid, non-zero handle returned by a `{{ prefix }}` function.
/// The returned string must be freed with `{{ prefix }}_free_string`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn {{ prefix }}_{{ enum_snake }}_to_json(handle: AlefHandle) -> *mut c_char {
catch_ffi_panic(std::ptr::null_mut(), || {
clear_last_error();
if handle == 0 {
set_last_error(ALEF_INVALID_HANDLE_ERROR, "zero is not a valid handle");
return std::ptr::null_mut();
}
match with_handle::<{{ qualified }}, _>(handle, serde_json::to_string) {
Err(error) => { set_handle_error(&error); std::ptr::null_mut() }
Ok(Err(error)) => { set_last_error(2, &error.to_string()); std::ptr::null_mut() }
Ok(Ok(s)) => match CString::new(s) {
Ok(cs) => cs.into_raw(),
Err(e) => {
set_last_error(2, &e.to_string());
std::ptr::null_mut()
}
}
}
})
}