summaryrefslogtreecommitdiff
path: root/codegen/formatting.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-04-07 20:20:43 +0200
committerHampusM <hampus@hampusmat.com>2023-04-07 20:22:07 +0200
commit966ebb03abd8ae5ed4f47f4b53c00222269a56b4 (patch)
tree24e3325f1657bef89005583e9650b5c8e57459df /codegen/formatting.rs
parentc7efbb55f075748d88e25735c6d7e85ba768fd15 (diff)
refactor: replace usage of opengl-registry-macros
Diffstat (limited to 'codegen/formatting.rs')
-rw-r--r--codegen/formatting.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/codegen/formatting.rs b/codegen/formatting.rs
new file mode 100644
index 0000000..c480be4
--- /dev/null
+++ b/codegen/formatting.rs
@@ -0,0 +1,54 @@
+use convert_case::{Case, Casing};
+use once_cell::sync::Lazy;
+use regex::{Captures, Regex};
+
+static C_PTR_RE: Lazy<Regex> =
+ Lazy::new(|| Regex::new(r"(const )?([a-zA-Z0-9_]+)\s?\*(.*)").unwrap());
+
+static REST_C_PTR_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(const)?\s?\*").unwrap());
+
+pub fn clean_param_name(param_name: &str) -> String
+{
+ match param_name {
+ "ref" => "reference",
+ "type" => "ty",
+ "in" => "inside",
+ "box" => "a_box",
+ name => name,
+ }
+ .to_case(Case::Snake)
+}
+
+pub fn fix_type(ty: &str) -> String
+{
+ let ty = ty.replace("struct", "");
+
+ let ty = c_ptr_to_rust_ptr(&ty);
+
+ ty.replace("void", "std::ffi::c_void")
+}
+
+pub fn c_ptr_to_rust_ptr(c_ptr: &str) -> String
+{
+ C_PTR_RE
+ .replace(c_ptr, |captures: &Captures| {
+ let const_or_mut = captures.get(1).map_or_else(|| "mut", |_| "const");
+
+ let type_name = captures.get(2).unwrap().as_str();
+
+ let rest = captures.get(3).unwrap();
+
+ let rest_ptr_matches = REST_C_PTR_RE.captures_iter(rest.as_str());
+
+ let rest_ptrs = rest_ptr_matches
+ .map(|capts| {
+ let const_or_mut = capts.get(1).map_or_else(|| "mut", |_| "const");
+
+ format!("*{const_or_mut} ")
+ })
+ .collect::<Vec<_>>();
+
+ format!("{}*{const_or_mut} {type_name}", rest_ptrs.join(" "))
+ })
+ .to_string()
+}