From 0c87a9ec25a45efc9b6b5ab7883cd19ded483909 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Wed, 16 Aug 2023 15:58:48 +0200 Subject: feat: impl IntoIterator for AttributeMap Making this change made me realize that adding an `impl IntoIterator for T` can be a breaking change if `impl IntoIterator for &T` already exists. See also the cargo-semver-checks issue[1] I filed about that. [1]: https://github.com/obi1kenobi/cargo-semver-checks/issues/518 --- src/attr.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src') diff --git a/src/attr.rs b/src/attr.rs index 9e4c984..a56eb95 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -41,6 +41,19 @@ pub struct Attribute<'a, O> { map_val: &'a AttrInternal, } +/// An owned HTML attribute. +#[derive(Debug, PartialEq, Eq)] +pub struct AttributeOwned { + /// The attribute name. + pub name: String, + /// The attribute value. + pub value: String, + /// The start offset of the attribute name. + pub name_offset: O, + /// The start offset of the attribute value. + pub value_offset: O, // TODO: wrap this in an Option once we can recognize the empty attribute syntax +} + impl AttributeMap { /// Returns the attribute with the given name. pub fn get(&self, name: &str) -> Option> { @@ -82,6 +95,33 @@ impl Index<&str> for AttributeMap { } } +impl IntoIterator for AttributeMap { + type Item = AttributeOwned; + + type IntoIter = AttrIntoIter; + + fn into_iter(self) -> Self::IntoIter { + AttrIntoIter(self.inner.into_iter()) + } +} + +/// A consuming iterator over the attributes of an [`AttributeMap`]. +pub struct AttrIntoIter(btree_map::IntoIter>); + +impl Iterator for AttrIntoIter { + type Item = AttributeOwned; + + fn next(&mut self) -> Option { + let (name, map_val) = self.0.next()?; + Some(AttributeOwned { + name, + value: map_val.value, + name_offset: map_val.name_offset, + value_offset: map_val.value_offset, + }) + } +} + impl<'a, O> IntoIterator for &'a AttributeMap { type Item = Attribute<'a, O>; -- cgit v1.2.3