@@ -791,19 +791,12 @@ impl Comment {
791
791
}
792
792
}
793
793
794
- /// Get the number of children this comment node has.
795
- pub fn num_children ( & self ) -> c_uint {
796
- unsafe {
797
- clang_Comment_getNumChildren ( self . x )
798
- }
799
- }
800
-
801
- /// Get this comment's `idx`th child comment
802
- pub fn get_child ( & self , idx : c_uint ) -> Option < Comment > {
803
- if idx >= self . num_children ( ) {
804
- None
805
- } else {
806
- Some ( Comment { x : unsafe { clang_Comment_getChild ( self . x , idx) } } )
794
+ /// Get this comment's children comment
795
+ pub fn get_children ( & self ) -> CommentChildrenIterator {
796
+ CommentChildrenIterator {
797
+ parent : self . x ,
798
+ length : unsafe { clang_Comment_getNumChildren ( self . x ) } ,
799
+ index : 0
807
800
}
808
801
}
809
802
@@ -815,39 +808,63 @@ impl Comment {
815
808
}
816
809
}
817
810
818
- /// Given that this comment is an HTML start tag, get the number of HTML
819
- /// attributes it has.
820
- pub fn get_num_tag_attrs ( & self ) -> c_uint {
821
- unsafe {
822
- clang_HTMLStartTag_getNumAttrs ( self . x )
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
823
817
}
824
818
}
819
+ }
825
820
826
- /// Given that this comment is an HTML start tag, get the `idx`th
827
- /// attribute's name.
828
- pub fn get_tag_attr_name ( & self , idx : c_uint ) -> Option < String > {
829
- if idx >= self . get_num_tag_attrs ( ) {
830
- None
821
+ /// An iterator for a comment's children
822
+ pub struct CommentChildrenIterator {
823
+ parent : CXComment ,
824
+ length : c_uint ,
825
+ index : c_uint
826
+ }
827
+
828
+ impl Iterator for CommentChildrenIterator {
829
+ type Item = Comment ;
830
+ fn next ( & mut self ) -> Option < Comment > {
831
+ if self . index < self . length {
832
+ let idx = self . index ;
833
+ self . index += 1 ;
834
+ Some ( Comment { x : unsafe { clang_Comment_getChild ( self . parent , idx) } } )
831
835
} else {
832
- unsafe {
833
- Some ( String_ {
834
- x : clang_HTMLStartTag_getAttrName ( self . x , idx)
835
- } . to_string ( ) )
836
- }
836
+ None
837
837
}
838
838
}
839
+ }
839
840
840
- /// Given that this comment is an HTML start tag, get the `idx`th
841
- /// attribute's value.
842
- pub fn get_tag_attr_value ( & self , idx : c_uint ) -> Option < String > {
843
- if idx >= self . get_num_tag_attrs ( ) {
844
- None
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
+ } )
845
866
} else {
846
- unsafe {
847
- Some ( String_ {
848
- x : clang_HTMLStartTag_getAttrValue ( self . x , idx)
849
- } . to_string ( ) )
850
- }
867
+ None
851
868
}
852
869
}
853
870
}
0 commit comments