forked from rust-lang/rust-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
84 lines (76 loc) · 2.43 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use bindgen::callbacks::*;
#[derive(Debug)]
pub struct RemovePrefixParseCallback {
pub remove_prefix: Option<String>,
}
impl RemovePrefixParseCallback {
pub fn new(prefix: &str) -> Self {
RemovePrefixParseCallback {
remove_prefix: Some(prefix.to_string()),
}
}
}
impl ParseCallbacks for RemovePrefixParseCallback {
fn generated_name_override(&self, item_info: ItemInfo) -> Option<String> {
if let Some(prefix) = &self.remove_prefix {
let (item_name, expected_prefix, expected_suffix) = match item_info
{
ItemInfo::Function { name } => (name, "function_", "_name"),
ItemInfo::Var { name } => (name, "var_", "_name"),
};
if let Some(name) = item_name.strip_prefix(prefix) {
assert!(name.starts_with(expected_prefix));
assert!(name.ends_with(expected_suffix));
return Some(name.to_string());
}
}
None
}
}
#[derive(Debug)]
struct EnumVariantRename;
impl ParseCallbacks for EnumVariantRename {
fn enum_variant_name(
&self,
_enum_name: Option<&str>,
original_variant_name: &str,
_variant_value: EnumVariantValue,
) -> Option<String> {
Some(format!("RENAMED_{}", original_variant_name))
}
}
#[derive(Debug)]
struct BlocklistedTypeImplementsTrait;
impl ParseCallbacks for BlocklistedTypeImplementsTrait {
fn blocklisted_type_implements_trait(
&self,
_name: &str,
derive_trait: DeriveTrait,
) -> Option<ImplementsTrait> {
if derive_trait == DeriveTrait::Hash {
Some(ImplementsTrait::No)
} else {
Some(ImplementsTrait::Yes)
}
}
}
pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
match cb {
"enum-variant-rename" => Box::new(EnumVariantRename),
"blocklisted-type-implements-trait" => {
Box::new(BlocklistedTypeImplementsTrait)
}
call_back => {
if call_back.starts_with("remove-function-prefix-") {
let prefix = call_back
.split("remove-function-prefix-")
.last()
.to_owned();
let lnopc = RemovePrefixParseCallback::new(prefix.unwrap());
Box::new(lnopc)
} else {
panic!("Couldn't find name ParseCallbacks: {}", cb)
}
}
}
}