diff options
author | 2024-11-19 16:46:51 +0000 | |
---|---|---|
committer | 2024-11-19 16:46:51 +0000 | |
commit | 0175a2d3651fac3337ef256c26eddee4a6ccb008 (patch) | |
tree | 5b00496f625a475a424dbaf34d84ecb4ec72209d | |
parent | caf8b7506e1fb1ccd8e3f43c897a02db90397dde (diff) | |
download | peanuts-0175a2d3651fac3337ef256c26eddee4a6ccb008.tar.gz peanuts-0175a2d3651fac3337ef256c26eddee4a6ccb008.tar.bz2 peanuts-0175a2d3651fac3337ef256c26eddee4a6ccb008.zip |
implement write end tag
-rw-r--r-- | src/writer.rs | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/writer.rs b/src/writer.rs index 249ced5..21d5fe0 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -13,7 +13,7 @@ use crate::{ pub struct Writer<W> { inner: W, depth: Vec<Name>, - namespaces: Vec<HashSet<NamespaceDeclaration>>, + namespace_declarations: Vec<HashSet<NamespaceDeclaration>>, } impl<W: AsyncWrite + Unpin> Writer<W> { @@ -26,28 +26,36 @@ impl<W: AsyncWrite + Unpin> Writer<W> { } pub async fn write_end(&mut self) -> Result<(), Error> { - todo!() - // let e_tag; - // if let Some(name) = self.depth.pop() { - // if let Some(prefix) = name.namespace.prefix { - // e_tag = xml::ETag { - // name: xml::QName::PrefixedName(xml::PrefixedName { - // prefix: xml::Prefix::parse_full(&prefix)?, - // local_part: xml::LocalPart::parse_full(&name.name)?, - // }), - // }; - // e_tag.write(&mut self.inner).await?; - // Ok(()) - // } else { - // e_tag = xml::ETag { - // name: xml::QName::UnprefixedName(xml::UnprefixedName::parse_full(&name.name)?), - // }; - // e_tag.write(&mut self.inner).await?; - // Ok(()) - // } - // } else { - // return Err(Error::NotInElement("".to_string())); - // } + let e_tag; + if let Some(name) = &self.depth.pop() { + let namespace_declarations_stack: Vec<_> = + self.namespace_declarations.iter().flatten().collect(); + let namespace_declaration = namespace_declarations_stack + .iter() + .rfind(|namespace_declaration| namespace_declaration.namespace == name.namespace) + // will always be in this vector + .unwrap(); + let prefix = &namespace_declaration.prefix; + if let Some(prefix) = &prefix { + e_tag = xml::ETag { + name: xml::QName::PrefixedName(xml::PrefixedName { + prefix: xml::Prefix::parse_full(prefix)?, + local_part: xml::LocalPart::parse_full(&name.local_name)?, + }), + }; + } else { + e_tag = xml::ETag { + name: xml::QName::UnprefixedName(xml::UnprefixedName::parse_full( + &name.local_name, + )?), + }; + } + e_tag.write(&mut self.inner).await?; + self.namespace_declarations.pop(); + Ok(()) + } else { + return Err(Error::NotInElement("".to_string())); + } } } |