Skip to content

Commit 9e8bae7

Browse files
committed
Use iterators for comment attributes fix #166
1 parent 2b605fb commit 9e8bae7

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

src/clang.rs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -802,27 +802,12 @@ impl Comment {
802802
}
803803
}
804804

805-
/// Given that this comment is an HTML start tag, get the number of HTML
806-
/// attributes it has.
807-
pub fn get_num_tag_attrs(&self) -> c_uint {
808-
unsafe {
809-
clang_HTMLStartTag_getNumAttrs(self.x)
810-
}
811-
}
812-
813-
/// Given that this comment is an HTML start tag, get the `idx`th
814-
/// attribute's name.
815-
pub fn get_tag_attr_name(&self, idx: c_uint) -> String {
816-
unsafe {
817-
String_ { x: clang_HTMLStartTag_getAttrName(self.x, idx) }.to_string()
818-
}
819-
}
820-
821-
/// Given that this comment is an HTML start tag, get the `idx`th
822-
/// attribute's value.
823-
pub fn get_tag_attr_value(&self, idx: c_uint) -> String {
824-
unsafe {
825-
String_ { x: clang_HTMLStartTag_getAttrValue(self.x, idx) }.to_string()
805+
/// Given that this comment is an HTML start tag, get its attributes.
806+
pub fn get_tag_attrs(&self) -> CommentAttributesIntoIterator {
807+
CommentAttributesIntoIterator {
808+
x: self.x,
809+
length: unsafe { clang_HTMLStartTag_getNumAttrs(self.x) },
810+
index: 0
826811
}
827812
}
828813
}
@@ -848,6 +833,38 @@ impl Iterator for CommentChildrenIntoIterator {
848833
}
849834
}
850835

836+
/// An HTML start tag comment attribute
837+
pub struct CommentAttribute {
838+
/// HTML start tag attribute name
839+
pub name: String,
840+
/// HTML start tag attribute value
841+
pub value: String
842+
}
843+
844+
/// An iterator for a comment's attributes
845+
pub struct CommentAttributesIntoIterator {
846+
x: CXComment,
847+
length: c_uint,
848+
index: c_uint
849+
}
850+
851+
impl Iterator for CommentAttributesIntoIterator {
852+
type Item = CommentAttribute;
853+
fn next(&mut self) -> Option<CommentAttribute> {
854+
if self.index < self.length {
855+
let idx = self.index;
856+
self.index += 1;
857+
Some( CommentAttribute {
858+
name: String_ { x: unsafe { clang_HTMLStartTag_getAttrName(self.x, idx) } }.to_string(),
859+
value: String_ { x: unsafe { clang_HTMLStartTag_getAttrValue(self.x, idx) } }.to_string()
860+
})
861+
}
862+
else {
863+
None
864+
}
865+
}
866+
}
867+
851868
/// A source file.
852869
pub struct File {
853870
x: CXFile

src/ir/annotations.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,17 @@ impl Annotations {
133133
use clangll::CXComment_HTMLStartTag;
134134
if comment.kind() == CXComment_HTMLStartTag &&
135135
comment.get_tag_name() == "div" &&
136-
comment.get_num_tag_attrs() > 1 &&
137-
comment.get_tag_attr_name(0) == "rustbindgen" {
136+
comment.get_tag_attrs().next().map_or(false, |attr| attr.name == "rustbindgen") {
138137
*matched = true;
139-
for i in 0..comment.get_num_tag_attrs() {
140-
let value = comment.get_tag_attr_value(i);
141-
let name = comment.get_tag_attr_name(i);
142-
match name.as_str() {
138+
for attr in comment.get_tag_attrs() {
139+
match attr.name.as_str() {
143140
"opaque" => self.opaque = true,
144141
"hide" => self.hide = true,
145142
"nocopy" => self.disallow_copy = true,
146-
"replaces" => self.use_instead_of = Some(value),
147-
"private" => self.private_fields = Some(value != "false"),
143+
"replaces" => self.use_instead_of = Some(attr.value),
144+
"private" => self.private_fields = Some(attr.value != "false"),
148145
"accessor"
149-
=> self.accessor_kind = Some(parse_accessor(&value)),
146+
=> self.accessor_kind = Some(parse_accessor(&attr.value)),
150147
_ => {},
151148
}
152149
}

0 commit comments

Comments
 (0)