Skip to content

Commit

Permalink
Add rename-all=prefix:
Browse files Browse the repository at this point in the history
This adds a new rename-all annotation which allows setting a specific
prefix to all enum fields. For example:

```rust
/// cbindgen:rename-all=prefix:ERR_
enum Error {
   Bad = 0,
   VeryBad = 1
}
```

While in principle this can be added to the config file, it's primarily
useful for overrides on a case-by-case basis.
  • Loading branch information
jsgf committed Oct 4, 2024
1 parent 3ed9434 commit b4a40d8
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl EnumVariant {

let body_rule = enum_annotations
.parse_atom::<RenameRule>("rename-variant-name-fields")
.unwrap_or(config.enumeration.rename_variant_name_fields);
.unwrap_or(config.enumeration.rename_variant_name_fields.clone());

let body = match variant.fields {
syn::Fields::Unit => VariantBody::Empty(annotations),
Expand Down Expand Up @@ -556,7 +556,7 @@ impl Item for Enum {
let rules = self
.annotations
.parse_atom::<RenameRule>("rename-all")
.unwrap_or(config.enumeration.rename_variants);
.unwrap_or(config.enumeration.rename_variants.clone());

if let Some(r) = rules.not_none() {
self.variants = self
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Function {
let rules = self
.annotations
.parse_atom::<RenameRule>("rename-all")
.unwrap_or(config.function.rename_args);
.unwrap_or(config.function.rename_args.clone());

if let Some(r) = rules.not_none() {
let args = std::mem::take(&mut self.args);
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl Item for Struct {
let field_rules = self
.annotations
.parse_atom::<RenameRule>("rename-all")
.unwrap_or(config.structure.rename_fields);
.unwrap_or(config.structure.rename_fields.clone());

if let Some(o) = self.annotations.list("field-names") {
for (dest, src) in names.zip(o) {
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/ir/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Item for Union {
let rules = self
.annotations
.parse_atom::<RenameRule>("rename-all")
.unwrap_or(config.structure.rename_fields);
.unwrap_or(config.structure.rename_fields.clone());

if let Some(o) = self.annotations.list("field-names") {
let mut overriden_fields = Vec::new();
Expand Down
2 changes: 2 additions & 0 deletions src/bindgen/mangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl<'a> Mangler<'a> {
&self
.config
.rename_types
.clone()
.apply(&sub_path, IdentifierType::Type),
);
}
Expand All @@ -101,6 +102,7 @@ impl<'a> Mangler<'a> {
&self
.config
.rename_types
.clone()
.apply(primitive.to_repr_rust(), IdentifierType::Type),
);
}
Expand Down
12 changes: 10 additions & 2 deletions src/bindgen/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a> IdentifierType<'a> {
}

/// A rule to apply to an identifier when generating bindings.
#[derive(Debug, Clone, Copy, Default)]
#[derive(Debug, Clone, Default)]
pub enum RenameRule {
/// Do not apply any renaming. The default.
#[default]
Expand All @@ -50,6 +50,8 @@ pub enum RenameRule {
/// Converts the identifier to SCREAMING_SNAKE_CASE and prefixes enum variants
/// with the enum name.
QualifiedScreamingSnakeCase,
/// Adds a given prefix
Prefix(String),
}

impl RenameRule {
Expand All @@ -61,7 +63,7 @@ impl RenameRule {
}

/// Applies the rename rule to a string
pub fn apply<'a>(self, text: &'a str, context: IdentifierType) -> Cow<'a, str> {
pub fn apply<'a>(&self, text: &'a str, context: IdentifierType) -> Cow<'a, str> {
use heck::*;

if text.is_empty() {
Expand Down Expand Up @@ -90,6 +92,7 @@ impl RenameRule {
result.push_str(&RenameRule::ScreamingSnakeCase.apply(text, context));
result
}
RenameRule::Prefix(prefix) => prefix.to_owned() + text,
})
}
}
Expand All @@ -98,6 +101,9 @@ impl FromStr for RenameRule {
type Err = String;

fn from_str(s: &str) -> Result<RenameRule, Self::Err> {
const PREFIX: &str = "prefix:";
const PREFIX_LEN: usize = PREFIX.len();

match s {
"none" => Ok(RenameRule::None),
"None" => Ok(RenameRule::None),
Expand Down Expand Up @@ -132,6 +138,8 @@ impl FromStr for RenameRule {
"QualifiedScreamingSnakeCase" => Ok(RenameRule::QualifiedScreamingSnakeCase),
"qualified_screaming_snake_case" => Ok(RenameRule::QualifiedScreamingSnakeCase),

s if s.starts_with(PREFIX) => Ok(RenameRule::Prefix(s[PREFIX_LEN..].to_string())),

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

0 comments on commit b4a40d8

Please sign in to comment.