diff options
Diffstat (limited to 'src/writer.rs')
-rw-r--r-- | src/writer.rs | 62 |
1 files changed, 49 insertions, 13 deletions
diff --git a/src/writer.rs b/src/writer.rs index e319fdc..8b45869 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -93,20 +93,38 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { } pub async fn write_empty(&mut self, element: &Element) -> Result<()> { - let namespace_declarations_stack: Vec<_> = self + let mut namespace_declarations_stack: Vec<_> = self .namespace_declarations .iter() .flatten() - .chain(&element.namespace_declarations) + .chain(&element.namespace_declaration_overrides) .collect(); + let mut namespace_declarations = element.namespace_declaration_overrides.clone(); + + let default_namespace_declaration; let prefix; if let Some(namespace) = &element.name.namespace { - let name_namespace_declaration = namespace_declarations_stack + if let Some(name_namespace_declaration) = namespace_declarations_stack .iter() .rfind(|namespace_declaration| namespace_declaration.namespace == *namespace) - .ok_or(Error::UndeclaredNamespace(namespace.clone()))?; - prefix = name_namespace_declaration.prefix.as_ref(); + { + prefix = name_namespace_declaration.prefix.as_ref(); + } else { + default_namespace_declaration = NamespaceDeclaration { + prefix: None, + namespace: namespace.clone(), + }; + if namespace_declarations.insert(default_namespace_declaration.clone()) { + namespace_declarations_stack.push(&default_namespace_declaration); + prefix = None + } else { + return Err(Error::DuplicateNameSpaceDeclaration(NamespaceDeclaration { + prefix: None, + namespace: namespace.clone(), + })); + } + } } else { prefix = None } @@ -125,7 +143,7 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { let mut attributes = Vec::new(); - for namespace_declaration in &element.namespace_declarations { + for namespace_declaration in namespace_declarations.iter() { let ns_name = namespace_declaration .prefix .as_ref() @@ -180,20 +198,38 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { } pub async fn write_element_start(&mut self, element: &Element) -> Result<()> { - let namespace_declarations_stack: Vec<_> = self + let mut namespace_declarations_stack: Vec<_> = self .namespace_declarations .iter() .flatten() - .chain(&element.namespace_declarations) + .chain(&element.namespace_declaration_overrides) .collect(); + let mut namespace_declarations = element.namespace_declaration_overrides.clone(); + + let default_namespace_declaration; let prefix; if let Some(namespace) = &element.name.namespace { - let name_namespace_declaration = namespace_declarations_stack + if let Some(name_namespace_declaration) = namespace_declarations_stack .iter() .rfind(|namespace_declaration| namespace_declaration.namespace == *namespace) - .ok_or(Error::UndeclaredNamespace(namespace.clone()))?; - prefix = name_namespace_declaration.prefix.as_ref(); + { + prefix = name_namespace_declaration.prefix.as_ref(); + } else { + default_namespace_declaration = NamespaceDeclaration { + prefix: None, + namespace: namespace.clone(), + }; + if namespace_declarations.insert(default_namespace_declaration.clone()) { + namespace_declarations_stack.push(&default_namespace_declaration); + prefix = None + } else { + return Err(Error::DuplicateNameSpaceDeclaration(NamespaceDeclaration { + prefix: None, + namespace: namespace.clone(), + })); + } + } } else { prefix = None } @@ -212,7 +248,7 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { let mut attributes = Vec::new(); - for namespace_declaration in &element.namespace_declarations { + for namespace_declaration in namespace_declarations.iter() { let ns_name = namespace_declaration .prefix .as_ref() @@ -265,7 +301,7 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { self.depth.push(element.name.clone()); self.namespace_declarations - .push(element.namespace_declarations.clone()); + .push(namespace_declarations.clone()); Ok(()) } |