From a91252c861ee61a390aac0a7907ae34f4482ea41 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 22 Aug 2019 18:04:47 +0200 Subject: [PATCH] Make comments optional. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a default "comments" feature to transform comments into errors when unset. This is so that XMPP implementations don’t have to care about comments, as they can’t happen in the stream. --- CHANGELOG.md | 1 + Cargo.toml | 4 ++++ src/element.rs | 11 ++++++++++- src/error.rs | 5 +++++ src/node.rs | 8 ++++++++ src/tests.rs | 4 ++++ 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0dd53d3..4dc9482e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version XXX, released YYY: * Changes * Update to quick-xml 0.15 + * Add a default "comments" feature to transform comments into errors when unset. Version 0.11.0, released 2019-06-14: * Breaking * Get rid of IntoElements, replace with `Into` and ` IntoIterator>` diff --git a/Cargo.toml b/Cargo.toml index 3c13bd80..9ed80d59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,7 @@ gitlab = { repository = "lumi/minidom-rs" } quick-xml = "0.15" failure = "0.1.1" failure_derive = "0.1.1" + +[features] +default = ["comments"] +comments = [] diff --git a/src/element.rs b/src/element.rs index 47c8de96..cc9f8e11 100644 --- a/src/element.rs +++ b/src/element.rs @@ -283,9 +283,14 @@ impl Element { Event::Eof => { return Err(Error::EndOfDocument); }, + #[cfg(not(feature = "comments"))] + Event::Comment { .. } => { + return Err(Error::CommentsDisabled); + } + #[cfg(feature = "comments")] + Event::Comment { .. } => (), Event::Text { .. } | Event::End { .. } | - Event::Comment { .. } | Event::CData { .. } | Event::Decl { .. } | Event::PI { .. } | @@ -361,6 +366,9 @@ impl Element { Event::Eof => { break; }, + #[cfg(not(feature = "comments"))] + Event::Comment(_) => return Err(Error::CommentsDisabled), + #[cfg(feature = "comments")] Event::Comment(s) => { let comment = reader.decode(&s).into_owned(); if comment != "" { @@ -569,6 +577,7 @@ impl Element { /// /// elem.append_comment_node("comment"); /// ``` + #[cfg(feature = "comments")] pub fn append_comment_node>(&mut self, child: S) { self.children.push(Node::Comment(child.into())); } diff --git a/src/error.rs b/src/error.rs index b2ba473c..773f214f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -28,6 +28,11 @@ pub enum Error { /// An error which is returned when an elemet's name contains more than one colon #[fail(display = "the XML element is invalid")] InvalidElement, + + /// An error which is returned when a comment is to be parsed by minidom + #[cfg(not(comments))] + #[fail(display = "a comment has been found even though comments are disabled by feature")] + CommentsDisabled, } impl From<::quick_xml::Error> for Error { diff --git a/src/node.rs b/src/node.rs index b136dbdc..56069d09 100644 --- a/src/node.rs +++ b/src/node.rs @@ -16,6 +16,7 @@ pub enum Node { Element(Element), /// A text node. Text(String), + #[cfg(feature = "comments")] /// A comment node. Comment(String), } @@ -39,6 +40,7 @@ impl Node { match *self { Node::Element(ref e) => Some(e), Node::Text(_) => None, + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -61,6 +63,7 @@ impl Node { match *self { Node::Element(ref mut e) => Some(e), Node::Text(_) => None, + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -83,6 +86,7 @@ impl Node { match self { Node::Element(e) => Some(e), Node::Text(_) => None, + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -105,6 +109,7 @@ impl Node { match *self { Node::Element(_) => None, Node::Text(ref s) => Some(s), + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -133,6 +138,7 @@ impl Node { match *self { Node::Element(_) => None, Node::Text(ref mut s) => Some(s), + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -155,6 +161,7 @@ impl Node { match self { Node::Element(_) => None, Node::Text(s) => Some(s), + #[cfg(feature = "comments")] Node::Comment(_) => None, } } @@ -166,6 +173,7 @@ impl Node { Node::Text(ref s) => { writer.write_event(Event::Text(BytesText::from_plain_str(s)))?; }, + #[cfg(feature = "comments")] Node::Comment(ref s) => { writer.write_event(Event::Comment(BytesText::from_plain_str(s)))?; }, diff --git a/src/tests.rs b/src/tests.rs index 2acc9705..1ff37e6c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -27,8 +27,10 @@ fn build_test_tree() -> Element { root } +#[cfg(feature = "comments")] const COMMENT_TEST_STRING: &'static str = r#""#; +#[cfg(feature = "comments")] fn build_comment_test_tree() -> Element { let mut root = Element::builder("root").build(); root.append_comment_node("This is a child."); @@ -211,12 +213,14 @@ fn namespace_inherited_prefixed2() { assert_eq!(child.ns(), Some("jabber:client".to_owned())); } +#[cfg(feature = "comments")] #[test] fn read_comments() { let mut reader = Reader::from_str(COMMENT_TEST_STRING); assert_eq!(Element::from_reader(&mut reader).unwrap(), build_comment_test_tree()); } +#[cfg(feature = "comments")] #[test] fn write_comments() { let root = build_comment_test_tree();