Skip to content

Commit bcb9166

Browse files
committed
test: check attribute/derive ordering
1 parent 35e7078 commit bcb9166

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

bindgen-tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ owo-colors.workspace = true
1313
prettyplease = { workspace = true, features = ["verbatim"] }
1414
proc-macro2.workspace = true
1515
regex.workspace = true
16+
serde.workspace = true
1617
shlex.workspace = true
1718
similar = { workspace = true, features = ["inline"] }
1819
syn.workspace = true

bindgen-tests/tests/expectations/tests/derive-and-attribute-order.rs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --no-layout-tests
2+
// bindgen-parse-callbacks: derive-transparent-serialize=color
3+
typedef struct {
4+
int red;
5+
int green;
6+
int blue;
7+
} color;

bindgen-tests/tests/parse_callbacks/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,27 @@ impl ParseCallbacks for WrapAsVariadicFn {
146146
}
147147
}
148148

149+
#[derive(Debug)]
150+
struct DeriveTransparentSerialize(String);
151+
152+
impl ParseCallbacks for DeriveTransparentSerialize {
153+
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
154+
if info.name == &self.0 {
155+
vec!["serde::Serialize".to_owned()]
156+
} else {
157+
vec![]
158+
}
159+
}
160+
161+
fn add_attributes(&self, info: &AttributeInfo<'_>) -> Vec<String> {
162+
if info.name == &self.0 {
163+
vec!["#[serde(transparent)]".to_owned()]
164+
} else {
165+
vec![]
166+
}
167+
}
168+
}
169+
149170
pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
150171
match cb {
151172
"enum-variant-rename" => Box::new(EnumVariantRename),
@@ -155,7 +176,11 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
155176
"wrap-as-variadic-fn" => Box::new(WrapAsVariadicFn),
156177
"type-visibility" => Box::new(TypeVisibility),
157178
call_back => {
158-
if let Some(prefix) =
179+
if let Some(name) =
180+
call_back.strip_prefix("derive-transparent-serialize=")
181+
{
182+
Box::new(DeriveTransparentSerialize(name.to_owned()))
183+
} else if let Some(prefix) =
159184
call_back.strip_prefix("remove-function-prefix-")
160185
{
161186
let lnopc = RemovePrefixParseCallback::new(prefix);

0 commit comments

Comments
 (0)