minidom: rustfmt
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
3d71e37e0c
commit
fa8b9ed199
5 changed files with 64 additions and 37 deletions
|
@ -15,8 +15,8 @@
|
||||||
use crate::convert::IntoAttributeValue;
|
use crate::convert::IntoAttributeValue;
|
||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use crate::namespaces::NSChoice;
|
use crate::namespaces::NSChoice;
|
||||||
use crate::prefixes::{Prefix, Namespace, Prefixes};
|
|
||||||
use crate::node::Node;
|
use crate::node::Node;
|
||||||
|
use crate::prefixes::{Namespace, Prefix, Prefixes};
|
||||||
|
|
||||||
use std::collections::{btree_map, BTreeMap};
|
use std::collections::{btree_map, BTreeMap};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
@ -178,7 +178,7 @@ impl Element {
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
BTreeMap::new(),
|
BTreeMap::new(),
|
||||||
Vec::new()
|
Vec::new(),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -456,7 +456,11 @@ impl Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like `write_to()` but without the `<?xml?>` prelude
|
/// 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();
|
let local_prefixes: &BTreeMap<Option<String>, String> = self.prefixes.declared_prefixes();
|
||||||
|
|
||||||
// Element namespace
|
// Element namespace
|
||||||
|
@ -485,7 +489,7 @@ impl Element {
|
||||||
}
|
}
|
||||||
(Some(format!("ns{}", prefix_n)), true)
|
(Some(format!("ns{}", prefix_n)), true)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
// Some prefix has already been declared (or is going to be) for our namespace. We
|
// 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
|
// don't need to declare a new one. We do however need to remember which one to use in
|
||||||
// the tag name.
|
// the tag name.
|
||||||
|
@ -509,7 +513,7 @@ impl Element {
|
||||||
let key = format!("xmlns");
|
let key = format!("xmlns");
|
||||||
start.push_attribute((key.as_bytes(), self.namespace.as_bytes()));
|
start.push_attribute((key.as_bytes(), self.namespace.as_bytes()));
|
||||||
all_prefixes.insert(self_prefix.0, self.namespace.clone());
|
all_prefixes.insert(self_prefix.0, self.namespace.clone());
|
||||||
},
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -525,7 +529,7 @@ impl Element {
|
||||||
|
|
||||||
start.push_attribute((key.as_bytes(), ns.as_ref()));
|
start.push_attribute((key.as_bytes(), ns.as_ref()));
|
||||||
all_prefixes.insert(prefix.clone(), ns.clone());
|
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 (prefix, name) = split_element_name(str::from_utf8(event.name())?)?;
|
||||||
let mut local_prefixes = BTreeMap::new();
|
let mut local_prefixes = BTreeMap::new();
|
||||||
|
|
||||||
|
@ -879,7 +887,7 @@ fn build_element<R: BufRead>(reader: &EventReader<R>, event: &BytesStart, prefix
|
||||||
Some(prefix),
|
Some(prefix),
|
||||||
local_prefixes,
|
local_prefixes,
|
||||||
attributes,
|
attributes,
|
||||||
Vec::new()
|
Vec::new(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -994,7 +1002,11 @@ pub struct ElementBuilder {
|
||||||
|
|
||||||
impl ElementBuilder {
|
impl ElementBuilder {
|
||||||
/// Sets a custom prefix. It is not possible to set the same prefix twice.
|
/// 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) {
|
if let Some(_) = self.root.prefixes.get(&prefix) {
|
||||||
return Err(Error::DuplicatePrefix);
|
return Err(Error::DuplicatePrefix);
|
||||||
}
|
}
|
||||||
|
@ -1102,7 +1114,10 @@ mod tests {
|
||||||
assert_eq!(elem.name(), String::from("bar"));
|
assert_eq!(elem.name(), String::from("bar"));
|
||||||
assert_eq!(elem.ns(), String::from("ns1"));
|
assert_eq!(elem.ns(), String::from("ns1"));
|
||||||
// Ensure the prefix is properly added to the store
|
// 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]
|
#[test]
|
||||||
|
|
|
@ -80,10 +80,7 @@ impl std::fmt::Display for Error {
|
||||||
}
|
}
|
||||||
Error::InvalidElement => write!(fmt, "the XML element is invalid"),
|
Error::InvalidElement => write!(fmt, "the XML element is invalid"),
|
||||||
Error::InvalidPrefix => write!(fmt, "the prefix is invalid"),
|
Error::InvalidPrefix => write!(fmt, "the prefix is invalid"),
|
||||||
Error::MissingNamespace => write!(
|
Error::MissingNamespace => write!(fmt, "the XML element is missing a namespace",),
|
||||||
fmt,
|
|
||||||
"the XML element is missing a namespace",
|
|
||||||
),
|
|
||||||
Error::NoComments => write!(
|
Error::NoComments => write!(
|
||||||
fmt,
|
fmt,
|
||||||
"a comment has been found even though comments are forbidden"
|
"a comment has been found even though comments are forbidden"
|
||||||
|
|
|
@ -80,8 +80,8 @@ pub mod convert;
|
||||||
pub mod element;
|
pub mod element;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
mod namespaces;
|
mod namespaces;
|
||||||
mod prefixes;
|
|
||||||
pub mod node;
|
pub mod node;
|
||||||
|
mod prefixes;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
|
@ -11,8 +11,8 @@
|
||||||
use crate::element::{Element, ElementBuilder};
|
use crate::element::{Element, ElementBuilder};
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
|
|
||||||
use std::io::Write;
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
use quick_xml::events::{BytesText, Event};
|
use quick_xml::events::{BytesText, Event};
|
||||||
use quick_xml::Writer as EventWriter;
|
use quick_xml::Writer as EventWriter;
|
||||||
|
@ -160,7 +160,11 @@ impl Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[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 {
|
match *self {
|
||||||
Node::Element(ref elmt) => elmt.write_to_inner(writer, prefixes)?,
|
Node::Element(ref elmt) => elmt.write_to_inner(writer, prefixes)?,
|
||||||
Node::Text(ref s) => {
|
Node::Text(ref s) => {
|
||||||
|
|
|
@ -49,12 +49,17 @@ fn reader_deduplicate_prefixes() {
|
||||||
// parent ns with the same prefix.
|
// parent ns with the same prefix.
|
||||||
let _: Element = r#"<root xmlns="ns1"><child/></root>"#.parse().unwrap();
|
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#"<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>() {
|
match r#"<p1:root xmlns:p1="ns1"><child/></p1:root>"#.parse::<Element>() {
|
||||||
Err(Error::MissingNamespace) => (),
|
Err(Error::MissingNamespace) => (),
|
||||||
Err(err) => panic!("No or wrong error: {:?}", err),
|
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>() {
|
match r#"<root xmlns="ns1"><p1:child1 xmlns:p1="ns2"/><p1:child2/></root>"#.parse::<Element>() {
|
||||||
Err(Error::MissingNamespace) => (),
|
Err(Error::MissingNamespace) => (),
|
||||||
Err(err) => panic!("No or wrong error: {:?}", err),
|
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")
|
.prefix(None, "ns2")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.build();
|
.build();
|
||||||
assert_eq!(String::from(&root),
|
assert_eq!(
|
||||||
|
String::from(&root),
|
||||||
r#"<p1:root xmlns="ns2" xmlns:p1="ns1"/>"#,
|
r#"<p1:root xmlns="ns2" xmlns:p1="ns1"/>"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -183,7 +193,10 @@ fn writer_no_prefix_namespace_child() {
|
||||||
.build();
|
.build();
|
||||||
let root = Element::builder("root", "ns1").append(child).build();
|
let root = Element::builder("root", "ns1").append(child).build();
|
||||||
// TODO: Same remark as `writer_no_prefix_namespace`.
|
// 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]
|
#[test]
|
||||||
|
@ -194,7 +207,10 @@ fn writer_prefix_namespace_child() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.append(child)
|
.append(child)
|
||||||
.build();
|
.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]
|
#[test]
|
||||||
|
@ -209,7 +225,8 @@ fn writer_with_prefix_deduplicate() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.append(child)
|
.append(child)
|
||||||
.build();
|
.build();
|
||||||
assert_eq!(String::from(&root),
|
assert_eq!(
|
||||||
|
String::from(&root),
|
||||||
r#"<p1:root xmlns="ns2" xmlns:p1="ns1"><p1:child/></p1: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();
|
root.write_to(&mut writer).unwrap();
|
||||||
}
|
}
|
||||||
assert_eq!(String::from_utf8(writer).unwrap(), r#"<root xmlns="ns1"><3</root>"#);
|
assert_eq!(
|
||||||
|
String::from_utf8(writer).unwrap(),
|
||||||
|
r#"<root xmlns="ns1"><3</root>"#
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -363,10 +383,7 @@ fn namespace_prefixed() {
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(elem.name(), "features");
|
assert_eq!(elem.name(), "features");
|
||||||
assert_eq!(
|
assert_eq!(elem.ns(), "http://etherx.jabber.org/streams".to_owned(),);
|
||||||
elem.ns(),
|
|
||||||
"http://etherx.jabber.org/streams".to_owned(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[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>"
|
let elem: Element = "<stream:features xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client'><message xmlns='jabber:client' /></stream:features>"
|
||||||
.parse().unwrap();
|
.parse().unwrap();
|
||||||
assert_eq!(elem.name(), "features");
|
assert_eq!(elem.name(), "features");
|
||||||
assert_eq!(
|
assert_eq!(elem.ns(), "http://etherx.jabber.org/streams".to_owned(),);
|
||||||
elem.ns(),
|
|
||||||
"http://etherx.jabber.org/streams".to_owned(),
|
|
||||||
);
|
|
||||||
let child = elem.children().next().unwrap();
|
let child = elem.children().next().unwrap();
|
||||||
assert_eq!(child.name(), "message");
|
assert_eq!(child.name(), "message");
|
||||||
assert_eq!(child.ns(), "jabber:client".to_owned());
|
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>"
|
let elem: Element = "<stream xmlns='http://etherx.jabber.org/streams' xmlns:jabber='jabber:client'><jabber:message xmlns:jabber='jabber:client' /></stream>"
|
||||||
.parse().unwrap();
|
.parse().unwrap();
|
||||||
assert_eq!(elem.name(), "stream");
|
assert_eq!(elem.name(), "stream");
|
||||||
assert_eq!(
|
assert_eq!(elem.ns(), "http://etherx.jabber.org/streams".to_owned(),);
|
||||||
elem.ns(),
|
|
||||||
"http://etherx.jabber.org/streams".to_owned(),
|
|
||||||
);
|
|
||||||
let child = elem.children().next().unwrap();
|
let child = elem.children().next().unwrap();
|
||||||
assert_eq!(child.name(), "message");
|
assert_eq!(child.name(), "message");
|
||||||
assert_eq!(child.ns(), "jabber:client".to_owned());
|
assert_eq!(child.ns(), "jabber:client".to_owned());
|
||||||
|
|
Loading…
Reference in a new issue