mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
mam: Switch to Into/TryFrom.
This commit is contained in:
parent
2b96751e5f
commit
418956c720
1 changed files with 215 additions and 193 deletions
122
src/mam.rs
122
src/mam.rs
|
@ -52,13 +52,16 @@ pub struct Prefs {
|
|||
pub never: Vec<Jid>,
|
||||
}
|
||||
|
||||
pub fn parse_query(root: &Element) -> Result<Query, Error> {
|
||||
if !root.is("query", ns::MAM) {
|
||||
impl<'a> TryFrom<&'a Element> for Query {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(elem: &'a Element) -> Result<Query, Error> {
|
||||
if !elem.is("query", ns::MAM) {
|
||||
return Err(Error::ParseError("This is not a query element."));
|
||||
}
|
||||
let mut form = None;
|
||||
let mut set = None;
|
||||
for child in root.children() {
|
||||
for child in elem.children() {
|
||||
if child.is("x", ns::DATA_FORMS) {
|
||||
form = Some(DataForm::try_from(child)?);
|
||||
} else if child.is("set", ns::RSM) {
|
||||
|
@ -67,34 +70,38 @@ pub fn parse_query(root: &Element) -> Result<Query, Error> {
|
|||
return Err(Error::ParseError("Unknown child in query element."));
|
||||
}
|
||||
}
|
||||
let queryid = match root.attr("queryid") {
|
||||
let queryid = match elem.attr("queryid") {
|
||||
Some(queryid) => Some(queryid.to_owned()),
|
||||
None => None,
|
||||
};
|
||||
let node = match root.attr("node") {
|
||||
let node = match elem.attr("node") {
|
||||
Some(node) => Some(node.to_owned()),
|
||||
None => None,
|
||||
};
|
||||
Ok(Query { queryid, node, form, set })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_result(root: &Element) -> Result<Result_, Error> {
|
||||
if !root.is("result", ns::MAM) {
|
||||
impl<'a> TryFrom<&'a Element> for Result_ {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(elem: &'a Element) -> Result<Result_, Error> {
|
||||
if !elem.is("result", ns::MAM) {
|
||||
return Err(Error::ParseError("This is not a result element."));
|
||||
}
|
||||
let mut forwarded = None;
|
||||
for child in root.children() {
|
||||
for child in elem.children() {
|
||||
if child.is("forwarded", ns::FORWARD) {
|
||||
forwarded = Some(Forwarded::try_from(child)?);
|
||||
} else {
|
||||
return Err(Error::ParseError("Unknown child in result element."));
|
||||
}
|
||||
}
|
||||
let queryid = match root.attr("queryid") {
|
||||
let queryid = match elem.attr("queryid") {
|
||||
Some(queryid) => queryid.to_owned(),
|
||||
None => return Err(Error::ParseError("No 'queryid' attribute present in result.")),
|
||||
};
|
||||
let id = match root.attr("id") {
|
||||
let id = match elem.attr("id") {
|
||||
Some(id) => id.to_owned(),
|
||||
None => return Err(Error::ParseError("No 'id' attribute present in result.")),
|
||||
};
|
||||
|
@ -107,21 +114,25 @@ pub fn parse_result(root: &Element) -> Result<Result_, Error> {
|
|||
id,
|
||||
forwarded,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_fin(root: &Element) -> Result<Fin, Error> {
|
||||
if !root.is("fin", ns::MAM) {
|
||||
impl<'a> TryFrom<&'a Element> for Fin {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(elem: &'a Element) -> Result<Fin, Error> {
|
||||
if !elem.is("fin", ns::MAM) {
|
||||
return Err(Error::ParseError("This is not a fin element."));
|
||||
}
|
||||
let mut set = None;
|
||||
for child in root.children() {
|
||||
for child in elem.children() {
|
||||
if child.is("set", ns::RSM) {
|
||||
set = Some(Set::try_from(child)?);
|
||||
} else {
|
||||
return Err(Error::ParseError("Unknown child in fin element."));
|
||||
}
|
||||
}
|
||||
let complete = match root.attr("complete") {
|
||||
let complete = match elem.attr("complete") {
|
||||
Some(complete) => complete == "true",
|
||||
None => false,
|
||||
};
|
||||
|
@ -130,15 +141,19 @@ pub fn parse_fin(root: &Element) -> Result<Fin, Error> {
|
|||
}
|
||||
let set = set.unwrap();
|
||||
Ok(Fin { complete, set })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_prefs(root: &Element) -> Result<Prefs, Error> {
|
||||
if !root.is("prefs", ns::MAM) {
|
||||
impl<'a> TryFrom<&'a Element> for Prefs {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(elem: &'a Element) -> Result<Prefs, Error> {
|
||||
if !elem.is("prefs", ns::MAM) {
|
||||
return Err(Error::ParseError("This is not a prefs element."));
|
||||
}
|
||||
let mut always = vec!();
|
||||
let mut never = vec!();
|
||||
for child in root.children() {
|
||||
for child in elem.children() {
|
||||
if child.is("always", ns::MAM) {
|
||||
for jid_elem in child.children() {
|
||||
if !jid_elem.is("jid", ns::MAM) {
|
||||
|
@ -157,7 +172,7 @@ pub fn parse_prefs(root: &Element) -> Result<Prefs, Error> {
|
|||
return Err(Error::ParseError("Unknown child in prefs element."));
|
||||
}
|
||||
}
|
||||
let default_ = match root.attr("default") {
|
||||
let default_ = match elem.attr("default") {
|
||||
Some("always") => Some(DefaultPrefs::Always),
|
||||
Some("never") => Some(DefaultPrefs::Never),
|
||||
Some("roster") => Some(DefaultPrefs::Roster),
|
||||
|
@ -166,60 +181,68 @@ pub fn parse_prefs(root: &Element) -> Result<Prefs, Error> {
|
|||
_ => return Err(Error::ParseError("Invalid 'default' attribute present in prefs.")),
|
||||
};
|
||||
Ok(Prefs { default_, always, never })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialise_query(query: &Query) -> Element {
|
||||
impl<'a> Into<Element> for &'a Query {
|
||||
fn into(self) -> Element {
|
||||
let mut elem = Element::builder("query")
|
||||
.ns(ns::MAM)
|
||||
.attr("queryid", query.queryid.clone())
|
||||
.attr("node", query.node.clone())
|
||||
.attr("queryid", self.queryid.clone())
|
||||
.attr("node", self.node.clone())
|
||||
.build();
|
||||
//if let Some(form) = query.form {
|
||||
//if let Some(form) = self.form {
|
||||
// elem.append_child((&form).into());
|
||||
//}
|
||||
if let Some(ref set) = query.set {
|
||||
if let Some(ref set) = self.set {
|
||||
elem.append_child(set.into());
|
||||
}
|
||||
elem
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialise_result(result: &Result_) -> Element {
|
||||
impl<'a> Into<Element> for &'a Result_ {
|
||||
fn into(self) -> Element {
|
||||
let mut elem = Element::builder("result")
|
||||
.ns(ns::MAM)
|
||||
.attr("queryid", result.queryid.clone())
|
||||
.attr("id", result.id.clone())
|
||||
.attr("queryid", self.queryid.clone())
|
||||
.attr("id", self.id.clone())
|
||||
.build();
|
||||
elem.append_child((&result.forwarded).into());
|
||||
elem.append_child((&self.forwarded).into());
|
||||
elem
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialise_fin(fin: &Fin) -> Element {
|
||||
impl<'a> Into<Element> for &'a Fin {
|
||||
fn into(self) -> Element {
|
||||
let mut elem = Element::builder("fin")
|
||||
.ns(ns::MAM)
|
||||
.attr("complete", match fin.complete {
|
||||
.attr("complete", match self.complete {
|
||||
true => Some("true"),
|
||||
false => None,
|
||||
})
|
||||
.build();
|
||||
elem.append_child((&fin.set).into());
|
||||
elem.append_child((&self.set).into());
|
||||
elem
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialise_prefs(prefs: &Prefs) -> Element {
|
||||
impl<'a> Into<Element> for &'a Prefs {
|
||||
fn into(self) -> Element {
|
||||
let mut elem = Element::builder("prefs")
|
||||
.ns(ns::MAM)
|
||||
.attr("default", match prefs.default_ {
|
||||
.attr("default", match self.default_ {
|
||||
Some(DefaultPrefs::Always) => Some("always"),
|
||||
Some(DefaultPrefs::Never) => Some("never"),
|
||||
Some(DefaultPrefs::Roster) => Some("roster"),
|
||||
None => None,
|
||||
})
|
||||
.build();
|
||||
if !prefs.always.is_empty() {
|
||||
if !self.always.is_empty() {
|
||||
let mut always = Element::builder("always")
|
||||
.ns(ns::RSM)
|
||||
.build();
|
||||
for jid in prefs.always.clone() {
|
||||
for jid in self.always.clone() {
|
||||
always.append_child(Element::builder("jid")
|
||||
.ns(ns::RSM)
|
||||
.append(String::from(jid))
|
||||
|
@ -227,11 +250,11 @@ pub fn serialise_prefs(prefs: &Prefs) -> Element {
|
|||
}
|
||||
elem.append_child(always);
|
||||
}
|
||||
if !prefs.never.is_empty() {
|
||||
if !self.never.is_empty() {
|
||||
let mut never = Element::builder("never")
|
||||
.ns(ns::RSM)
|
||||
.build();
|
||||
for jid in prefs.never.clone() {
|
||||
for jid in self.never.clone() {
|
||||
never.append_child(Element::builder("jid")
|
||||
.ns(ns::RSM)
|
||||
.append(String::from(jid))
|
||||
|
@ -240,18 +263,17 @@ pub fn serialise_prefs(prefs: &Prefs) -> Element {
|
|||
elem.append_child(never);
|
||||
}
|
||||
elem
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use minidom::Element;
|
||||
use error::Error;
|
||||
use mam;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_query() {
|
||||
let elem: Element = "<query xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
|
||||
mam::parse_query(&elem).unwrap();
|
||||
Query::try_from(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -266,7 +288,7 @@ mod tests {
|
|||
</forwarded>
|
||||
</result>
|
||||
"#.parse().unwrap();
|
||||
mam::parse_result(&elem).unwrap();
|
||||
Result_::try_from(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -279,7 +301,7 @@ mod tests {
|
|||
</set>
|
||||
</fin>
|
||||
"#.parse().unwrap();
|
||||
mam::parse_fin(&elem).unwrap();
|
||||
Fin::try_from(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -296,7 +318,7 @@ mod tests {
|
|||
</x>
|
||||
</query>
|
||||
"#.parse().unwrap();
|
||||
mam::parse_query(&elem).unwrap();
|
||||
Query::try_from(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -316,13 +338,13 @@ mod tests {
|
|||
</set>
|
||||
</query>
|
||||
"#.parse().unwrap();
|
||||
mam::parse_query(&elem).unwrap();
|
||||
Query::try_from(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_prefs_get() {
|
||||
let elem: Element = "<prefs xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
|
||||
mam::parse_prefs(&elem).unwrap();
|
||||
Prefs::try_from(&elem).unwrap();
|
||||
|
||||
let elem: Element = r#"
|
||||
<prefs xmlns='urn:xmpp:mam:2' default='roster'>
|
||||
|
@ -330,7 +352,7 @@ mod tests {
|
|||
<never/>
|
||||
</prefs>
|
||||
"#.parse().unwrap();
|
||||
mam::parse_prefs(&elem).unwrap();
|
||||
Prefs::try_from(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -345,13 +367,13 @@ mod tests {
|
|||
</never>
|
||||
</prefs>
|
||||
"#.parse().unwrap();
|
||||
mam::parse_prefs(&elem).unwrap();
|
||||
Prefs::try_from(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_child() {
|
||||
let elem: Element = "<query xmlns='urn:xmpp:mam:2'><coucou/></query>".parse().unwrap();
|
||||
let error = mam::parse_query(&elem).unwrap_err();
|
||||
let error = Query::try_from(&elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
@ -362,8 +384,8 @@ mod tests {
|
|||
#[test]
|
||||
fn test_serialise() {
|
||||
let elem: Element = "<query xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
|
||||
let replace = mam::Query { queryid: None, node: None, form: None, set: None };
|
||||
let elem2 = mam::serialise_query(&replace);
|
||||
let replace = Query { queryid: None, node: None, form: None, set: None };
|
||||
let elem2 = (&replace).into();
|
||||
assert_eq!(elem, elem2);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue