ScanElement now owns the Element

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-04-21 17:33:16 +02:00
parent d97bc62121
commit 1e02dd2d1f

View file

@ -85,7 +85,7 @@ impl<'a> PartialEq<Node> for ScanNode<'a> {
match (&self.node, other) { match (&self.node, other) {
(Node::Text(text1), Node::Text(text2)) => text1 == text2, (Node::Text(text1), Node::Text(text2)) => text1 == text2,
(Node::Element(elem1), Node::Element(elem2)) => { (Node::Element(elem1), Node::Element(elem2)) => {
ScanElement::new(&elem1).with_context(self.context) == elem2 ScanElement::new(elem1.clone()).with_context(self.context) == elem2
} }
_ => false, _ => false,
} }
@ -202,12 +202,12 @@ impl<'a> PartialEq<Vec<Node>> for ScanNodes<'a, NonStrictComparison> {
/// changes the way the comparison is done. /// changes the way the comparison is done.
/// Also uses the custom ScanNode implementation. /// Also uses the custom ScanNode implementation.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ScanElement<'a, 'b> { pub struct ScanElement<'a> {
elem: &'a Element, elem: Element,
context: Option<&'b Context>, context: Option<&'a Context>,
} }
impl<'a, 'b> Deref for ScanElement<'a, 'b> { impl<'a> Deref for ScanElement<'a> {
type Target = Element; type Target = Element;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@ -215,8 +215,8 @@ impl<'a, 'b> Deref for ScanElement<'a, 'b> {
} }
} }
impl<'a> ScanElement<'a, 'static> { impl<'a> ScanElement<'a> {
pub fn new(elem: &'a Element) -> ScanElement { pub fn new(elem: Element) -> ScanElement<'a> {
Self { Self {
elem, elem,
context: None, context: None,
@ -224,8 +224,8 @@ impl<'a> ScanElement<'a, 'static> {
} }
} }
impl<'a, 'b> ScanElement<'a, 'b> { impl<'a> ScanElement<'a> {
pub fn with_context(self, context: Option<&'b Context>) -> ScanElement<'a, 'b> { pub fn with_context(self, context: Option<&'a Context>) -> ScanElement<'a> {
Self { Self {
elem: self.elem, elem: self.elem,
context, context,
@ -233,7 +233,7 @@ impl<'a, 'b> ScanElement<'a, 'b> {
} }
} }
impl<'a, 'b> PartialEq<&Element> for ScanElement<'a, 'b> { impl<'a> PartialEq<&Element> for ScanElement<'a> {
fn eq(&self, other: &&Element) -> bool { fn eq(&self, other: &&Element) -> bool {
let self_ns = self.elem.ns(); let self_ns = self.elem.ns();
if self.elem.name() == other.name() && self_ns == other.ns() { if self.elem.name() == other.name() && self_ns == other.ns() {
@ -375,7 +375,7 @@ mod tests {
.parse() .parse()
.unwrap(); .unwrap();
let elem2: Element = "<presence xmlns='foo'><foo/></presence>".parse().unwrap(); let elem2: Element = "<presence xmlns='foo'><foo/></presence>".parse().unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1);
assert_eq!(scan1, &elem2); assert_eq!(scan1, &elem2);
} }
@ -388,7 +388,7 @@ mod tests {
let elem2: Element = "<presence xmlns='foo'>\n\tfoo\t</presence>" let elem2: Element = "<presence xmlns='foo'>\n\tfoo\t</presence>"
.parse() .parse()
.unwrap(); .unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1);
assert_ne!(scan1, &elem2); assert_ne!(scan1, &elem2);
} }
@ -396,7 +396,7 @@ mod tests {
#[test] #[test]
fn compare_element_strict_attributes_success() { fn compare_element_strict_attributes_success() {
let elem1: Element = "<presence xmlns='foo'/>".parse().unwrap(); let elem1: Element = "<presence xmlns='foo'/>".parse().unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1.clone());
assert_eq!(scan1, &elem1); assert_eq!(scan1, &elem1);
@ -410,7 +410,7 @@ mod tests {
</presence>" </presence>"
.parse() .parse()
.unwrap(); .unwrap();
let scan2 = ScanElement::new(&elem2); let scan2 = ScanElement::new(elem2);
assert_eq!(scan2, &elem3); assert_eq!(scan2, &elem3);
} }
@ -419,7 +419,7 @@ mod tests {
fn compare_element_strict_attributes_failure() { fn compare_element_strict_attributes_failure() {
let elem1: Element = "<presence xmlns='foo' foo='bar'/>".parse().unwrap(); let elem1: Element = "<presence xmlns='foo' foo='bar'/>".parse().unwrap();
let elem2: Element = "<presence xmlns='foo'/>".parse().unwrap(); let elem2: Element = "<presence xmlns='foo'/>".parse().unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1);
assert_ne!(scan1, &elem2); assert_ne!(scan1, &elem2);
} }
@ -443,7 +443,7 @@ mod tests {
</presence>" </presence>"
.parse() .parse()
.unwrap(); .unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1);
assert_eq!(scan1, &elem2); assert_eq!(scan1, &elem2);
} }
@ -464,7 +464,7 @@ mod tests {
</presence>" </presence>"
.parse() .parse()
.unwrap(); .unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1);
assert_ne!(scan1, &elem2); assert_ne!(scan1, &elem2);
} }
@ -474,7 +474,7 @@ mod tests {
let elem1: Element = "<presence scansion:strict='false' xmlns='foo'/>" let elem1: Element = "<presence scansion:strict='false' xmlns='foo'/>"
.parse() .parse()
.unwrap(); .unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1.clone());
assert_eq!(scan1, &elem1); assert_eq!(scan1, &elem1);
@ -488,7 +488,7 @@ mod tests {
</presence>" </presence>"
.parse() .parse()
.unwrap(); .unwrap();
let scan2 = ScanElement::new(&elem2); let scan2 = ScanElement::new(elem2);
assert_eq!(scan2, &elem3); assert_eq!(scan2, &elem3);
} }
@ -499,7 +499,7 @@ mod tests {
.parse() .parse()
.unwrap(); .unwrap();
let elem2: Element = "<presence xmlns='foo' />".parse().unwrap(); let elem2: Element = "<presence xmlns='foo' />".parse().unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1);
assert_ne!(scan1, &elem2); assert_ne!(scan1, &elem2);
@ -513,7 +513,7 @@ mod tests {
</presence>" </presence>"
.parse() .parse()
.unwrap(); .unwrap();
let scan2 = ScanElement::new(&elem2); let scan2 = ScanElement::new(elem2);
assert_ne!(scan2, &elem3); assert_ne!(scan2, &elem3);
} }
@ -524,7 +524,7 @@ mod tests {
.parse() .parse()
.unwrap(); .unwrap();
let elem2: Element = "<presence xmlns='foo'><foo/></presence>".parse().unwrap(); let elem2: Element = "<presence xmlns='foo'><foo/></presence>".parse().unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1);
assert_eq!(scan1, &elem2); assert_eq!(scan1, &elem2);
@ -533,7 +533,7 @@ mod tests {
let elem4: Element = "<presence xmlns='jabber:client'><foo/></presence>" let elem4: Element = "<presence xmlns='jabber:client'><foo/></presence>"
.parse() .parse()
.unwrap(); .unwrap();
let scan3 = ScanElement::new(&elem3); let scan3 = ScanElement::new(elem3);
assert_eq!(scan3, &elem4); assert_eq!(scan3, &elem4);
} }
@ -550,7 +550,7 @@ mod tests {
</message>" </message>"
.parse() .parse()
.unwrap(); .unwrap();
let scan2 = ScanElement::new(&elem2); let scan2 = ScanElement::new(elem2);
assert_ne!(scan2, &elem3); assert_ne!(scan2, &elem3);
} }
@ -567,7 +567,7 @@ mod tests {
</message>" </message>"
.parse() .parse()
.unwrap(); .unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1);
assert_ne!(scan1, &elem2); assert_ne!(scan1, &elem2);
} }
@ -578,7 +578,7 @@ mod tests {
.parse() .parse()
.unwrap(); .unwrap();
let elem2: Element = "<message xmlns='foo' id='some-id' />".parse().unwrap(); let elem2: Element = "<message xmlns='foo' id='some-id' />".parse().unwrap();
let scan1 = ScanElement::new(&elem1); let scan1 = ScanElement::new(elem1);
assert_eq!(scan1, &elem2); assert_eq!(scan1, &elem2);
} }