Skip to content

Commit

Permalink
Refactor: Move to a new function to avoid copy-pasting
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Aug 25, 2023
1 parent 1a3f566 commit 81bf4ca
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
15 changes: 14 additions & 1 deletion src/bindgen/ir/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,27 @@ impl AnnotationSet {
self.must_use && config.language != Language::Cython
}

pub(crate) fn deprecated_node(&self, config: &Config) -> Option<String> {
pub(crate) fn deprecated_note(&self, config: &Config) -> Option<String> {
if config.language == Language::Cython {
return None;
}

self.deprecated.clone()
}

pub(crate) fn format_deprecated_note(
&self,
format_without_note: Option<&str>,
format_with_note: Option<&str>,
note: &str,
) -> Option<String> {
if note.is_empty() {
return format_without_note.map(|x| x.to_string());
}
format_with_note
.map(|format_with_note| format_with_note.replace("{}", format!("{:?}", note).as_str()))
}

pub fn load(attrs: &[syn::Attribute]) -> Result<AnnotationSet, String> {
let lines = attrs.get_comment_lines();
let lines: Vec<&str> = lines
Expand Down
20 changes: 10 additions & 10 deletions src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ impl Enum {
// If we need to specify size, then we have no choice but to create a typedef,
// so `config.style` is not respected.
write!(out, "enum");
if let Some(note) = self.annotations.deprecated_node(config) {
if let Some(note) = self.annotations.deprecated_note(config) {
if note.is_empty() {
if let Some(ref anno) = config.structure.deprecated {
write!(out, " {}", anno);
Expand Down Expand Up @@ -783,7 +783,7 @@ impl Enum {
out.write("typedef ");
}
out.write("enum");
if let Some(note) = self.annotations.deprecated_node(config) {
if let Some(note) = self.annotations.deprecated_note(config) {
if note.is_empty() {
if let Some(ref anno) = config.structure.deprecated {
write!(out, " {}", anno);
Expand Down Expand Up @@ -814,7 +814,7 @@ impl Enum {
}
}

if let Some(note) = self.annotations.deprecated_node(config) {
if let Some(note) = self.annotations.deprecated_note(config) {
if note.is_empty() {
if let Some(ref anno) = config.structure.deprecated {
write!(out, " {}", anno);
Expand Down Expand Up @@ -906,13 +906,13 @@ impl Enum {
}
}

if let Some(note) = self.annotations.deprecated_node(config) {
if note.is_empty() {
if let Some(ref anno) = config.structure.deprecated {
write!(out, " {}", anno);
}
} else if let Some(ref anno) = config.structure.deprecated_with_note {
write!(out, " {}", anno);
if let Some(note) = self.annotations.deprecated_note(config) {
if let Some(note) = self.annotations.format_deprecated_note(
config.enumeration.deprecated.as_deref(),
config.enumeration.deprecated_with_note.as_deref(),
&note,
) {
write!(out, " {} ", note);
}
}

Expand Down
20 changes: 8 additions & 12 deletions src/bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,13 @@ impl Source for Function {
write!(out, "{} ", anno);
}
}
if let Some(note) = func.annotations.deprecated_node(config) {
if note.is_empty() {
if let Some(ref anno) = config.function.deprecated {
write!(out, "{} ", anno);
}
} else if let Some(ref anno) = config.function.deprecated_with_note {
write!(
out,
"{} ",
anno.replace("{}", format!("{:?}", note).as_str())
);
if let Some(note) = func.annotations.deprecated_note(config) {
if let Some(note) = func.annotations.format_deprecated_note(
config.function.deprecated.as_deref(),
config.function.deprecated_with_note.as_deref(),
&note,
) {
write!(out, "{} ", note);
}
}
}
Expand Down Expand Up @@ -297,7 +293,7 @@ impl Source for Function {
out.new_line();
}
}
if let Some(note) = func.annotations.deprecated_node(config) {
if let Some(note) = func.annotations.deprecated_note(config) {
if note.is_empty() {
if let Some(ref anno) = config.function.deprecated {
write!(out, "{}", anno);
Expand Down
18 changes: 7 additions & 11 deletions src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,13 @@ impl Source for Struct {
write!(out, " {}", anno);
}
}
if let Some(note) = self.annotations.deprecated_node(config) {
if note.is_empty() {
if let Some(ref anno) = config.structure.deprecated {
write!(out, " {}", anno);
}
} else if let Some(ref anno) = config.structure.deprecated_with_note {
write!(
out,
" {}",
anno.replace("{}", format!("{:?}", note).as_str())
);
if let Some(note) = self.annotations.deprecated_note(config) {
if let Some(note) = self.annotations.format_deprecated_note(
config.structure.deprecated.as_deref(),
config.structure.deprecated_with_note.as_deref(),
&note,
) {
write!(out, " {}", note);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub trait SynAttributeHelpers {

fn find_deprecated_note(&self) -> Result<Option<String>, String> {
let attrs = self.attrs();
// #[deprecated(note = "")]
// #[deprecated = ""]
if let Some(note) = attrs.attr_name_value_lookup("deprecated") {
return Ok(Some(note));
}
Expand Down

0 comments on commit 81bf4ca

Please sign in to comment.