diff options
author | HampusM <hampus@hampusmat.com> | 2023-04-15 18:26:29 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-05-09 19:51:02 +0200 |
commit | e762babd9e69400ccd178ba8946168640093eb63 (patch) | |
tree | e07a56940f0c4a3551c87afad80bb949969335c7 /src/tagged.rs | |
parent | da509c366972ac6d423f95732cd3d319a2265841 (diff) |
feat: add deserialization
Diffstat (limited to 'src/tagged.rs')
-rw-r--r-- | src/tagged.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/tagged.rs b/src/tagged.rs new file mode 100644 index 0000000..19ae03b --- /dev/null +++ b/src/tagged.rs @@ -0,0 +1,62 @@ +//! Tagged element. + +use std::borrow::Cow; +use std::str::Utf8Error; + +use quick_xml::events::BytesStart; + +use crate::attribute::Iter as AttributeIter; + +/// The start tag of a tagged element. +/// +/// The `<xyz foo="bar">` in `<xyz foo="bar">Hello</xyz>` +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TagStart<'a> +{ + inner: BytesStart<'a>, +} + +impl<'a> TagStart<'a> +{ + /// Returns a new `TagStart`. + pub fn new(name: impl Into<Cow<'a, str>>) -> Self + { + Self { + inner: BytesStart::new(name), + } + } + + /// Returns the tag name. + #[must_use] + pub fn name(&self) -> &[u8] + { + let name_length = self.inner.name().as_ref().len(); + + &self.inner.as_ref()[..name_length] + } + + /// Returns the tag name in UTF-8. + /// + /// # Errors + /// Returns `Err` if the name is not valid UTF-8. + pub fn name_utf8(&self) -> Result<&str, Utf8Error> + { + std::str::from_utf8(self.name()) + } + + /// Returns the tag attributes. + #[must_use] + pub fn attributes(&'a self) -> AttributeIter<'a> + { + AttributeIter::new(self.inner.attributes()) + } +} + +// Crate-local functions +impl<'a> TagStart<'a> +{ + pub(crate) fn from_inner(inner: BytesStart<'a>) -> Self + { + Self { inner } + } +} |