mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
mam: Use a macro to generate Result_.
This commit is contained in:
parent
965f6a1f83
commit
d9f2af6c97
2 changed files with 13 additions and 42 deletions
|
@ -543,6 +543,9 @@ macro_rules! generate_element_with_children {
|
||||||
($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, child: ($(#[$child_meta:meta])* $child_ident:ident: $child_type:ty = ($child_name:tt, $child_ns:ident) => $child_constructor:ident)) => (
|
($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, child: ($(#[$child_meta:meta])* $child_ident:ident: $child_type:ty = ($child_name:tt, $child_ns:ident) => $child_constructor:ident)) => (
|
||||||
generate_element_with_children!($(#[$meta])* $elem, $name, $ns, attributes: [], child: ($(#[$child_meta])* $child_ident: $child_type = ($child_name, $child_ns) => $child_constructor));
|
generate_element_with_children!($(#[$meta])* $elem, $name, $ns, attributes: [], child: ($(#[$child_meta])* $child_ident: $child_type = ($child_name, $child_ns) => $child_constructor));
|
||||||
);
|
);
|
||||||
|
($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*,], child: ($(#[$child_meta:meta])* $child_ident:ident: $child_type:ty = ($child_name:tt, $child_ns:ident) => $child_constructor:ident)) => (
|
||||||
|
generate_element_with_children!($(#[$meta])* $elem, $name, $ns, attributes: [$($(#[$attr_meta])* $attr: $attr_type = $attr_name => $attr_action),*], child: ($(#[$child_meta])* $child_ident: $child_type = ($child_name, $child_ns) => $child_constructor));
|
||||||
|
);
|
||||||
($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*], child: ($(#[$child_meta:meta])* $child_ident:ident: $child_type:ty = ($child_name:tt, $child_ns:ident) => $child_constructor:ident)) => (
|
($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*], child: ($(#[$child_meta:meta])* $child_ident:ident: $child_type:ty = ($child_name:tt, $child_ns:ident) => $child_constructor:ident)) => (
|
||||||
$(#[$meta])*
|
$(#[$meta])*
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
52
src/mam.rs
52
src/mam.rs
|
@ -25,12 +25,16 @@ pub struct Query {
|
||||||
pub set: Option<Set>,
|
pub set: Option<Set>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
generate_element_with_children!(
|
||||||
pub struct Result_ {
|
Result_, "result", MAM,
|
||||||
pub queryid: String,
|
attributes: [
|
||||||
pub id: String,
|
id: String = "id" => required,
|
||||||
pub forwarded: Forwarded,
|
queryid: String = "queryid" => required,
|
||||||
}
|
],
|
||||||
|
child: (
|
||||||
|
forwarded: Forwarded = ("forwarded", FORWARD) => Forwarded
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Fin {
|
pub struct Fin {
|
||||||
|
@ -74,31 +78,6 @@ impl TryFrom<Element> for Query {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Element> for Result_ {
|
|
||||||
type Err = Error;
|
|
||||||
|
|
||||||
fn try_from(elem: Element) -> Result<Result_, Error> {
|
|
||||||
check_self!(elem, "result", MAM);
|
|
||||||
check_no_unknown_attributes!(elem, "result", ["queryid", "id"]);
|
|
||||||
let mut forwarded = None;
|
|
||||||
for child in elem.children() {
|
|
||||||
if child.is("forwarded", ns::FORWARD) {
|
|
||||||
forwarded = Some(Forwarded::try_from(child.clone())?);
|
|
||||||
} else {
|
|
||||||
return Err(Error::ParseError("Unknown child in result element."));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let forwarded = forwarded.ok_or(Error::ParseError("Mandatory forwarded element missing in result."))?;
|
|
||||||
let queryid = get_attr!(elem, "queryid", required);
|
|
||||||
let id = get_attr!(elem, "id", required);
|
|
||||||
Ok(Result_ {
|
|
||||||
queryid,
|
|
||||||
id,
|
|
||||||
forwarded,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<Element> for Fin {
|
impl TryFrom<Element> for Fin {
|
||||||
type Err = Error;
|
type Err = Error;
|
||||||
|
|
||||||
|
@ -168,17 +147,6 @@ impl From<Query> for Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Result_> for Element {
|
|
||||||
fn from(result: Result_) -> Element {
|
|
||||||
Element::builder("result")
|
|
||||||
.ns(ns::MAM)
|
|
||||||
.attr("queryid", result.queryid)
|
|
||||||
.attr("id", result.id)
|
|
||||||
.append(result.forwarded)
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Fin> for Element {
|
impl From<Fin> for Element {
|
||||||
fn from(fin: Fin) -> Element {
|
fn from(fin: Fin) -> Element {
|
||||||
Element::builder("fin")
|
Element::builder("fin")
|
||||||
|
|
Loading…
Reference in a new issue