From 0175a2d3651fac3337ef256c26eddee4a6ccb008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?cel=20=F0=9F=8C=B8?= Date: Tue, 19 Nov 2024 16:46:51 +0000 Subject: [PATCH] implement write end tag --- src/writer.rs | 54 +++++++++++++++++++++++++++++---------------------- 1 file 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 { inner: W, depth: Vec, - namespaces: Vec>, + namespace_declarations: Vec>, } impl Writer { @@ -26,28 +26,36 @@ impl Writer { } 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())); + } } }