Skip to content

Commit

Permalink
simplify Node::attrs() using impl Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarshkukreti committed Oct 18, 2020
1 parent 2775d9f commit 2e311f8
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ impl<'a> Node<'a> {

/// Get an iterator over the names and values of attributes of the Element.
/// Returns an empty iterator for non Element nodes.
pub fn attrs(&self) -> Attrs<'a> {
pub fn attrs(&self) -> impl Iterator<Item = (&'a str, &'a str)> {
match *self.data() {
Data::Element(_, ref attrs) => Attrs {
inner: Some(attrs.iter()),
},
_ => Attrs { inner: None },
Data::Element(_, ref attrs) => &attrs,
_ => [].as_ref(),
}
.iter()
.map(|(name, value)| (name.local.as_ref(), value.as_ref()))
}

pub fn parent(&self) -> Option<Node<'a>> {
Expand Down Expand Up @@ -371,18 +371,3 @@ impl<'a> Iterator for Children<'a> {
}
}
}

pub struct Attrs<'a> {
inner: Option<::std::slice::Iter<'a, (QualName, StrTendril)>>,
}

impl<'a> Iterator for Attrs<'a> {
type Item = (&'a str, &'a str);

fn next(&mut self) -> Option<Self::Item> {
self.inner.as_mut().and_then(|it| {
it.next()
.map(|&(ref name, ref value)| (name.local.as_ref(), value.as_ref()))
})
}
}

0 comments on commit 2e311f8

Please sign in to comment.