xmpp-parsers: Convert blocking to xso
This commit is contained in:
parent
ce0f3bec0e
commit
2df243966f
2 changed files with 31 additions and 76 deletions
|
|
@ -9,8 +9,6 @@ use xso::{AsXml, FromXml};
|
||||||
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
|
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
|
||||||
use crate::ns;
|
use crate::ns;
|
||||||
use jid::Jid;
|
use jid::Jid;
|
||||||
use minidom::Element;
|
|
||||||
use xso::error::FromElementError;
|
|
||||||
|
|
||||||
/// The element requesting the blocklist, the result iq will contain a
|
/// The element requesting the blocklist, the result iq will contain a
|
||||||
/// [BlocklistResult].
|
/// [BlocklistResult].
|
||||||
|
|
@ -20,70 +18,40 @@ pub struct BlocklistRequest;
|
||||||
|
|
||||||
impl IqGetPayload for BlocklistRequest {}
|
impl IqGetPayload for BlocklistRequest {}
|
||||||
|
|
||||||
macro_rules! generate_blocking_element {
|
/// The element containing the current blocklist, as a reply from
|
||||||
($(#[$meta:meta])* $elem:ident, $name:tt) => (
|
/// [BlocklistRequest].
|
||||||
$(#[$meta])*
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
||||||
#[derive(Debug, Clone)]
|
#[xml(namespace = ns::BLOCKING, name = "blocklist")]
|
||||||
pub struct $elem {
|
pub struct BlocklistResult {
|
||||||
/// List of JIDs affected by this command.
|
/// List of JIDs affected by this command.
|
||||||
|
#[xml(extract(n = .., name = "item", fields(attribute(name = "jid", type_ = Jid))))]
|
||||||
pub items: Vec<Jid>,
|
pub items: Vec<Jid>,
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<Element> for $elem {
|
|
||||||
type Error = FromElementError;
|
|
||||||
|
|
||||||
fn try_from(elem: Element) -> Result<$elem, FromElementError> {
|
|
||||||
check_self!(elem, $name, BLOCKING);
|
|
||||||
check_no_attributes!(elem, $name);
|
|
||||||
let mut items = vec!();
|
|
||||||
for child in elem.children() {
|
|
||||||
check_child!(child, "item", BLOCKING);
|
|
||||||
check_no_unknown_attributes!(child, "item", ["jid"]);
|
|
||||||
check_no_children!(child, "item");
|
|
||||||
items.push(get_attr!(child, "jid", Required));
|
|
||||||
}
|
|
||||||
Ok($elem { items })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<$elem> for Element {
|
|
||||||
fn from(elem: $elem) -> Element {
|
|
||||||
Element::builder($name, ns::BLOCKING)
|
|
||||||
.append_all(elem.items.into_iter().map(|jid| {
|
|
||||||
Element::builder("item", ns::BLOCKING)
|
|
||||||
.attr("jid", jid)
|
|
||||||
}))
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_blocking_element!(
|
|
||||||
/// The element containing the current blocklist, as a reply from
|
|
||||||
/// [BlocklistRequest].
|
|
||||||
BlocklistResult,
|
|
||||||
"blocklist"
|
|
||||||
);
|
|
||||||
|
|
||||||
impl IqResultPayload for BlocklistResult {}
|
impl IqResultPayload for BlocklistResult {}
|
||||||
|
|
||||||
|
/// A query to block one or more JIDs.
|
||||||
// TODO: Prevent zero elements from being allowed.
|
// TODO: Prevent zero elements from being allowed.
|
||||||
generate_blocking_element!(
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
||||||
/// A query to block one or more JIDs.
|
#[xml(namespace = ns::BLOCKING, name = "block")]
|
||||||
Block,
|
pub struct Block {
|
||||||
"block"
|
/// List of JIDs affected by this command.
|
||||||
);
|
#[xml(extract(n = .., name = "item", fields(attribute(name = "jid", type_ = Jid))))]
|
||||||
|
pub items: Vec<Jid>,
|
||||||
|
}
|
||||||
|
|
||||||
impl IqSetPayload for Block {}
|
impl IqSetPayload for Block {}
|
||||||
|
|
||||||
generate_blocking_element!(
|
/// A query to unblock one or more JIDs, or all of them.
|
||||||
/// A query to unblock one or more JIDs, or all of them.
|
///
|
||||||
///
|
/// Warning: not putting any JID there means clearing out the blocklist.
|
||||||
/// Warning: not putting any JID there means clearing out the blocklist.
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
||||||
Unblock,
|
#[xml(namespace = ns::BLOCKING, name = "unblock")]
|
||||||
"unblock"
|
pub struct Unblock {
|
||||||
);
|
/// List of JIDs affected by this command.
|
||||||
|
#[xml(extract(n = .., name = "item", fields(attribute(name = "jid", type_ = Jid))))]
|
||||||
|
pub items: Vec<Jid>,
|
||||||
|
}
|
||||||
|
|
||||||
impl IqSetPayload for Unblock {}
|
impl IqSetPayload for Unblock {}
|
||||||
|
|
||||||
|
|
@ -94,9 +62,10 @@ pub struct Blocked;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use xso::error::Error;
|
use xso::error::{Error, FromElementError};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use minidom::Element;
|
||||||
|
|
||||||
#[cfg(target_pointer_width = "32")]
|
#[cfg(target_pointer_width = "32")]
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -176,7 +145,7 @@ mod tests {
|
||||||
FromElementError::Invalid(Error::Other(string)) => string,
|
FromElementError::Invalid(Error::Other(string)) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
};
|
};
|
||||||
assert_eq!(message, "Unknown attribute in blocklist element.");
|
assert_eq!(message, "Unknown attribute in BlocklistResult element.");
|
||||||
|
|
||||||
let elem: Element = "<block xmlns='urn:xmpp:blocking' coucou=''/>"
|
let elem: Element = "<block xmlns='urn:xmpp:blocking' coucou=''/>"
|
||||||
.parse()
|
.parse()
|
||||||
|
|
@ -186,7 +155,7 @@ mod tests {
|
||||||
FromElementError::Invalid(Error::Other(string)) => string,
|
FromElementError::Invalid(Error::Other(string)) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
};
|
};
|
||||||
assert_eq!(message, "Unknown attribute in block element.");
|
assert_eq!(message, "Unknown attribute in Block element.");
|
||||||
|
|
||||||
let elem: Element = "<unblock xmlns='urn:xmpp:blocking' coucou=''/>"
|
let elem: Element = "<unblock xmlns='urn:xmpp:blocking' coucou=''/>"
|
||||||
.parse()
|
.parse()
|
||||||
|
|
@ -196,7 +165,7 @@ mod tests {
|
||||||
FromElementError::Invalid(Error::Other(string)) => string,
|
FromElementError::Invalid(Error::Other(string)) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
};
|
};
|
||||||
assert_eq!(message, "Unknown attribute in unblock element.");
|
assert_eq!(message, "Unknown attribute in Unblock element.");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "disable-validation"))]
|
#[cfg(not(feature = "disable-validation"))]
|
||||||
|
|
|
||||||
|
|
@ -366,20 +366,6 @@ macro_rules! check_self {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! check_child {
|
|
||||||
($elem:ident, $name:tt, $ns:ident) => {
|
|
||||||
check_child!($elem, $name, $ns, $name);
|
|
||||||
};
|
|
||||||
($elem:ident, $name:tt, $ns:ident, $pretty_name:tt) => {
|
|
||||||
if !$elem.is($name, crate::ns::$ns) {
|
|
||||||
return Err(xso::error::Error::Other(
|
|
||||||
concat!("This is not a ", $pretty_name, " element.").into(),
|
|
||||||
)
|
|
||||||
.into());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! check_ns_only {
|
macro_rules! check_ns_only {
|
||||||
($elem:ident, $name:tt, $ns:ident) => {
|
($elem:ident, $name:tt, $ns:ident) => {
|
||||||
if !$elem.has_ns(crate::ns::$ns) {
|
if !$elem.has_ns(crate::ns::$ns) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue