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
52
src/mam.rs
52
src/mam.rs
|
|
@ -25,12 +25,16 @@ pub struct Query {
|
|||
pub set: Option<Set>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Result_ {
|
||||
pub queryid: String,
|
||||
pub id: String,
|
||||
pub forwarded: Forwarded,
|
||||
}
|
||||
generate_element_with_children!(
|
||||
Result_, "result", MAM,
|
||||
attributes: [
|
||||
id: String = "id" => required,
|
||||
queryid: String = "queryid" => required,
|
||||
],
|
||||
child: (
|
||||
forwarded: Forwarded = ("forwarded", FORWARD) => Forwarded
|
||||
)
|
||||
);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
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 {
|
||||
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 {
|
||||
fn from(fin: Fin) -> Element {
|
||||
Element::builder("fin")
|
||||
|
|
|
|||
Loading…
Reference in a new issue