/// Convert a `{{ enum_name }}` serde wire value (C string) to its integer discriminant. Returns -1 on invalid input.
/// # Safety
/// Caller must ensure `ptr` is a valid pointer to a `c_char` or null.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn {{ prefix }}_{{ enum_snake }}_from_str(name: *const c_char) -> i32 {
    if name.is_null() {
        set_last_error(1, "Null pointer passed for enum name");
        return -1;
    }
    // SAFETY: null check above guarantees name is a valid pointer; string is valid UTF-8 from caller.
    let s = match unsafe { CStr::from_ptr(name) }.to_str() {
        Ok(s) => s,
        Err(_) => {
            set_last_error(1, "Invalid UTF-8 in enum name");
            return -1;
        }
    };
    match s {
{% for variant in variants %}        "{{ variant }}" => {{ loop.index0 }},
{% endfor %}        _ => {
            set_last_error(1, "Unknown {{ enum_name }} variant");
            -1
        }
    }
}
