From f3bf8578050b6bf19ebe9ebaa49aa8e3b8641c2d Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 8 Sep 2024 01:12:11 +0200 Subject: make constants not start with GL_ --- codegen/bindings.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/codegen/bindings.rs b/codegen/bindings.rs index d6b7e0d..5fdfaf3 100644 --- a/codegen/bindings.rs +++ b/codegen/bindings.rs @@ -1,6 +1,7 @@ use std::error::Error; use std::path::Path; +use bindgen::callbacks::ParseCallbacks; use bindgen::{CodegenConfig, MacroTypeVariation}; pub fn generate_bindings(dest_file: &Path) -> Result<(), Box> @@ -16,9 +17,56 @@ pub fn generate_bindings(dest_file: &Path) -> Result<(), Box> .allowlist_var("GL_.*") .blocklist_item("GL_Z4.*") .blocklist_item("GL_Z6.*") + .parse_callbacks(Box::new(Callbacks)) .generate()?; bindings.write_to_file(dest_file)?; Ok(()) } + +#[derive(Debug)] +struct Callbacks; + +impl ParseCallbacks for Callbacks +{ + fn item_name(&self, original_item_name: &str) -> Option + { + let stripped = original_item_name.strip_prefix("GL_")?; + + if stripped.starts_with(|character: char| character.is_ascii_digit()) { + let digit_cnt = stripped + .chars() + .filter(|character| character.is_ascii_digit()) + .count(); + + let digits = stripped + .chars() + .take_while(|character| character.is_ascii_digit()) + .map(|character| match character { + '0' => "zero", + '1' => "one", + '2' => "two", + '3' => "three", + '4' => "four", + '5' => "five", + '6' => "six", + '7' => "seven", + '8' => "eight", + '9' => "nine", + _ => unreachable!(), + }); + + return Some( + digits + .map(|digit| digit.chars()) + .flatten() + .map(|character| character.to_ascii_uppercase()) + .chain(stripped.chars().skip(digit_cnt)) + .collect::(), + ); + } + + Some(stripped.to_string()) + } +} -- cgit v1.2.3-18-g5258