mirror of
https://github.com/Sarsoo/csbindgen.git
synced 2024-12-22 22:46:26 +00:00
filter api
This commit is contained in:
parent
33d1012da4
commit
f7aba95ada
@ -1,6 +1,4 @@
|
||||
use std::{
|
||||
error::Error,
|
||||
};
|
||||
use std::error::Error;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
bindgen::Builder::default()
|
||||
|
@ -13,6 +13,7 @@ pub struct Builder {
|
||||
|
||||
pub struct BindgenOptions {
|
||||
pub input_bindgen_file: String,
|
||||
pub method_filter: fn(method_name: String) -> bool,
|
||||
pub rust_method_type_path: String,
|
||||
pub rust_method_prefix: String,
|
||||
pub rust_file_header: String,
|
||||
@ -32,6 +33,7 @@ impl Builder {
|
||||
Self {
|
||||
options: BindgenOptions {
|
||||
input_bindgen_file: "".to_string(),
|
||||
method_filter: |x| !x.starts_with("_"),
|
||||
rust_method_type_path: "".to_string(),
|
||||
rust_method_prefix: "".to_string(),
|
||||
rust_file_header: "".to_string(),
|
||||
@ -54,6 +56,12 @@ impl Builder {
|
||||
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,
|
||||
/// `return {rust_method_type_path}::foo()`
|
||||
pub fn rust_method_type_path<T: Into<String>>(mut self, rust_method_type_path: T) -> Builder {
|
||||
|
@ -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_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 structs = collect_struct(&file_ast);
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
use crate::type_meta::*;
|
||||
use crate::{builder::BindgenOptions, type_meta::*};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
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();
|
||||
|
||||
for item in ast.items.iter() {
|
||||
@ -42,15 +42,12 @@ pub fn collect_method(ast: &syn::File) -> Vec<ExternMethod> {
|
||||
retrun_type = Some(rust_type);
|
||||
}
|
||||
|
||||
let t = ExternMethod {
|
||||
method_name: method_name.clone(),
|
||||
parameters: parameters,
|
||||
return_type: retrun_type,
|
||||
};
|
||||
|
||||
// TODO:filter
|
||||
if !(t.method_name.starts_with("_") || t.method_name == "") {
|
||||
list.push(t.clone());
|
||||
if method_name != "" && (&options.method_filter)(method_name.clone()) {
|
||||
list.push(ExternMethod {
|
||||
method_name: method_name.clone(),
|
||||
parameters: parameters,
|
||||
return_type: retrun_type,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user