diff options
-rwxr-xr-x | src/clang.rs | 93 | ||||
-rw-r--r-- | src/ir/annotations.rs | 22 |
2 files changed, 64 insertions, 51 deletions
diff --git a/src/clang.rs b/src/clang.rs index 1bb219b0..b08bc086 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -791,19 +791,12 @@ impl Comment { } } - /// Get the number of children this comment node has. - pub fn num_children(&self) -> c_uint { - unsafe { - clang_Comment_getNumChildren(self.x) - } - } - - /// Get this comment's `idx`th child comment - pub fn get_child(&self, idx: c_uint) -> Option<Comment> { - if idx >= self.num_children() { - None - } else { - Some(Comment { x: unsafe { clang_Comment_getChild(self.x, idx) } }) + /// Get this comment's children comment + pub fn get_children(&self) -> CommentChildrenIterator { + CommentChildrenIterator { + parent: self.x, + length: unsafe { clang_Comment_getNumChildren(self.x) }, + index: 0 } } @@ -815,39 +808,63 @@ impl Comment { } } - /// Given that this comment is an HTML start tag, get the number of HTML - /// attributes it has. - pub fn get_num_tag_attrs(&self) -> c_uint { - unsafe { - clang_HTMLStartTag_getNumAttrs(self.x) + /// Given that this comment is an HTML start tag, get its attributes. + pub fn get_tag_attrs(&self) -> CommentAttributesIterator { + CommentAttributesIterator { + x: self.x, + length: unsafe { clang_HTMLStartTag_getNumAttrs(self.x) }, + index: 0 } } +} - /// Given that this comment is an HTML start tag, get the `idx`th - /// attribute's name. - pub fn get_tag_attr_name(&self, idx: c_uint) -> Option<String> { - if idx >= self.get_num_tag_attrs() { - None +/// An iterator for a comment's children +pub struct CommentChildrenIterator { + parent: CXComment, + length: c_uint, + index: c_uint +} + +impl Iterator for CommentChildrenIterator { + type Item = Comment; + fn next(&mut self) -> Option<Comment> { + if self.index < self.length { + let idx = self.index; + self.index += 1; + Some( Comment { x: unsafe { clang_Comment_getChild(self.parent, idx) } } ) } else { - unsafe { - Some(String_ { - x: clang_HTMLStartTag_getAttrName(self.x, idx) - }.to_string()) - } + None } } +} - /// Given that this comment is an HTML start tag, get the `idx`th - /// attribute's value. - pub fn get_tag_attr_value(&self, idx: c_uint) -> Option<String> { - if idx >= self.get_num_tag_attrs() { - None +/// An HTML start tag comment attribute +pub struct CommentAttribute { + /// HTML start tag attribute name + pub name: String, + /// HTML start tag attribute value + pub value: String +} + +/// An iterator for a comment's attributes +pub struct CommentAttributesIterator { + x: CXComment, + length: c_uint, + index: c_uint +} + +impl Iterator for CommentAttributesIterator { + type Item = CommentAttribute; + fn next(&mut self) -> Option<CommentAttribute> { + if self.index < self.length { + let idx = self.index; + self.index += 1; + Some( CommentAttribute { + name: String_ { x: unsafe { clang_HTMLStartTag_getAttrName(self.x, idx) } }.to_string(), + value: String_ { x: unsafe { clang_HTMLStartTag_getAttrValue(self.x, idx) } }.to_string() + }) } else { - unsafe { - Some(String_ { - x: clang_HTMLStartTag_getAttrValue(self.x, idx) - }.to_string()) - } + None } } } diff --git a/src/ir/annotations.rs b/src/ir/annotations.rs index 9b141372..0ceb676d 100644 --- a/src/ir/annotations.rs +++ b/src/ir/annotations.rs @@ -133,28 +133,24 @@ impl Annotations { use clangll::CXComment_HTMLStartTag; if comment.kind() == CXComment_HTMLStartTag && comment.get_tag_name() == "div" && - comment.get_num_tag_attrs() > 1 && - comment.get_tag_attr_name(0).as_ref().map(|s| s.as_str()) - == Some("rustbindgen") { + comment.get_tag_attrs().next().map_or(false, |attr| attr.name == "rustbindgen") { *matched = true; - for i in 0..comment.get_num_tag_attrs() { - let value_opt = comment.get_tag_attr_value(i); - match comment.get_tag_attr_name(i).unwrap().as_str() { + for attr in comment.get_tag_attrs() { + match attr.name.as_str() { "opaque" => self.opaque = true, "hide" => self.hide = true, "nocopy" => self.disallow_copy = true, - "replaces" => self.use_instead_of = value_opt, - "private" => self.private_fields = value_opt.map(|v| - v != "false"), - "accessor" => self.accessor_kind = value_opt.map(|v| - parse_accessor(&v)), + "replaces" => self.use_instead_of = Some(attr.value), + "private" => self.private_fields = Some(attr.value != "false"), + "accessor" + => self.accessor_kind = Some(parse_accessor(&attr.value)), _ => {}, } } } - for i in 0..comment.num_children() { - self.parse(&comment.get_child(i).unwrap(), matched); + for child in comment.get_children() { + self.parse(&child, matched); } } } |