Merge branch 'optional-comments' into 'master'

Maxime Buquet created

Make comments optional

See merge request xmpp-rs/minidom-rs!36

Change summary

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(-)

Detailed changes

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<Node>` and `<T: Into<Node> IntoIterator<Item = T>>`

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 = []

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<S: Into<String>>(&mut self, child: S) {
         self.children.push(Node::Comment(child.into()));
     }

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 {

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)))?;
             },

src/tests.rs 🔗

@@ -27,8 +27,10 @@ fn build_test_tree() -> Element {
     root
 }
 
+#[cfg(feature = "comments")]
 const COMMENT_TEST_STRING: &'static str = r#"<?xml version="1.0" encoding="utf-8"?><root><!--This is a child.--><child attr="val"><!--This is a grandchild.--><grandchild/></child></root>"#;
 
+#[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();