minidom: Make the whole crate almost no_std

Only std::io is still missing, maybe we should consider using a crate
implementing its API on top of no_std?
This commit is contained in:
Emmanuel Gil Peyrot 2025-02-25 01:06:30 +01:00 committed by Link Mauve
commit d203a348cb
7 changed files with 33 additions and 27 deletions

View file

@ -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: Version 0.16, released 2024-07-23:
* Breaking * Breaking
* Element comparison returns unequal when number of nodes is unequal. * Element comparison returns unequal when number of nodes is unequal.

View file

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

View file

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

View file

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

View file

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

View file

@ -12,6 +12,9 @@
use crate::element::Element; use crate::element::Element;
use crate::error::Error; 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>"#; 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(body)
.append(correction) .append(correction)
.build(); .build();
let stream = Element::builder("stream", "http://etherx.jabber.org/streams") let _stream = Element::builder("stream", "http://etherx.jabber.org/streams")
.prefix( .prefix(
Some(String::from("stream")), Some(String::from("stream")),
"http://etherx.jabber.org/streams", "http://etherx.jabber.org/streams",
@ -92,7 +95,6 @@ fn test_real_data() {
.unwrap() .unwrap()
.append(message) .append(message)
.build(); .build();
println!("{}", String::from(&stream));
let jid = Element::builder("jid", "urn:xmpp:presence:0").build(); let jid = Element::builder("jid", "urn:xmpp:presence:0").build();
let nick = Element::builder("nick", "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") let iq = Element::builder("iq", "jabber:client")
.append(pubsub) .append(pubsub)
.build(); .build();
let stream = Element::builder("stream", "http://etherx.jabber.org/streams") let _stream = Element::builder("stream", "http://etherx.jabber.org/streams")
.prefix( .prefix(
Some(String::from("stream")), Some(String::from("stream")),
"http://etherx.jabber.org/streams", "http://etherx.jabber.org/streams",
@ -129,8 +131,6 @@ fn test_real_data() {
.unwrap() .unwrap()
.append(iq) .append(iq)
.build(); .build();
println!("{}", String::from(&stream));
} }
#[test] #[test]

View file

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