From 937e2380b978abfd90b25aa26da043f26729b2c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 29 Mar 2020 21:35:53 +0200 Subject: [PATCH] minidom: Make `ElementBuilder::prefix` fail on adding duplicate prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- minidom-rs/src/element.rs | 10 ++++++---- minidom-rs/src/error.rs | 5 +++++ minidom-rs/src/tests.rs | 20 ++++++++++---------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/minidom-rs/src/element.rs b/minidom-rs/src/element.rs index fdd4dd1..7c4bc55 100644 --- a/minidom-rs/src/element.rs +++ b/minidom-rs/src/element.rs @@ -194,7 +194,7 @@ impl Element { name.into(), namespace.into(), None, - BTreeMap::new(), + None, BTreeMap::new(), Vec::new(), ) @@ -987,10 +987,12 @@ pub struct ElementBuilder { impl ElementBuilder { /// Sets a custom prefix. It is not possible to set the same prefix twice. - pub fn prefix>(mut self, prefix: Prefix, namespace: S) -> ElementBuilder { - // TODO: Make this actually send back an error when a duplicate prefix is added. + pub fn prefix>(mut self, prefix: Prefix, namespace: S) -> Result { + if let Some(_) = self.root.prefixes.get(&prefix) { + return Err(Error::DuplicatePrefix); + } self.root.prefixes.insert(prefix, namespace.into()); - self + Ok(self) } /// Sets an attribute. diff --git a/minidom-rs/src/error.rs b/minidom-rs/src/error.rs index a783b11..9a0e0e2 100644 --- a/minidom-rs/src/error.rs +++ b/minidom-rs/src/error.rs @@ -44,6 +44,9 @@ pub enum Error { /// An error which is returned when a comment is to be parsed by minidom NoComments, + + /// An error which is returned when a prefixed is defined twice + DuplicatePrefix, } impl StdError for Error { @@ -58,6 +61,7 @@ impl StdError for Error { Error::InvalidPrefix => None, Error::MissingNamespace => None, Error::NoComments => None, + Error::DuplicatePrefix => None, } } } @@ -84,6 +88,7 @@ impl std::fmt::Display for Error { fmt, "a comment has been found even though comments are forbidden" ), + Error::DuplicatePrefix => write!(fmt, "the prefix is already defined"), } } } diff --git a/minidom-rs/src/tests.rs b/minidom-rs/src/tests.rs index aa71e60..3ba15a9 100644 --- a/minidom-rs/src/tests.rs +++ b/minidom-rs/src/tests.rs @@ -77,8 +77,8 @@ fn test_real_data() { .append(correction) .build(); let stream = Element::builder("stream", "http://etherx.jabber.org/streams") - .prefix(Some(String::from("stream")), "http://etherx.jabber.org/streams") - .prefix(None, "jabber:client") + .prefix(Some(String::from("stream")), "http://etherx.jabber.org/streams").unwrap() + .prefix(None, "jabber:client").unwrap() .append(message) .build(); println!("{}", String::from(&stream)); @@ -109,8 +109,8 @@ fn test_real_data() { .append(pubsub) .build(); let stream = Element::builder("stream", "http://etherx.jabber.org/streams") - .prefix(Some(String::from("stream")), "http://etherx.jabber.org/streams") - .prefix(None, "jabber:client") + .prefix(Some(String::from("stream")), "http://etherx.jabber.org/streams").unwrap() + .prefix(None, "jabber:client").unwrap() .append(iq) .build(); @@ -141,8 +141,8 @@ fn writer_with_decl_works() { #[test] fn writer_with_prefix() { let root = Element::builder("root", "ns1") - .prefix(Some(String::from("p1")), "ns1") - .prefix(None, "ns2") + .prefix(Some(String::from("p1")), "ns1").unwrap() + .prefix(None, "ns2").unwrap() .build(); assert_eq!(String::from(&root), r#""#, @@ -168,7 +168,7 @@ fn writer_no_prefix_namespace_child() { assert_eq!(String::from(&root), r#""#); let child = Element::builder("child", "ns2") - .prefix(None, "ns3") + .prefix(None, "ns3").unwrap() .build(); let root = Element::builder("root", "ns1") .append(child) @@ -181,7 +181,7 @@ fn writer_no_prefix_namespace_child() { fn writer_prefix_namespace_child() { let child = Element::builder("child", "ns1").build(); let root = Element::builder("root", "ns1") - .prefix(Some(String::from("p1")), "ns1") + .prefix(Some(String::from("p1")), "ns1").unwrap() .append(child) .build(); assert_eq!(String::from(&root), r#""#); @@ -193,8 +193,8 @@ fn writer_with_prefix_deduplicate() { // .prefix(Some(String::from("p1")), "ns1") .build(); let root = Element::builder("root", "ns1") - .prefix(Some(String::from("p1")), "ns1") - .prefix(None, "ns2") + .prefix(Some(String::from("p1")), "ns1").unwrap() + .prefix(None, "ns2").unwrap() .append(child) .build(); assert_eq!(String::from(&root),