diff options
-rwxr-xr-x | src/clang.rs | 70 | ||||
-rw-r--r-- | src/ir/annotations.rs | 18 |
2 files changed, 44 insertions, 44 deletions
diff --git a/src/clang.rs b/src/clang.rs index a69bb5cf..b08bc086 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -808,39 +808,12 @@ 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 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 - } else { - unsafe { - Some(String_ { - x: clang_HTMLStartTag_getAttrName(self.x, idx) - }.to_string()) - } - } - } - - /// 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 - } else { - unsafe { - Some(String_ { - x: clang_HTMLStartTag_getAttrValue(self.x, idx) - }.to_string()) - } + /// 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 } } } @@ -865,6 +838,37 @@ impl Iterator for CommentChildrenIterator { } } +/// 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 { + None + } + } +} + /// A source file. pub struct File { x: CXFile diff --git a/src/ir/annotations.rs b/src/ir/annotations.rs index 44c0190c..0ceb676d 100644 --- a/src/ir/annotations.rs +++ b/src/ir/annotations.rs @@ -133,21 +133,17 @@ 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)), _ => {}, } } |