diff options
author | HampusM <hampus@hampusmat.com> | 2023-05-14 16:29:37 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-05-14 16:29:37 +0200 |
commit | 4d6664fb369607d42c269a8a50f26bfd3c3b8634 (patch) | |
tree | e1025e69d7d94f622e0e12cd1f279ec981baafcb | |
parent | 0f44f2f36c3fa594e8bf9c0b23b7b8041cc6d13e (diff) |
feat: add TagStart attribute functions
-rw-r--r-- | src/attribute.rs | 5 | ||||
-rw-r--r-- | src/tagged.rs | 44 |
2 files changed, 48 insertions, 1 deletions
diff --git a/src/attribute.rs b/src/attribute.rs index 4ae2142..3d17fe9 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -85,6 +85,11 @@ impl<'a> Attribute<'a> { Self { inner } } + + pub(crate) fn into_inner(self) -> QuickXMLAttribute<'a> + { + self.inner + } } /// Errors that can be raised when parsing [`Attribute`]s. diff --git a/src/tagged.rs b/src/tagged.rs index 0150697..1fca458 100644 --- a/src/tagged.rs +++ b/src/tagged.rs @@ -5,7 +5,7 @@ use std::str::Utf8Error; use quick_xml::events::BytesStart; -use crate::attribute::Iter as AttributeIter; +use crate::attribute::{Attribute, Error as AttributeError, Iter as AttributeIter}; /// The start tag of a tagged element. /// @@ -26,6 +26,19 @@ impl<'a> TagStart<'a> } } + /// Returns `Self` with the specified attributes. + #[must_use] + pub fn with_attributes<Attrs>(mut self, attrs: Attrs) -> Self + where + Attrs: IntoIterator<Item = Attribute<'a>>, + { + self.inner = self + .inner + .with_attributes(attrs.into_iter().map(Attribute::into_inner)); + + self + } + /// Returns the name. /// /// # Errors @@ -50,6 +63,26 @@ impl<'a> TagStart<'a> { AttributeIter::new(self.inner.attributes()) } + + /// Returns a attribute. + /// + /// # Errors + /// Returns `Err` if a invalid attribute is found. + pub fn get_attribute( + &self, + attr_name: &str, + ) -> Result<Option<Attribute>, TagStartError> + { + for attr_result in self.inner.attributes().with_checks(false) { + let attr = attr_result.map_err(AttributeError::from)?; + + if attr.key.as_ref() == attr_name.as_bytes() { + return Ok(Some(Attribute::from_inner(attr))); + } + } + + Ok(None) + } } // Crate-local functions @@ -60,3 +93,12 @@ impl<'a> TagStart<'a> Self { inner } } } + +/// `TagStart` error. +#[derive(Debug, thiserror::Error)] +pub enum TagStartError +{ + /// Invalid attribute. + #[error("Invalid attribute")] + InvalidAttribute(#[from] AttributeError), +} |