Skip to content

Commit 452d020

Browse files
committed
Add prefix with conversion rename rule
The new rule adds a given prefix and converts the identifier to a specific case, The prefix is applied before the conversion. For example: - `prefix:Foo + CamelCase` applied to `bar` with `CamelCase` would result in `FooBar`. - `prefix:FOO_ + ScreamingSnakeCase` applied to `Bar` with `ScreamingSnakeCase` would result in `FOO_BAR`.
1 parent bb13187 commit 452d020

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/bindgen/rename.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ pub enum RenameRule {
5252
QualifiedScreamingSnakeCase,
5353
/// Adds a given prefix
5454
Prefix(String),
55+
/// Adds a given prefix and converts the identifier to a specific case,
56+
/// The prefix is applied before the conversion.
57+
/// For example:
58+
/// - `prefix:Foo + CamelCase` applied to `bar` with `CamelCase` would result in `FooBar`.
59+
/// - `prefix:FOO_ + ScreamingSnakeCase` applied to `Bar` with `ScreamingSnakeCase` would result in `FOO_BAR`.
60+
PrefixWithConversion(String, Box<Self>),
5561
}
5662

5763
impl RenameRule {
@@ -93,6 +99,10 @@ impl RenameRule {
9399
result
94100
}
95101
RenameRule::Prefix(prefix) => prefix.to_owned() + text,
102+
RenameRule::PrefixWithConversion(prefix, rule) => {
103+
let converted = rule.apply(text, context);
104+
prefix.to_owned() + &converted
105+
}
96106
})
97107
}
98108
}
@@ -138,7 +148,33 @@ impl FromStr for RenameRule {
138148
"QualifiedScreamingSnakeCase" => Ok(RenameRule::QualifiedScreamingSnakeCase),
139149
"qualified_screaming_snake_case" => Ok(RenameRule::QualifiedScreamingSnakeCase),
140150

141-
s if s.starts_with(PREFIX) => Ok(RenameRule::Prefix(s[PREFIX_LEN..].to_string())),
151+
s if s.starts_with(PREFIX) => {
152+
if s.contains('+') {
153+
let parts: Vec<&str> = s.split('+').collect();
154+
if parts.len() != 2 {
155+
return Err(format!(
156+
"Invalid RenameRule: '{s}', expected 'prefix:... + Conversion'."
157+
));
158+
}
159+
let prefix = parts[0][PREFIX_LEN..].trim().to_string();
160+
let convert = parts[1].trim();
161+
if convert.is_empty() {
162+
return Err(format!(
163+
"Invalid RenameRule: '{s}', no conversion specified."
164+
));
165+
}
166+
let convert_rule = RenameRule::from_str(convert)?;
167+
if let RenameRule::Prefix(_) = convert_rule {
168+
return Err(format!("Invalid conversion rule: '{convert}', Prefix with conversion must not be a prefix."));
169+
}
170+
Ok(RenameRule::PrefixWithConversion(
171+
prefix,
172+
Box::new(convert_rule),
173+
))
174+
} else {
175+
Ok(RenameRule::Prefix(s[PREFIX_LEN..].to_string()))
176+
}
177+
}
142178

143179
_ => Err(format!("Unrecognized RenameRule: '{s}'.")),
144180
}

tests/rust/rename.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ pub extern "C" fn root(
4545
f: F,
4646
) { }
4747

48+
#[repr(C)]
49+
enum AvCodecId {
50+
H264,
51+
H265,
52+
}

tests/rust/rename.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ prefix = "C_"
33

44
[export.rename]
55
"B" = "AwesomeB"
6+
"AvCodecId" = "prefix:AV_CID_ + ScreamingSnakeCase"

0 commit comments

Comments
 (0)