diff options
Diffstat (limited to 'src/tag.rs')
-rw-r--r-- | src/tag.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/tag.rs b/src/tag.rs index e325b1f..0a97061 100644 --- a/src/tag.rs +++ b/src/tag.rs @@ -108,6 +108,36 @@ impl Tag { pub fn set_inner_text<S: Into<String>>(&mut self, txt: S) { self.children = vec![Node::Text(txt.into())]; } + + pub fn child_tags(&self) -> TagIterator { + TagIterator { + inner: self.children.iter(), + } + } + + pub fn child_tags_mut(&mut self) -> TagIteratorMut { + TagIteratorMut { + inner: self.children.iter_mut(), + } + } + + pub fn has_tag(&self, tag: &str) -> bool { + self.child_tags().any(|t| t.name == tag) + } + + pub fn by_tag_mut<'a>(&'a mut self, looking: &str) -> Option<&'a mut Tag> { + for tag in self.child_tags_mut() { + if tag.name == looking { + return Some(tag); + } + + if let Some(found) = tag.by_tag_mut(looking) { + return Some(found); + } + } + + None + } } impl fmt::Display for Tag { @@ -138,6 +168,42 @@ impl fmt::Display for Tag { } } +pub struct TagIterator<'a> { + pub(crate) inner: std::slice::Iter<'a, Node>, +} + +impl<'a> Iterator for TagIterator<'a> { + type Item = &'a Tag; + + fn next(&mut self) -> Option<Self::Item> { + loop { + match self.inner.next() { + None => break None, + Some(Node::Tag(ref tag)) => break Some(tag), + Some(_) => continue, + } + } + } +} + +pub struct TagIteratorMut<'a> { + pub(crate) inner: std::slice::IterMut<'a, Node>, +} + +impl<'a> Iterator for TagIteratorMut<'a> { + type Item = &'a mut Tag; + + fn next(&mut self) -> Option<Self::Item> { + loop { + match self.inner.next() { + None => break None, + Some(Node::Tag(ref mut tag)) => break Some(tag), + Some(_) => continue, + } + } + } +} + #[cfg(test)] mod test { use crate::Tag; |