ecaps2: Switch to Into/TryFrom.
This commit is contained in:
parent
2f05d02d23
commit
f963715e77
2 changed files with 33 additions and 30 deletions
|
|
@ -25,12 +25,15 @@ pub struct ECaps2 {
|
||||||
hashes: Vec<Hash>,
|
hashes: Vec<Hash>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_ecaps2(root: &Element) -> Result<ECaps2, Error> {
|
impl<'a> TryFrom<&'a Element> for ECaps2 {
|
||||||
if !root.is("c", ns::ECAPS2) {
|
type Error = Error;
|
||||||
|
|
||||||
|
fn try_from(elem: &'a Element) -> Result<ECaps2, Error> {
|
||||||
|
if !elem.is("c", ns::ECAPS2) {
|
||||||
return Err(Error::ParseError("This is not an ecaps2 element."));
|
return Err(Error::ParseError("This is not an ecaps2 element."));
|
||||||
}
|
}
|
||||||
let mut hashes = vec!();
|
let mut hashes = vec!();
|
||||||
for child in root.children() {
|
for child in elem.children() {
|
||||||
if child.is("hash", ns::HASHES) {
|
if child.is("hash", ns::HASHES) {
|
||||||
let hash = Hash::try_from(child)?;
|
let hash = Hash::try_from(child)?;
|
||||||
hashes.push(hash);
|
hashes.push(hash);
|
||||||
|
|
@ -42,16 +45,17 @@ pub fn parse_ecaps2(root: &Element) -> Result<ECaps2, Error> {
|
||||||
hashes: hashes,
|
hashes: hashes,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialise(ecaps2: &ECaps2) -> Element {
|
|
||||||
let mut c = Element::builder("c")
|
|
||||||
.ns(ns::ECAPS2)
|
|
||||||
.build();
|
|
||||||
for hash in ecaps2.hashes.clone() {
|
|
||||||
let hash_elem = (&hash).into();
|
|
||||||
c.append_child(hash_elem);
|
|
||||||
}
|
}
|
||||||
c
|
|
||||||
|
impl<'a> Into<Element> for &'a ECaps2 {
|
||||||
|
fn into(self) -> Element {
|
||||||
|
Element::builder("c")
|
||||||
|
.ns(ns::ECAPS2)
|
||||||
|
.append(self.hashes.iter()
|
||||||
|
.map(|hash| hash.into())
|
||||||
|
.collect::<Vec<Element>>())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_item(field: &str) -> Vec<u8> {
|
fn compute_item(field: &str) -> Vec<u8> {
|
||||||
|
|
@ -161,8 +165,7 @@ pub fn hash_ecaps2(data: &[u8], algo: &str) -> String {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use minidom::Element;
|
use super::*;
|
||||||
use error::Error;
|
|
||||||
use disco;
|
use disco;
|
||||||
use ecaps2;
|
use ecaps2;
|
||||||
use base64;
|
use base64;
|
||||||
|
|
@ -170,7 +173,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse() {
|
fn test_parse() {
|
||||||
let elem: Element = "<c xmlns='urn:xmpp:caps'><hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=</hash><hash xmlns='urn:xmpp:hashes:2' algo='sha3-256'>+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=</hash></c>".parse().unwrap();
|
let elem: Element = "<c xmlns='urn:xmpp:caps'><hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=</hash><hash xmlns='urn:xmpp:hashes:2' algo='sha3-256'>+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=</hash></c>".parse().unwrap();
|
||||||
let ecaps2 = ecaps2::parse_ecaps2(&elem).unwrap();
|
let ecaps2 = ECaps2::try_from(&elem).unwrap();
|
||||||
assert_eq!(ecaps2.hashes.len(), 2);
|
assert_eq!(ecaps2.hashes.len(), 2);
|
||||||
assert_eq!(ecaps2.hashes[0].algo, "sha-256");
|
assert_eq!(ecaps2.hashes[0].algo, "sha-256");
|
||||||
assert_eq!(ecaps2.hashes[0].hash, "K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=");
|
assert_eq!(ecaps2.hashes[0].hash, "K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=");
|
||||||
|
|
@ -181,7 +184,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_child() {
|
fn test_invalid_child() {
|
||||||
let elem: Element = "<c xmlns='urn:xmpp:caps'><hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=</hash><hash xmlns='urn:xmpp:hashes:1' algo='sha3-256'>+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=</hash></c>".parse().unwrap();
|
let elem: Element = "<c xmlns='urn:xmpp:caps'><hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=</hash><hash xmlns='urn:xmpp:hashes:1' algo='sha3-256'>+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=</hash></c>".parse().unwrap();
|
||||||
let error = ecaps2::parse_ecaps2(&elem).unwrap_err();
|
let error = ECaps2::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ use ns;
|
||||||
|
|
||||||
use stanza_error;
|
use stanza_error;
|
||||||
use delay::Delay;
|
use delay::Delay;
|
||||||
use ecaps2;
|
use ecaps2::ECaps2;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Show {
|
pub enum Show {
|
||||||
|
|
@ -53,7 +53,7 @@ pub enum PresencePayload {
|
||||||
Priority(Priority),
|
Priority(Priority),
|
||||||
StanzaError(stanza_error::StanzaError),
|
StanzaError(stanza_error::StanzaError),
|
||||||
Delay(Delay),
|
Delay(Delay),
|
||||||
ECaps2(ecaps2::ECaps2),
|
ECaps2(ECaps2),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
|
@ -183,7 +183,7 @@ pub fn parse_presence(root: &Element) -> Result<Presence, Error> {
|
||||||
Some(PresencePayload::StanzaError(stanza_error))
|
Some(PresencePayload::StanzaError(stanza_error))
|
||||||
} else if let Ok(delay) = Delay::try_from(elem) {
|
} else if let Ok(delay) = Delay::try_from(elem) {
|
||||||
Some(PresencePayload::Delay(delay))
|
Some(PresencePayload::Delay(delay))
|
||||||
} else if let Ok(ecaps2) = ecaps2::parse_ecaps2(elem) {
|
} else if let Ok(ecaps2) = ECaps2::try_from(elem) {
|
||||||
Some(PresencePayload::ECaps2(ecaps2))
|
Some(PresencePayload::ECaps2(ecaps2))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
@ -228,7 +228,7 @@ pub fn serialise_payload(payload: &PresencePayload) -> Element {
|
||||||
},
|
},
|
||||||
PresencePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error),
|
PresencePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error),
|
||||||
PresencePayload::Delay(ref delay) => delay.into(),
|
PresencePayload::Delay(ref delay) => delay.into(),
|
||||||
PresencePayload::ECaps2(ref ecaps2) => ecaps2::serialise(ecaps2),
|
PresencePayload::ECaps2(ref ecaps2) => ecaps2.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue