Skip to content

Commit 91eb86d

Browse files
committed
Use iterators for comment attributes fix #166
1 parent 2744b23 commit 91eb86d

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

src/clang.rs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -808,39 +808,12 @@ impl Comment {
808808
}
809809
}
810810

811-
/// Given that this comment is an HTML start tag, get the number of HTML
812-
/// attributes it has.
813-
pub fn get_num_tag_attrs(&self) -> c_uint {
814-
unsafe {
815-
clang_HTMLStartTag_getNumAttrs(self.x)
816-
}
817-
}
818-
819-
/// Given that this comment is an HTML start tag, get the `idx`th
820-
/// attribute's name.
821-
pub fn get_tag_attr_name(&self, idx: c_uint) -> Option<String> {
822-
if idx >= self.get_num_tag_attrs() {
823-
None
824-
} else {
825-
unsafe {
826-
Some(String_ {
827-
x: clang_HTMLStartTag_getAttrName(self.x, idx)
828-
}.to_string())
829-
}
830-
}
831-
}
832-
833-
/// Given that this comment is an HTML start tag, get the `idx`th
834-
/// attribute's value.
835-
pub fn get_tag_attr_value(&self, idx: c_uint) -> Option<String> {
836-
if idx >= self.get_num_tag_attrs() {
837-
None
838-
} else {
839-
unsafe {
840-
Some(String_ {
841-
x: clang_HTMLStartTag_getAttrValue(self.x, idx)
842-
}.to_string())
843-
}
811+
/// Given that this comment is an HTML start tag, get its attributes.
812+
pub fn get_tag_attrs(&self) -> CommentAttributesIterator {
813+
CommentAttributesIterator {
814+
x: self.x,
815+
length: unsafe { clang_HTMLStartTag_getNumAttrs(self.x) },
816+
index: 0
844817
}
845818
}
846819
}
@@ -865,6 +838,37 @@ impl Iterator for CommentChildrenIterator {
865838
}
866839
}
867840

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

src/ir/annotations.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +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).as_ref().map(|s| s.as_str())
138-
== Some("rustbindgen") {
136+
comment.get_tag_attrs().next().map_or(false, |attr| attr.name == "rustbindgen") {
139137
*matched = true;
140-
for i in 0..comment.get_num_tag_attrs() {
141-
let value_opt = comment.get_tag_attr_value(i);
142-
match comment.get_tag_attr_name(i).unwrap().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 = value_opt,
147-
"private" => self.private_fields = value_opt.map(|v|
148-
v != "false"),
149-
"accessor" => self.accessor_kind = value_opt.map(|v|
150-
parse_accessor(&v)),
143+
"replaces" => self.use_instead_of = Some(attr.value),
144+
"private" => self.private_fields = Some(attr.value != "false"),
145+
"accessor"
146+
=> self.accessor_kind = Some(parse_accessor(&attr.value)),
151147
_ => {},
152148
}
153149
}

0 commit comments

Comments
 (0)