minidom: rustfmt

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2020-04-02 22:42:28 +02:00
parent 3d71e37e0c
commit fa8b9ed199
5 changed files with 64 additions and 37 deletions

View file

@ -15,8 +15,8 @@
use crate::convert::IntoAttributeValue;
use crate::error::{Error, Result};
use crate::namespaces::NSChoice;
use crate::prefixes::{Prefix, Namespace, Prefixes};
use crate::node::Node;
use crate::prefixes::{Namespace, Prefix, Prefixes};
use std::collections::{btree_map, BTreeMap};
use std::io::Write;
@ -178,7 +178,7 @@ impl Element {
None,
None,
BTreeMap::new(),
Vec::new()
Vec::new(),
),
}
}
@ -456,7 +456,11 @@ impl Element {
}
/// Like `write_to()` but without the `<?xml?>` prelude
pub fn write_to_inner<W: Write>(&self, writer: &mut EventWriter<W>, all_prefixes: &mut BTreeMap<Prefix, Namespace>) -> Result<()> {
pub fn write_to_inner<W: Write>(
&self,
writer: &mut EventWriter<W>,
all_prefixes: &mut BTreeMap<Prefix, Namespace>,
) -> Result<()> {
let local_prefixes: &BTreeMap<Option<String>, String> = self.prefixes.declared_prefixes();
// Element namespace
@ -485,7 +489,7 @@ impl Element {
}
(Some(format!("ns{}", prefix_n)), true)
}
},
}
// Some prefix has already been declared (or is going to be) for our namespace. We
// don't need to declare a new one. We do however need to remember which one to use in
// the tag name.
@ -509,7 +513,7 @@ impl Element {
let key = format!("xmlns");
start.push_attribute((key.as_bytes(), self.namespace.as_bytes()));
all_prefixes.insert(self_prefix.0, self.namespace.clone());
},
}
_ => (),
};
@ -525,7 +529,7 @@ impl Element {
start.push_attribute((key.as_bytes(), ns.as_ref()));
all_prefixes.insert(prefix.clone(), ns.clone());
},
}
}
}
@ -834,7 +838,11 @@ fn split_element_name<S: AsRef<str>>(s: S) -> Result<(Option<String>, String)> {
}
}
fn build_element<R: BufRead>(reader: &EventReader<R>, event: &BytesStart, prefixes: &mut BTreeMap<Prefix, Namespace>) -> Result<Element> {
fn build_element<R: BufRead>(
reader: &EventReader<R>,
event: &BytesStart,
prefixes: &mut BTreeMap<Prefix, Namespace>,
) -> Result<Element> {
let (prefix, name) = split_element_name(str::from_utf8(event.name())?)?;
let mut local_prefixes = BTreeMap::new();
@ -879,7 +887,7 @@ fn build_element<R: BufRead>(reader: &EventReader<R>, event: &BytesStart, prefix
Some(prefix),
local_prefixes,
attributes,
Vec::new()
Vec::new(),
))
}
@ -994,7 +1002,11 @@ pub struct ElementBuilder {
impl ElementBuilder {
/// Sets a custom prefix. It is not possible to set the same prefix twice.
pub fn prefix<S: Into<Namespace>>(mut self, prefix: Prefix, namespace: S) -> Result<ElementBuilder> {
pub fn prefix<S: Into<Namespace>>(
mut self,
prefix: Prefix,
namespace: S,
) -> Result<ElementBuilder> {
if let Some(_) = self.root.prefixes.get(&prefix) {
return Err(Error::DuplicatePrefix);
}
@ -1102,7 +1114,10 @@ mod tests {
assert_eq!(elem.name(), String::from("bar"));
assert_eq!(elem.ns(), String::from("ns1"));
// Ensure the prefix is properly added to the store
assert_eq!(elem.prefixes.get(&Some(String::from("foo"))), Some(&String::from("ns1")));
assert_eq!(
elem.prefixes.get(&Some(String::from("foo"))),
Some(&String::from("ns1"))
);
}
#[test]

View file

@ -80,10 +80,7 @@ impl std::fmt::Display for Error {
}
Error::InvalidElement => write!(fmt, "the XML element is invalid"),
Error::InvalidPrefix => write!(fmt, "the prefix is invalid"),
Error::MissingNamespace => write!(
fmt,
"the XML element is missing a namespace",
),
Error::MissingNamespace => write!(fmt, "the XML element is missing a namespace",),
Error::NoComments => write!(
fmt,
"a comment has been found even though comments are forbidden"

View file

@ -80,8 +80,8 @@ pub mod convert;
pub mod element;
pub mod error;
mod namespaces;
mod prefixes;
pub mod node;
mod prefixes;
#[cfg(test)]
mod tests;

View file

@ -11,8 +11,8 @@
use crate::element::{Element, ElementBuilder};
use crate::error::Result;
use std::io::Write;
use std::collections::BTreeMap;
use std::io::Write;
use quick_xml::events::{BytesText, Event};
use quick_xml::Writer as EventWriter;
@ -160,7 +160,11 @@ impl Node {
}
#[doc(hidden)]
pub(crate) fn write_to_inner<W: Write>(&self, writer: &mut EventWriter<W>, prefixes: &mut BTreeMap<Option<String>, String>) -> Result<()> {
pub(crate) fn write_to_inner<W: Write>(
&self,
writer: &mut EventWriter<W>,
prefixes: &mut BTreeMap<Option<String>, String>,
) -> Result<()> {
match *self {
Node::Element(ref elmt) => elmt.write_to_inner(writer, prefixes)?,
Node::Text(ref s) => {

View file

@ -49,12 +49,17 @@ fn reader_deduplicate_prefixes() {
// parent ns with the same prefix.
let _: Element = r#"<root xmlns="ns1"><child/></root>"#.parse().unwrap();
let _: Element = r#"<p1:root xmlns:p1="ns1"><p1:child/></p1:root>"#.parse().unwrap();
let _: Element = r#"<root xmlns="ns1"><child xmlns:p1="ns2"><p1:grandchild/></child></root>"#.parse().unwrap();
let _: Element = r#"<root xmlns="ns1"><child xmlns:p1="ns2"><p1:grandchild/></child></root>"#
.parse()
.unwrap();
match r#"<p1:root xmlns:p1="ns1"><child/></p1:root>"#.parse::<Element>() {
Err(Error::MissingNamespace) => (),
Err(err) => panic!("No or wrong error: {:?}", err),
Ok(elem) => panic!("Got Element: {}; was expecting Error::MissingNamespace", String::from(&elem)),
Ok(elem) => panic!(
"Got Element: {}; was expecting Error::MissingNamespace",
String::from(&elem)
),
}
}
@ -64,7 +69,11 @@ fn reader_no_deduplicate_sibling_prefixes() {
match r#"<root xmlns="ns1"><p1:child1 xmlns:p1="ns2"/><p1:child2/></root>"#.parse::<Element>() {
Err(Error::MissingNamespace) => (),
Err(err) => panic!("No or wrong error: {:?}", err),
Ok(elem) => panic!("Got Element:\n{:?}\n{}\n; was expecting Error::MissingNamespace", elem, String::from(&elem)),
Ok(elem) => panic!(
"Got Element:\n{:?}\n{}\n; was expecting Error::MissingNamespace",
elem,
String::from(&elem)
),
}
}
@ -156,7 +165,8 @@ fn writer_with_prefix() {
.prefix(None, "ns2")
.unwrap()
.build();
assert_eq!(String::from(&root),
assert_eq!(
String::from(&root),
r#"<p1:root xmlns="ns2" xmlns:p1="ns1"/>"#,
);
}
@ -183,7 +193,10 @@ fn writer_no_prefix_namespace_child() {
.build();
let root = Element::builder("root", "ns1").append(child).build();
// TODO: Same remark as `writer_no_prefix_namespace`.
assert_eq!(String::from(&root), r#"<root xmlns="ns1"><ns0:child xmlns:ns0="ns2" xmlns="ns3"/></root>"#);
assert_eq!(
String::from(&root),
r#"<root xmlns="ns1"><ns0:child xmlns:ns0="ns2" xmlns="ns3"/></root>"#
);
}
#[test]
@ -194,7 +207,10 @@ fn writer_prefix_namespace_child() {
.unwrap()
.append(child)
.build();
assert_eq!(String::from(&root), r#"<p1:root xmlns:p1="ns1"><p1:child/></p1:root>"#);
assert_eq!(
String::from(&root),
r#"<p1:root xmlns:p1="ns1"><p1:child/></p1:root>"#
);
}
#[test]
@ -209,7 +225,8 @@ fn writer_with_prefix_deduplicate() {
.unwrap()
.append(child)
.build();
assert_eq!(String::from(&root),
assert_eq!(
String::from(&root),
r#"<p1:root xmlns="ns2" xmlns:p1="ns1"><p1:child/></p1:root>"#,
);
@ -245,7 +262,10 @@ fn writer_escapes_text() {
{
root.write_to(&mut writer).unwrap();
}
assert_eq!(String::from_utf8(writer).unwrap(), r#"<root xmlns="ns1">&lt;3</root>"#);
assert_eq!(
String::from_utf8(writer).unwrap(),
r#"<root xmlns="ns1">&lt;3</root>"#
);
}
#[test]
@ -363,10 +383,7 @@ fn namespace_prefixed() {
.parse()
.unwrap();
assert_eq!(elem.name(), "features");
assert_eq!(
elem.ns(),
"http://etherx.jabber.org/streams".to_owned(),
);
assert_eq!(elem.ns(), "http://etherx.jabber.org/streams".to_owned(),);
}
#[test]
@ -386,10 +403,7 @@ fn namespace_inherited_prefixed1() {
let elem: Element = "<stream:features xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client'><message xmlns='jabber:client' /></stream:features>"
.parse().unwrap();
assert_eq!(elem.name(), "features");
assert_eq!(
elem.ns(),
"http://etherx.jabber.org/streams".to_owned(),
);
assert_eq!(elem.ns(), "http://etherx.jabber.org/streams".to_owned(),);
let child = elem.children().next().unwrap();
assert_eq!(child.name(), "message");
assert_eq!(child.ns(), "jabber:client".to_owned());
@ -400,10 +414,7 @@ fn namespace_inherited_prefixed2() {
let elem: Element = "<stream xmlns='http://etherx.jabber.org/streams' xmlns:jabber='jabber:client'><jabber:message xmlns:jabber='jabber:client' /></stream>"
.parse().unwrap();
assert_eq!(elem.name(), "stream");
assert_eq!(
elem.ns(),
"http://etherx.jabber.org/streams".to_owned(),
);
assert_eq!(elem.ns(), "http://etherx.jabber.org/streams".to_owned(),);
let child = elem.children().next().unwrap();
assert_eq!(child.name(), "message");
assert_eq!(child.ns(), "jabber:client".to_owned());