rsphp-build-tools 0.1.3

Tools to support building FFI bindings for PHP's Zend engine
Documentation
use bindgen::callbacks::{ItemInfo, ParseCallbacks};

/// Renames some anonymous type names from the names generated by bindgen
/// (e.g. `_zend_function__bindgen_ty_1`) to more understandable names
/// (e.g. `_zend_function__common`).
#[derive(Clone, Copy, Debug, Default)]
#[non_exhaustive]
pub struct AnonymousTypeNames;

impl AnonymousTypeNames {
    /// Create a new callbacks instance to process item names.
    #[must_use]
    pub fn new() -> Self {
        AnonymousTypeNames
    }
}

impl ParseCallbacks for AnonymousTypeNames {
    fn item_name(&self, info: ItemInfo<'_>) -> Option<String> {
        macro_rules! map {
            ( $( $orig:literal => $new:literal, )+ ) => {
                match info.name {
                    $( $orig => Some($new.to_owned()), )+
                    _ => None,
                }
            }
        }
        map! {
            "_zend_function__bindgen_ty_1" => "_zend_function__common",
            "_zend_array__bindgen_ty_1" => "_zend_array__flags",
            "_zend_array__bindgen_ty_2" => "_zend_array__data",
            "_zval_struct__bindgen_ty_1" => "_zval_u1",
            "_zval_struct__bindgen_ty_2" => "_zval_u2",
            "_zval_struct__bindgen_ty_1__bindgen_ty_1" => "_zval_u1_v",
            "_zval_struct__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1" => "_zval_u1_v_u",
            "_zend_refcounted_h__bindgen_ty_1" => "_zend_refcounted_h_u",
            "_zend_class_entry__bindgen_ty_1" => "_zend_class_entry_parent_info",
            "_zend_class_entry__bindgen_ty_2" => "_zend_class_entry_hook",
            "_zend_class_entry__bindgen_ty_3" => "_zend_class_entry_interfaces_info",
            "_zend_class_entry__bindgen_ty_4" => "_zend_class_entry_info",
        }
    }
}