minidom: Implement PartialEq manually for Node and Element
Move the NamespaceAwareCompare implementation from xmpp-parsers as Node and Element's PartialEq implementation. Thanks Astro! It's a lot more useful in tests to use `assert_eq!` than `assert!`, so we get both items compared (left and right) instead of a "it failed." message. This "breaks" comparison for these two structs in the sense that it is not strict object comparison anymore but it ensures that namespaces are all present in the compared objects. Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
ea6e7c24a3
commit
1c5551a917
20 changed files with 65 additions and 172 deletions
|
|
@ -68,7 +68,7 @@ pub fn escape(raw: &[u8]) -> Cow<[u8]> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
#[derive(Clone, Eq, Debug)]
|
||||
/// A struct representing a DOM Element.
|
||||
pub struct Element {
|
||||
prefix: Option<String>,
|
||||
|
|
@ -95,6 +95,31 @@ impl FromStr for Element {
|
|||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Element {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
if self.name() == other.name() && self.ns() == other.ns() && self.attrs().eq(other.attrs())
|
||||
{
|
||||
let child_elems = self.children().count();
|
||||
let text_is_whitespace = self
|
||||
.texts()
|
||||
.all(|text| text.chars().all(char::is_whitespace));
|
||||
if child_elems > 0 && text_is_whitespace {
|
||||
// Ignore all the whitespace text nodes
|
||||
self.children()
|
||||
.zip(other.children())
|
||||
.all(|(node1, node2)| node1 == node2)
|
||||
} else {
|
||||
// Compare with text nodes
|
||||
self.nodes()
|
||||
.zip(other.nodes())
|
||||
.all(|(node1, node2)| node1 == node2)
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Element {
|
||||
fn new<NS: Into<NamespaceSet>>(
|
||||
name: String,
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use quick_xml::events::{BytesText, Event};
|
|||
use quick_xml::Writer as EventWriter;
|
||||
|
||||
/// A node in an element tree.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, Eq)]
|
||||
pub enum Node {
|
||||
/// An `Element`.
|
||||
Element(Element),
|
||||
|
|
@ -208,3 +208,13 @@ impl From<ElementBuilder> for Node {
|
|||
Node::Element(builder.build())
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Node {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(&Node::Element(ref elem1), &Node::Element(ref elem2)) => elem1 == elem2,
|
||||
(&Node::Text(ref text1), &Node::Text(ref text2)) => text1 == text2,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue