Skip to content

Commit

Permalink
test: check attribute/derive ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Dec 4, 2024
1 parent 35e7078 commit bcb9166
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions bindgen-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ owo-colors.workspace = true
prettyplease = { workspace = true, features = ["verbatim"] }
proc-macro2.workspace = true
regex.workspace = true
serde.workspace = true
shlex.workspace = true
similar = { workspace = true, features = ["inline"] }
syn.workspace = true
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions bindgen-tests/tests/headers/derive-and-attribute-order.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// bindgen-flags: --no-layout-tests
// bindgen-parse-callbacks: derive-transparent-serialize=color
typedef struct {
int red;
int green;
int blue;
} color;
27 changes: 26 additions & 1 deletion bindgen-tests/tests/parse_callbacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ impl ParseCallbacks for WrapAsVariadicFn {
}
}

#[derive(Debug)]
struct DeriveTransparentSerialize(String);

impl ParseCallbacks for DeriveTransparentSerialize {
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
if info.name == &self.0 {
vec!["serde::Serialize".to_owned()]
} else {
vec![]
}
}

fn add_attributes(&self, info: &AttributeInfo<'_>) -> Vec<String> {
if info.name == &self.0 {
vec!["#[serde(transparent)]".to_owned()]
} else {
vec![]
}
}
}

pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
match cb {
"enum-variant-rename" => Box::new(EnumVariantRename),
Expand All @@ -155,7 +176,11 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
"wrap-as-variadic-fn" => Box::new(WrapAsVariadicFn),
"type-visibility" => Box::new(TypeVisibility),
call_back => {
if let Some(prefix) =
if let Some(name) =
call_back.strip_prefix("derive-transparent-serialize=")
{
Box::new(DeriveTransparentSerialize(name.to_owned()))
} else if let Some(prefix) =
call_back.strip_prefix("remove-function-prefix-")
{
let lnopc = RemovePrefixParseCallback::new(prefix);
Expand Down

0 comments on commit bcb9166

Please sign in to comment.