From 2d97e2d5d9afae6ea4dd71f9ad96cb447b38be8c Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 23:29:19 +0100 Subject: [PATCH] put Attribute into its own module, take into account prefixes when determining namespaces --- src/attribute.rs | 24 ++++++++++++++++++++++++ src/lib.rs | 39 ++++++++++++++++----------------------- 2 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 src/attribute.rs diff --git a/src/attribute.rs b/src/attribute.rs new file mode 100644 index 0000000..ac41f12 --- /dev/null +++ b/src/attribute.rs @@ -0,0 +1,24 @@ +use xml::escape::escape_str_attribute; + +use std::fmt; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Attribute { + pub name: String, + pub value: String, +} + +impl fmt::Display for Attribute { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value)) + } +} + +impl Attribute { + pub fn new, V: Into>(name: N, value: V) -> Attribute { + Attribute { + name: name.into(), + value: value.into(), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index f524772..51a8373 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ extern crate xml; pub mod error; +pub mod attribute; use std::io::prelude::*; @@ -15,31 +16,11 @@ use std::fmt; use xml::reader::{XmlEvent as ReaderEvent, EventReader}; use xml::writer::{XmlEvent as WriterEvent, EventWriter}; use xml::name::Name; -use xml::escape::escape_str_attribute; use xml::namespace::NS_NO_PREFIX; use error::Error; -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Attribute { - pub name: String, - pub value: String, -} - -impl fmt::Display for Attribute { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value)) - } -} - -impl Attribute { - pub fn new, V: Into>(name: N, value: V) -> Attribute { - Attribute { - name: name.into(), - value: value.into(), - } - } -} +use attribute::Attribute; #[derive(Clone, PartialEq, Eq)] pub struct Element { @@ -131,7 +112,13 @@ impl Element { let attributes = attributes.into_iter() .map(|o| Attribute::new(o.name.local_name, o.value)) .collect(); - let mut root = Element::new(name.local_name, namespace.get(NS_NO_PREFIX).map(|s| s.to_owned()), attributes); + let ns = if let Some(ref prefix) = name.prefix { + namespace.get(prefix) + } + else { + namespace.get(NS_NO_PREFIX) + }.map(|s| s.to_owned()); + let mut root = Element::new(name.local_name, ns, attributes); root.from_reader_inner(reader); return Ok(root); }, @@ -151,7 +138,13 @@ impl Element { let attributes = attributes.into_iter() .map(|o| Attribute::new(o.name.local_name, o.value)) .collect(); - let elem = Element::new(name.local_name, namespace.get(NS_NO_PREFIX).map(|s| s.to_owned()), attributes); + let ns = if let Some(ref prefix) = name.prefix { + namespace.get(prefix) + } + else { + namespace.get(NS_NO_PREFIX) + }.map(|s| s.to_owned()); + let elem = Element::new(name.local_name, ns, attributes); let elem_ref = self.append_child(elem); elem_ref.from_reader_inner(reader); },