Fix bookamrks2 empty extensions field handling

This commit is contained in:
Paul Fariello 2020-12-06 14:15:55 +01:00
parent 20a94117d3
commit e921168380

View file

@ -31,7 +31,7 @@ pub struct Conference {
pub password: Option<String>, pub password: Option<String>,
/// Extensions elements. /// Extensions elements.
pub extensions: Vec<Element>, pub extensions: Option<Vec<Element>>,
} }
impl Conference { impl Conference {
@ -42,7 +42,7 @@ impl Conference {
name: None, name: None,
nick: None, nick: None,
password: None, password: None,
extensions: vec![], extensions: None,
} }
} }
} }
@ -59,17 +59,17 @@ impl TryFrom<Element> for Conference {
name: get_attr!(root, "name", Option), name: get_attr!(root, "name", Option),
nick: None, nick: None,
password: None, password: None,
extensions: vec![], extensions: None,
}; };
for child in root.children().cloned() { for child in root.children().cloned() {
if child.is("extensions", ns::BOOKMARKS2) { if child.is("extensions", ns::BOOKMARKS2) {
if conference.extensions.len() > 0 { if conference.extensions.is_some() {
return Err(Error::ParseError( return Err(Error::ParseError(
"Conference must not have more than one extensions element.", "Conference must not have more than one extensions element.",
)); ));
} }
conference.extensions.extend(child.children().cloned()); conference.extensions = Some(child.children().cloned().collect());
} else if child.is("nick", ns::BOOKMARKS2) { } else if child.is("nick", ns::BOOKMARKS2) {
if conference.nick.is_some() { if conference.nick.is_some() {
return Err(Error::ParseError( return Err(Error::ParseError(
@ -108,7 +108,10 @@ impl From<Conference> for Element {
.password .password
.map(|password| Element::builder("password", ns::BOOKMARKS2).append(password)), .map(|password| Element::builder("password", ns::BOOKMARKS2).append(password)),
) )
.append_all(conference.extensions) .append_all(match conference.extensions {
Some(extensions) => extensions,
None => vec![],
})
.build() .build()
} }
} }
@ -156,11 +159,8 @@ mod tests {
assert_eq!(conference.name, Some(String::from("Test MUC"))); assert_eq!(conference.name, Some(String::from("Test MUC")));
assert_eq!(conference.clone().nick.unwrap(), "Coucou"); assert_eq!(conference.clone().nick.unwrap(), "Coucou");
assert_eq!(conference.clone().password.unwrap(), "secret"); assert_eq!(conference.clone().password.unwrap(), "secret");
assert_eq!(conference.clone().extensions.len(), 1); assert_eq!(conference.clone().extensions.unwrap().len(), 1);
assert_eq!( assert!(conference.clone().extensions.unwrap()[0].is("test", "urn:xmpp:unknown"));
conference.clone().extensions[0],
Element::builder("test", "urn:xmpp:unknown").build()
);
} }
#[test] #[test]