minidom: Make the whole crate almost no_std

Emmanuel Gil Peyrot created

Only std::io is still missing, maybe we should consider using a crate
implementing its API on top of no_std?

Change summary

minidom/CHANGELOG.md        |  4 ++++
minidom/src/convert.rs      |  4 +++-
minidom/src/element.rs      | 12 ++++++------
minidom/src/node.rs         |  1 +
minidom/src/prefixes.rs     | 17 +++++++----------
minidom/src/tests.rs        | 10 +++++-----
minidom/src/tree_builder.rs | 12 +++++++-----
7 files changed, 33 insertions(+), 27 deletions(-)

Detailed changes

minidom/CHANGELOG.md 🔗

@@ -1,3 +1,7 @@
+Version NEXT:
+  * Changes
+    * Almost make the whole crate `no_std`, only `std::io` is still remaining.
+
 Version 0.16, released 2024-07-23:
   * Breaking
     * Element comparison returns unequal when number of nodes is unequal.

minidom/src/convert.rs 🔗

@@ -7,6 +7,8 @@
 
 //! A module which exports a few traits for converting types to elements and attributes.
 
+use alloc::string::String;
+
 /// A trait for types which can be converted to an attribute value.
 pub trait IntoAttributeValue {
     /// Turns this into an attribute string, or None if it shouldn't be added.
@@ -17,7 +19,7 @@ macro_rules! impl_into_attribute_value {
     ($t:ty) => {
         impl IntoAttributeValue for $t {
             fn into_attribute_value(self) -> Option<String> {
-                Some(format!("{}", self))
+                Some(self.to_string())
             }
         }
     };

minidom/src/element.rs 🔗

@@ -19,10 +19,10 @@ use crate::node::Node;
 use crate::prefixes::{Namespace, Prefix, Prefixes};
 use crate::tree_builder::TreeBuilder;
 
-use alloc::{
-    borrow::Cow,
-    collections::btree_map::{self, BTreeMap},
-};
+use alloc::borrow::Cow;
+use alloc::collections::btree_map::{self, BTreeMap};
+use alloc::string::String;
+use alloc::vec::Vec;
 
 use core::slice;
 use core::str::FromStr;
@@ -360,7 +360,7 @@ impl Element {
         reader: R,
         prefixes: P,
     ) -> Result<Element> {
-        let mut tree_builder = TreeBuilder::new().with_prefixes_stack(vec![prefixes.into()]);
+        let mut tree_builder = TreeBuilder::new().with_prefixes_stack([prefixes.into()].into());
         let mut driver = RawReader::new(reader);
         while let Some(event) = driver.read()? {
             tree_builder.process_event(event)?;
@@ -964,7 +964,7 @@ mod tests {
             "name".to_owned(),
             "namespace".to_owned(),
             (None, "namespace".to_owned()),
-            BTreeMap::from_iter(vec![("name".to_string(), "value".to_string())].into_iter()),
+            BTreeMap::from_iter([("name".to_string(), "value".to_string())].into_iter()),
             Vec::new(),
         );
 

minidom/src/node.rs 🔗

@@ -10,6 +10,7 @@
 
 use crate::element::{Element, ElementBuilder, ItemWriter};
 use crate::error::Result;
+use alloc::string::String;
 
 use rxml::writer::Item;
 

minidom/src/prefixes.rs 🔗

@@ -7,8 +7,9 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+use alloc::collections::BTreeMap;
+use alloc::string::String;
 use core::fmt;
-use std::collections::BTreeMap;
 
 pub type Prefix = Option<String>;
 pub type Namespace = String;
@@ -23,15 +24,11 @@ impl fmt::Debug for Prefixes {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "Prefixes(")?;
         for (prefix, namespace) in &self.prefixes {
-            write!(
-                f,
-                "xmlns{}={:?} ",
-                match prefix {
-                    None => String::new(),
-                    Some(prefix) => format!(":{}", prefix),
-                },
-                namespace
-            )?;
+            if let Some(prefix) = prefix {
+                write!(f, "xmlns:{prefix}={namespace:?} ")?;
+            } else {
+                write!(f, "xmlns={namespace:?} ")?;
+            }
         }
         write!(f, ")")
     }

minidom/src/tests.rs 🔗

@@ -12,6 +12,9 @@
 
 use crate::element::Element;
 use crate::error::Error;
+use alloc::format;
+use alloc::string::String;
+use alloc::vec::Vec;
 
 const TEST_STRING: &'static [u8] = br#"<root xmlns='root_ns' a="b" xml:lang="en">meow<child c="d"/><child xmlns='child_ns' d="e" xml:lang="fr"/>nya</root>"#;
 
@@ -82,7 +85,7 @@ fn test_real_data() {
         .append(body)
         .append(correction)
         .build();
-    let stream = Element::builder("stream", "http://etherx.jabber.org/streams")
+    let _stream = Element::builder("stream", "http://etherx.jabber.org/streams")
         .prefix(
             Some(String::from("stream")),
             "http://etherx.jabber.org/streams",
@@ -92,7 +95,6 @@ fn test_real_data() {
         .unwrap()
         .append(message)
         .build();
-    println!("{}", String::from(&stream));
 
     let jid = Element::builder("jid", "urn:xmpp:presence:0").build();
     let nick = Element::builder("nick", "urn:xmpp:presence:0").build();
@@ -119,7 +121,7 @@ fn test_real_data() {
     let iq = Element::builder("iq", "jabber:client")
         .append(pubsub)
         .build();
-    let stream = Element::builder("stream", "http://etherx.jabber.org/streams")
+    let _stream = Element::builder("stream", "http://etherx.jabber.org/streams")
         .prefix(
             Some(String::from("stream")),
             "http://etherx.jabber.org/streams",
@@ -129,8 +131,6 @@ fn test_real_data() {
         .unwrap()
         .append(iq)
         .build();
-
-    println!("{}", String::from(&stream));
 }
 
 #[test]

minidom/src/tree_builder.rs 🔗

@@ -4,8 +4,11 @@
 
 use crate::prefixes::{Prefix, Prefixes};
 use crate::{Element, Error};
+use alloc::collections::BTreeMap;
+use alloc::format;
+use alloc::string::String;
+use alloc::vec::Vec;
 use rxml::RawEvent;
-use std::collections::BTreeMap;
 
 /// Tree-building parser state
 pub struct TreeBuilder {
@@ -30,8 +33,8 @@ impl TreeBuilder {
     pub fn new() -> Self {
         TreeBuilder {
             next_tag: None,
-            stack: vec![],
-            prefixes_stack: vec![],
+            stack: Vec::new(),
+            prefixes_stack: Vec::new(),
             root: None,
         }
     }
@@ -143,8 +146,7 @@ impl TreeBuilder {
                         .lookup_prefix(&prefix.map(|prefix| prefix.as_str().to_owned()))
                         .ok_or(Error::MissingNamespace)?
                         .to_owned();
-                    let el =
-                        Element::new(name.as_str().to_owned(), namespace, prefixes, attrs, vec![]);
+                    let el = Element::new(name.to_owned(), namespace, prefixes, attrs, Vec::new());
                     self.stack.push(el);
                 }
             }