cargo fmt
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
c847ceb3bf
commit
6ede9da169
3 changed files with 237 additions and 190 deletions
164
src/element.rs
164
src/element.rs
|
@ -39,13 +39,13 @@
|
|||
//! ```
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Deref;
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::{Client, ClientName};
|
||||
use crate::types::VariableAttr;
|
||||
use crate::parsers::parse_variable;
|
||||
use crate::types::VariableAttr;
|
||||
use crate::{Client, ClientName};
|
||||
|
||||
use jid::BareJid;
|
||||
use minidom::{Element, Node};
|
||||
|
@ -84,12 +84,8 @@ impl ScanNode {
|
|||
impl PartialEq<Node> for ScanNode {
|
||||
fn eq(&self, other: &Node) -> bool {
|
||||
match (&self.node, other) {
|
||||
(Node::Text(text1), Node::Text(text2)) => {
|
||||
text1 == text2
|
||||
},
|
||||
(Node::Element(elem1), Node::Element(elem2)) => {
|
||||
ScanElement::new(&elem1) == elem2
|
||||
},
|
||||
(Node::Text(text1), Node::Text(text2)) => text1 == text2,
|
||||
(Node::Element(elem1), Node::Element(elem2)) => ScanElement::new(&elem1) == elem2,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
@ -116,27 +112,21 @@ fn filter_whitespace_nodes(nodes: Vec<Node>) -> Vec<Node> {
|
|||
(Some(type_), acc)
|
||||
};
|
||||
|
||||
let rm_empty_text = |node: &Node| {
|
||||
match node {
|
||||
Node::Text(text) => {
|
||||
text.trim().len() != 0
|
||||
},
|
||||
let rm_empty_text = |node: &Node| match node {
|
||||
Node::Text(text) => text.trim().len() != 0,
|
||||
_ => true,
|
||||
}
|
||||
};
|
||||
|
||||
let nodes = nodes
|
||||
.into_iter()
|
||||
.fold((None::<NodeType>, vec![]), filter_nodes).1;
|
||||
.fold((None::<NodeType>, vec![]), filter_nodes)
|
||||
.1;
|
||||
|
||||
// Don't remove possibly significant whitespace text leaves
|
||||
if nodes.iter().count() == 1 {
|
||||
nodes
|
||||
} else {
|
||||
nodes
|
||||
.into_iter()
|
||||
.filter(rm_empty_text)
|
||||
.collect()
|
||||
nodes.into_iter().filter(rm_empty_text).collect()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -148,13 +138,19 @@ struct ScanNodes<T: Debug> {
|
|||
|
||||
impl ScanNodes<NonStrictComparison> {
|
||||
fn new(nodes: Vec<Node>) -> ScanNodes<NonStrictComparison> {
|
||||
Self { nodes, _strict: PhantomData }
|
||||
Self {
|
||||
nodes,
|
||||
_strict: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ScanNodes<StrictComparison> {
|
||||
fn new_strict(nodes: Vec<Node>) -> ScanNodes<StrictComparison> {
|
||||
Self { nodes, _strict: PhantomData }
|
||||
Self {
|
||||
nodes,
|
||||
_strict: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -185,10 +181,10 @@ impl PartialEq<Vec<Node>> for ScanNodes<NonStrictComparison> {
|
|||
// Maps nodes to their comparison result
|
||||
.fold(true, |res, node| {
|
||||
let scan = ScanNode::new(node);
|
||||
res &&
|
||||
filtered_other.iter().find(|onode| {
|
||||
&&scan == onode
|
||||
}).is_some()
|
||||
res && filtered_other
|
||||
.iter()
|
||||
.find(|onode| &&scan == onode)
|
||||
.is_some()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -214,22 +210,26 @@ impl<'a, 'b> Deref for ScanElement<'a, 'b> {
|
|||
|
||||
impl<'a> ScanElement<'a, 'static> {
|
||||
pub fn new(elem: &'a Element) -> ScanElement {
|
||||
Self { elem, context: None }
|
||||
Self {
|
||||
elem,
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a, 'b> ScanElement<'a, 'b> {
|
||||
impl<'a, 'b> ScanElement<'a, 'b> {
|
||||
pub fn with_context(self, context: &'b HashMap<ClientName, Client>) -> ScanElement<'a, 'b> {
|
||||
Self { elem: self.elem, context: Some(context) }
|
||||
Self {
|
||||
elem: self.elem,
|
||||
context: Some(context),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> PartialEq<&Element> for ScanElement<'a, 'b> {
|
||||
fn eq(&self, other: &&Element) -> bool {
|
||||
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() {
|
||||
let strict_attr = self.elem.attr("scansion:strict");
|
||||
|
||||
// Force true if scansion:strict is set or if a tag isn't in the default ns.
|
||||
|
@ -304,6 +304,8 @@ mod tests {
|
|||
use super::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
use jid::Jid;
|
||||
|
||||
#[test]
|
||||
fn compare_nodes_simple() {
|
||||
let text1 = Node::Text(String::from("\t\t"));
|
||||
|
@ -360,7 +362,9 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn compare_element_non_strict_whitespace_success() {
|
||||
let elem1: Element = "<presence xmlns='foo'>\n\t<foo/></presence>".parse().unwrap();
|
||||
let elem1: Element = "<presence xmlns='foo'>\n\t<foo/></presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem2: Element = "<presence xmlns='foo'><foo/></presence>".parse().unwrap();
|
||||
let scan1 = ScanElement::new(&elem1);
|
||||
|
||||
|
@ -369,8 +373,12 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn compare_element_non_strict_whitespace_failure() {
|
||||
let elem1: Element = "<presence scansion:strict='false' xmlns='foo'>\n\tfoo</presence>".parse().unwrap();
|
||||
let elem2: Element = "<presence xmlns='foo'>\n\tfoo\t</presence>".parse().unwrap();
|
||||
let elem1: Element = "<presence scansion:strict='false' xmlns='foo'>\n\tfoo</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem2: Element = "<presence xmlns='foo'>\n\tfoo\t</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan1 = ScanElement::new(&elem1);
|
||||
|
||||
assert_ne!(scan1, &elem2);
|
||||
|
@ -385,10 +393,14 @@ mod tests {
|
|||
|
||||
let elem2: Element = "<presence scansion:strict='true' xmlns='jabber:client'>
|
||||
<x xmlns='http://jabber.org/protocol/muc'/>
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem3: Element = "<presence xmlns='jabber:client'>
|
||||
<x xmlns='http://jabber.org/protocol/muc'/>
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan2 = ScanElement::new(&elem2);
|
||||
|
||||
assert_eq!(scan2, &elem3);
|
||||
|
@ -410,14 +422,18 @@ mod tests {
|
|||
<x xmlns='vcard-temp:x:update'>
|
||||
<photo/>
|
||||
</x>
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
// The same, minus 'scansion:strict'
|
||||
let elem2: Element = "<presence xmlns='jabber:client'>
|
||||
<x xmlns='http://jabber.org/protocol/muc' />
|
||||
<x xmlns='vcard-temp:x:update'>
|
||||
<photo/>
|
||||
</x>
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan1 = ScanElement::new(&elem1);
|
||||
|
||||
assert_eq!(scan1, &elem2);
|
||||
|
@ -430,11 +446,15 @@ mod tests {
|
|||
<x xmlns='vcard-temp:x:update'>
|
||||
<photo/>
|
||||
</x>
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem2: Element = "<presence scansion:strict='true' xmlns='jabber:client'>
|
||||
<x xmlns='http://jabber.org/protocol/muc' />
|
||||
<x xmlns='vcard-temp:x:update'/>
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan1 = ScanElement::new(&elem1);
|
||||
|
||||
assert_ne!(scan1, &elem2);
|
||||
|
@ -442,17 +462,23 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn compare_element_non_strict_attributes_success() {
|
||||
let elem1: Element = "<presence scansion:strict='false' xmlns='foo'/>".parse().unwrap();
|
||||
let elem1: Element = "<presence scansion:strict='false' xmlns='foo'/>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan1 = ScanElement::new(&elem1);
|
||||
|
||||
assert_eq!(scan1, &elem1);
|
||||
|
||||
let elem2: Element = "<presence xmlns='jabber:client'>
|
||||
<x xmlns='http://jabber.org/protocol/muc' scansion:strict='false' />
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem3: Element = "<presence xmlns='jabber:client' foo='bar'>
|
||||
<x xmlns='http://jabber.org/protocol/muc' baz='qxx' />
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan2 = ScanElement::new(&elem2);
|
||||
|
||||
assert_eq!(scan2, &elem3);
|
||||
|
@ -460,7 +486,9 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn compare_element_non_strict_attributes_failure() {
|
||||
let elem1: Element = "<presence scansion:strict='false' foo='bar' xmlns='foo'/>".parse().unwrap();
|
||||
let elem1: Element = "<presence scansion:strict='false' foo='bar' xmlns='foo'/>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem2: Element = "<presence xmlns='foo' />".parse().unwrap();
|
||||
let scan1 = ScanElement::new(&elem1);
|
||||
|
||||
|
@ -468,10 +496,14 @@ mod tests {
|
|||
|
||||
let elem2: Element = "<presence xmlns='jabber:client' foo='bar'>
|
||||
<x xmlns='http://jabber.org/protocol/muc'/>
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem3: Element = "<presence xmlns='jabber:client'>
|
||||
<x xmlns='http://jabber.org/protocol/muc'/>
|
||||
</presence>".parse().unwrap();
|
||||
</presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan2 = ScanElement::new(&elem2);
|
||||
|
||||
assert_ne!(scan2, &elem3);
|
||||
|
@ -479,7 +511,9 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn compare_element_non_strict_elem_success() {
|
||||
let elem1: Element = "<presence scansion:strict='false' xmlns='foo'/>".parse().unwrap();
|
||||
let elem1: Element = "<presence scansion:strict='false' xmlns='foo'/>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem2: Element = "<presence xmlns='foo'><foo/></presence>".parse().unwrap();
|
||||
let scan1 = ScanElement::new(&elem1);
|
||||
|
||||
|
@ -487,7 +521,9 @@ mod tests {
|
|||
|
||||
// 'jabber:client' is non strict by default
|
||||
let elem3: Element = "<presence xmlns='jabber:client'/>".parse().unwrap();
|
||||
let elem4: Element = "<presence xmlns='jabber:client'><foo/></presence>".parse().unwrap();
|
||||
let elem4: Element = "<presence xmlns='jabber:client'><foo/></presence>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan3 = ScanElement::new(&elem3);
|
||||
|
||||
assert_eq!(scan3, &elem4);
|
||||
|
@ -497,10 +533,14 @@ mod tests {
|
|||
fn compare_element_non_strict_elem_failure() {
|
||||
let elem2: Element = "<message scansion:strict='false' xmlns='jabber:client'>
|
||||
<body>foo</body>
|
||||
</message>".parse().unwrap();
|
||||
</message>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem3: Element = "<message xmlns='jabber:client'>
|
||||
<body>bar</body>
|
||||
</message>".parse().unwrap();
|
||||
</message>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan2 = ScanElement::new(&elem2);
|
||||
|
||||
assert_ne!(scan2, &elem3);
|
||||
|
@ -510,10 +550,14 @@ mod tests {
|
|||
fn compare_element_propagate_strictness() {
|
||||
let elem1: Element = "<message scansion:strict='true' xmlns='jabber:client'>
|
||||
<foo><bar /></foo>
|
||||
</message>".parse().unwrap();
|
||||
</message>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem2: Element = "<message scansion:strict='true' xmlns='jabber:client'>
|
||||
<foo><bar baz='qxx' /></foo>
|
||||
</message>".parse().unwrap();
|
||||
</message>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan1 = ScanElement::new(&elem1);
|
||||
|
||||
assert_ne!(scan1, &elem2);
|
||||
|
@ -521,9 +565,10 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn ignore_attr_val_success() {
|
||||
let elem1: Element = "<message scansion:strict='true' xmlns='jabber:client' id='{scansion:any}' />"
|
||||
.parse().unwrap();
|
||||
let elem2: Element = "<message xmlns='jabber:client' id='some-id' />".parse().unwrap();
|
||||
let elem1: Element = "<message xmlns='foo' id='{scansion:any}' />"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem2: Element = "<message xmlns='foo' id='some-id' />".parse().unwrap();
|
||||
let scan1 = ScanElement::new(&elem1);
|
||||
|
||||
assert_eq!(scan1, &elem2);
|
||||
|
@ -540,8 +585,11 @@ mod tests {
|
|||
};
|
||||
|
||||
let elem1: Element = "<message xmlns='foo' to=\"${louise's full JID}\" />"
|
||||
.parse().unwrap();
|
||||
let elem2: Element = "<message xmlns='foo' to='louise@example.com' />".parse().unwrap();
|
||||
.parse()
|
||||
.unwrap();
|
||||
let elem2: Element = "<message xmlns='foo' to='louise@example.com' />"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let scan1 = ScanElement::new(&elem1).with_context(&clients);
|
||||
|
||||
assert_eq!(scan1, &elem2);
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
// 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/.
|
||||
|
||||
use crate::types::{Action, Client, ClientName, Metadata, VariableAttr, Spec};
|
||||
use crate::types::{Action, Client, ClientName, Metadata, Spec, VariableAttr};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
|
@ -281,21 +281,20 @@ pub fn parse_spec(i: &str) -> Result<Spec, Token> {
|
|||
|
||||
pub fn parse_variable(s: Span) -> IResult<Span, VariableAttr> {
|
||||
let (s, (_, name, attr, _)) = tuple((
|
||||
tag("${"), take_until_tags(vec![
|
||||
"'s full JID",
|
||||
"'s JID",
|
||||
].into_iter(),
|
||||
"}",
|
||||
),
|
||||
tag("${"),
|
||||
take_until_tags(vec!["'s full JID", "'s JID"].into_iter(), "}"),
|
||||
alt((tag("'s full JID"), tag("'s JID"))),
|
||||
tag("}"),
|
||||
))(s)?;
|
||||
|
||||
Ok((s, match *attr.fragment() {
|
||||
Ok((
|
||||
s,
|
||||
match *attr.fragment() {
|
||||
"'s full JID" => VariableAttr::FullJid(name.to_string()),
|
||||
"'s JID" => VariableAttr::BareJid(name.to_string()),
|
||||
_ => unreachable!(),
|
||||
}))
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue