Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rename-all=prefix: #1007

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe avoid these clones by:

let rules = self.annotations.parse_atom::<>();
let rules = rules.as_ref().unwrap_or(&config.structure.rename_fields);

But fine either way.


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
Loading