{% if source_cfg -%}
#[cfg({{ source_cfg }})]
{% endif -%}
/// Create a `{{ enum_name }}` from a JSON string. Returns 0 on failure.
/// # Safety
/// JSON string must be valid UTF-8 and null-terminated.
/// Returned handle must be freed with `{{ prefix }}_{{ enum_snake }}_free`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn {{ prefix }}_{{ enum_snake }}_from_json(json: *const c_char) -> AlefHandle {
catch_ffi_panic(0, || {
clear_last_error();
if json.is_null() {
set_last_error(1, "Null pointer passed for JSON string");
return 0;
}
// SAFETY: null check above guarantees json is a valid pointer; string is valid UTF-8 from caller.
let c_str = match unsafe { CStr::from_ptr(json) }.to_str() {
Ok(s) => s,
Err(_) => {
set_last_error(1, "Invalid UTF-8 in JSON string");
return 0;
}
};
match serde_json::from_str::<{{ qualified }}>(c_str) {
Ok(val) => match insert_handle(val) {
Ok(handle) => handle,
Err(error) => { set_handle_error(&error); 0 }
},
Err(e) => {
set_last_error(2, &e.to_string());
0
}
}
})
}