automatically declare default namespaces unless overriden
This commit is contained in:
		
							parent
							
								
									87e6ff405b
								
							
						
					
					
						commit
						381af38a09
					
				|  | @ -58,7 +58,8 @@ pub struct Element { | ||||||
|     // namespace: String,
 |     // namespace: String,
 | ||||||
|     // hashmap of explicit namespace declarations on the element itself only
 |     // hashmap of explicit namespace declarations on the element itself only
 | ||||||
|     // possibly not needed as can be calculated at write time depending on context and qualified namespace, and for reading, element validity and namespaces are kept track of by the reader.
 |     // possibly not needed as can be calculated at write time depending on context and qualified namespace, and for reading, element validity and namespaces are kept track of by the reader.
 | ||||||
|     pub namespace_declarations: HashSet<NamespaceDeclaration>, |     // change this to custom namespace declarations only, so you can override the definition of namespaces if you wish
 | ||||||
|  |     pub namespace_declaration_overrides: HashSet<NamespaceDeclaration>, | ||||||
|     // attributes can be in a different namespace than the element. how to make sure they are valid?
 |     // attributes can be in a different namespace than the element. how to make sure they are valid?
 | ||||||
|     // maybe include the namespace instead of or with the prefix
 |     // maybe include the namespace instead of or with the prefix
 | ||||||
|     // you can calculate the prefix from the namespaced name and the current writer context
 |     // you can calculate the prefix from the namespaced name and the current writer context
 | ||||||
|  |  | ||||||
|  | @ -1,6 +1,9 @@ | ||||||
| use std::{num::ParseIntError, str::Utf8Error}; | use std::{num::ParseIntError, str::Utf8Error}; | ||||||
| 
 | 
 | ||||||
| use crate::element::{Content, Name, NamespaceDeclaration}; | use crate::{ | ||||||
|  |     element::{Content, Name, NamespaceDeclaration}, | ||||||
|  |     Element, | ||||||
|  | }; | ||||||
| 
 | 
 | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub enum Error { | pub enum Error { | ||||||
|  |  | ||||||
|  | @ -436,7 +436,7 @@ impl<R> Reader<R> { | ||||||
| 
 | 
 | ||||||
|         return Ok(Element { |         return Ok(Element { | ||||||
|             name: element_name, |             name: element_name, | ||||||
|             namespace_declarations: element_namespace_declarations, |             namespace_declaration_overrides: element_namespace_declarations, | ||||||
|             attributes, |             attributes, | ||||||
|             content: Vec::new(), |             content: Vec::new(), | ||||||
|         }); |         }); | ||||||
|  | @ -657,7 +657,7 @@ impl<R> Reader<R> { | ||||||
| 
 | 
 | ||||||
|         return Ok(Element { |         return Ok(Element { | ||||||
|             name: element_name, |             name: element_name, | ||||||
|             namespace_declarations: element_namespace_declarations, |             namespace_declaration_overrides: element_namespace_declarations, | ||||||
|             attributes, |             attributes, | ||||||
|             content, |             content, | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|  | @ -93,20 +93,38 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub async fn write_empty(&mut self, element: &Element) -> Result<()> { |     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 |             .namespace_declarations | ||||||
|             .iter() |             .iter() | ||||||
|             .flatten() |             .flatten() | ||||||
|             .chain(&element.namespace_declarations) |             .chain(&element.namespace_declaration_overrides) | ||||||
|             .collect(); |             .collect(); | ||||||
| 
 | 
 | ||||||
|  |         let mut namespace_declarations = element.namespace_declaration_overrides.clone(); | ||||||
|  | 
 | ||||||
|  |         let default_namespace_declaration; | ||||||
|         let prefix; |         let prefix; | ||||||
|         if let Some(namespace) = &element.name.namespace { |         if let Some(namespace) = &element.name.namespace { | ||||||
|             let name_namespace_declaration = namespace_declarations_stack |             if let Some(name_namespace_declaration) = namespace_declarations_stack | ||||||
|                 .iter() |                 .iter() | ||||||
|                 .rfind(|namespace_declaration| namespace_declaration.namespace == *namespace) |                 .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 { |         } else { | ||||||
|             prefix = None |             prefix = None | ||||||
|         } |         } | ||||||
|  | @ -125,7 +143,7 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { | ||||||
| 
 | 
 | ||||||
|         let mut attributes = Vec::new(); |         let mut attributes = Vec::new(); | ||||||
| 
 | 
 | ||||||
|         for namespace_declaration in &element.namespace_declarations { |         for namespace_declaration in namespace_declarations.iter() { | ||||||
|             let ns_name = namespace_declaration |             let ns_name = namespace_declaration | ||||||
|                 .prefix |                 .prefix | ||||||
|                 .as_ref() |                 .as_ref() | ||||||
|  | @ -180,20 +198,38 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub async fn write_element_start(&mut self, element: &Element) -> Result<()> { |     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 |             .namespace_declarations | ||||||
|             .iter() |             .iter() | ||||||
|             .flatten() |             .flatten() | ||||||
|             .chain(&element.namespace_declarations) |             .chain(&element.namespace_declaration_overrides) | ||||||
|             .collect(); |             .collect(); | ||||||
| 
 | 
 | ||||||
|  |         let mut namespace_declarations = element.namespace_declaration_overrides.clone(); | ||||||
|  | 
 | ||||||
|  |         let default_namespace_declaration; | ||||||
|         let prefix; |         let prefix; | ||||||
|         if let Some(namespace) = &element.name.namespace { |         if let Some(namespace) = &element.name.namespace { | ||||||
|             let name_namespace_declaration = namespace_declarations_stack |             if let Some(name_namespace_declaration) = namespace_declarations_stack | ||||||
|                 .iter() |                 .iter() | ||||||
|                 .rfind(|namespace_declaration| namespace_declaration.namespace == *namespace) |                 .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 { |         } else { | ||||||
|             prefix = None |             prefix = None | ||||||
|         } |         } | ||||||
|  | @ -212,7 +248,7 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { | ||||||
| 
 | 
 | ||||||
|         let mut attributes = Vec::new(); |         let mut attributes = Vec::new(); | ||||||
| 
 | 
 | ||||||
|         for namespace_declaration in &element.namespace_declarations { |         for namespace_declaration in namespace_declarations.iter() { | ||||||
|             let ns_name = namespace_declaration |             let ns_name = namespace_declaration | ||||||
|                 .prefix |                 .prefix | ||||||
|                 .as_ref() |                 .as_ref() | ||||||
|  | @ -265,7 +301,7 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> { | ||||||
| 
 | 
 | ||||||
|         self.depth.push(element.name.clone()); |         self.depth.push(element.name.clone()); | ||||||
|         self.namespace_declarations |         self.namespace_declarations | ||||||
|             .push(element.namespace_declarations.clone()); |             .push(namespace_declarations.clone()); | ||||||
|         Ok(()) |         Ok(()) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue