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 3543efb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ quickcheck = "1.0"
quote = { version = "1", default-features = false }
regex = { version = "1.5.3", default-features = false }
rustc-hash = "2.1.0"
serde = { version = "1", features = ["derive"] }
shlex = "1"
similar = "2.2.1"
syn = "2.0"
Expand Down
1 change: 1 addition & 0 deletions bindgen-tests/tests/expectations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ edition.workspace = true
block.workspace = true
libloading.workspace = true
objc.workspace = true
serde.workspace = true

# Both of these sections need to be copied here from the workspace because
# Cargo currently does not allow overriding workspace settings in a member
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-uppercase-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(rename_all = \"UPPERCASE\")]".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-uppercase-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 3543efb

Please sign in to comment.