{% if source_cfg -%}
#[cfg({{ source_cfg }})]
{% endif -%}
/// Render a `{{ enum_name }}` as its string representation
/// (the unit-variant name as serialized by serde — e.g. `"completed"`,
/// without surrounding JSON quotes).
/// # 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_string(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();
}
let rendered = with_handle::<{{ qualified }}, _>(handle, |val| {
serde_json::to_value(val)
.ok()
.and_then(|v| v.as_str().map(str::to_owned))
.unwrap_or_default()
});
match rendered {
Err(error) => { set_handle_error(&error); std::ptr::null_mut() }
Ok(s) => match CString::new(s) {
Ok(cs) => cs.into_raw(),
Err(_) => {
set_last_error(1, "{{ enum_name }} variant contained interior NUL byte");
std::ptr::null_mut()
}
}
}
})
}