filter api

This commit is contained in:
neuecc 2023-02-28 11:51:39 +09:00
parent 33d1012da4
commit f7aba95ada
4 changed files with 18 additions and 15 deletions

View File

@ -1,6 +1,4 @@
use std::{ use std::error::Error;
error::Error,
};
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
bindgen::Builder::default() bindgen::Builder::default()

View File

@ -13,6 +13,7 @@ pub struct Builder {
pub struct BindgenOptions { pub struct BindgenOptions {
pub input_bindgen_file: String, pub input_bindgen_file: String,
pub method_filter: fn(method_name: String) -> bool,
pub rust_method_type_path: String, pub rust_method_type_path: String,
pub rust_method_prefix: String, pub rust_method_prefix: String,
pub rust_file_header: String, pub rust_file_header: String,
@ -32,6 +33,7 @@ impl Builder {
Self { Self {
options: BindgenOptions { options: BindgenOptions {
input_bindgen_file: "".to_string(), input_bindgen_file: "".to_string(),
method_filter: |x| !x.starts_with("_"),
rust_method_type_path: "".to_string(), rust_method_type_path: "".to_string(),
rust_method_prefix: "".to_string(), rust_method_prefix: "".to_string(),
rust_file_header: "".to_string(), rust_file_header: "".to_string(),
@ -54,6 +56,12 @@ impl Builder {
self self
} }
/// Filter generate method callback, default is `!x.starts_with("_")`
pub fn method_filter(mut self, method_filter: fn(method_name: String) -> bool) -> Builder {
self.options.method_filter = method_filter;
self
}
/// add original extern call type prefix to rust wrapper, /// add original extern call type prefix to rust wrapper,
/// `return {rust_method_type_path}::foo()` /// `return {rust_method_type_path}::foo()`
pub fn rust_method_type_path<T: Into<String>>(mut self, rust_method_type_path: T) -> Builder { pub fn rust_method_type_path<T: Into<String>>(mut self, rust_method_type_path: T) -> Builder {

View File

@ -16,7 +16,7 @@ pub(crate) fn generate(options: &BindgenOptions) -> Result<(String, String), Box
let file_content = std::fs::read_to_string(path)?; let file_content = std::fs::read_to_string(path)?;
let file_ast = syn::parse_file(file_content.as_str())?; let file_ast = syn::parse_file(file_content.as_str())?;
let methods = collect_method(&file_ast); let methods = collect_method(&file_ast, options);
let aliases = collect_type_alias(&file_ast); let aliases = collect_type_alias(&file_ast);
let structs = collect_struct(&file_ast); let structs = collect_struct(&file_ast);

View File

@ -1,8 +1,8 @@
use crate::type_meta::*; use crate::{builder::BindgenOptions, type_meta::*};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use syn::{ForeignItem, Item, Pat, ReturnType}; use syn::{ForeignItem, Item, Pat, ReturnType};
pub fn collect_method(ast: &syn::File) -> Vec<ExternMethod> { pub fn collect_method(ast: &syn::File, options: &BindgenOptions) -> Vec<ExternMethod> {
let mut list: Vec<ExternMethod> = Vec::new(); let mut list: Vec<ExternMethod> = Vec::new();
for item in ast.items.iter() { for item in ast.items.iter() {
@ -42,15 +42,12 @@ pub fn collect_method(ast: &syn::File) -> Vec<ExternMethod> {
retrun_type = Some(rust_type); retrun_type = Some(rust_type);
} }
let t = ExternMethod { if method_name != "" && (&options.method_filter)(method_name.clone()) {
list.push(ExternMethod {
method_name: method_name.clone(), method_name: method_name.clone(),
parameters: parameters, parameters: parameters,
return_type: retrun_type, return_type: retrun_type,
}; });
// TODO:filter
if !(t.method_name.starts_with("_") || t.method_name == "") {
list.push(t.clone());
} }
} }
} }