bookmarks2: Simplify the type of the extensions

This commit is contained in:
Emmanuel Gil Peyrot 2022-01-03 12:34:24 +01:00
parent 1cc16e65db
commit f2af82718e
3 changed files with 26 additions and 18 deletions

View file

@ -31,7 +31,7 @@ pub struct Conference {
pub password: Option<String>, pub password: Option<String>,
/// Extensions elements. /// Extensions elements.
pub extensions: Option<Vec<Element>>, pub extensions: Vec<Element>,
} }
impl Conference { impl Conference {
@ -53,18 +53,11 @@ 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: None, extensions: Vec::new(),
}; };
for child in root.children().cloned() { for child in root.children().cloned() {
if child.is("extensions", ns::BOOKMARKS2) { if child.is("nick", ns::BOOKMARKS2) {
if conference.extensions.is_some() {
return Err(Error::ParseError(
"Conference must not have more than one extensions element.",
));
}
conference.extensions = Some(child.children().cloned().collect());
} 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(
"Conference must not have more than one nick.", "Conference must not have more than one nick.",
@ -82,6 +75,17 @@ impl TryFrom<Element> for Conference {
check_no_children!(child, "password"); check_no_children!(child, "password");
check_no_attributes!(child, "password"); check_no_attributes!(child, "password");
conference.password = Some(child.text()); conference.password = Some(child.text());
} else if child.is("extensions", ns::BOOKMARKS2) {
if !conference.extensions.is_empty() {
return Err(Error::ParseError(
"Conference must not have more than one extensions element.",
));
}
conference.extensions.extend(child.children().cloned());
} else {
return Err(Error::ParseError(
"Unknown element in bookmarks2 conference",
));
} }
} }
@ -105,8 +109,10 @@ impl From<Conference> for Element {
.map(|password| Element::builder("password", ns::BOOKMARKS2).append(password)), .map(|password| Element::builder("password", ns::BOOKMARKS2).append(password)),
) )
.append_all(match conference.extensions { .append_all(match conference.extensions {
Some(extensions) => extensions, empty if empty.is_empty() => None,
None => vec![], extensions => {
Some(Element::builder("extensions", ns::BOOKMARKS2).append_all(extensions))
}
}) })
.build() .build()
} }
@ -155,8 +161,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.unwrap().len(), 1); assert_eq!(conference.clone().extensions.len(), 1);
assert!(conference.clone().extensions.unwrap()[0].is("test", "urn:xmpp:unknown")); assert!(conference.clone().extensions[0].is("test", "urn:xmpp:unknown"));
} }
#[test] #[test]

View file

@ -225,7 +225,7 @@ pub mod eme;
/// XEP-0390: Entity Capabilities 2.0 /// XEP-0390: Entity Capabilities 2.0
pub mod ecaps2; pub mod ecaps2;
/// XEP-0402: Bookmarks 2 (This Time it's Serious) /// XEP-0402: PEP Native Bookmarks
pub mod bookmarks2; pub mod bookmarks2;
/// XEP-0421: Anonymous unique occupant identifiers for MUCs /// XEP-0421: Anonymous unique occupant identifiers for MUCs

View file

@ -252,10 +252,12 @@ pub const ECAPS2: &str = "urn:xmpp:caps";
/// XEP-0390: Entity Capabilities 2.0 /// XEP-0390: Entity Capabilities 2.0
pub const ECAPS2_OPTIMIZE: &str = "urn:xmpp:caps:optimize"; pub const ECAPS2_OPTIMIZE: &str = "urn:xmpp:caps:optimize";
/// XEP-0402: Bookmarks 2 (This Time it's Serious) /// XEP-0402: PEP Native Bookmarks
pub const BOOKMARKS2: &str = "urn:xmpp:bookmarks:1"; pub const BOOKMARKS2: &str = "urn:xmpp:bookmarks:1";
/// XEP-0402: Bookmarks 2 (This Time it's Serious) /// XEP-0402: PEP Native Bookmarks
pub const BOOKMARKS2_COMPAT: &str = "urn:xmpp:bookmarks:0#compat"; pub const BOOKMARKS2_COMPAT: &str = "urn:xmpp:bookmarks:1#compat";
/// XEP-0402: PEP Native Bookmarks
pub const BOOKMARKS2_COMPAT_PEP: &str = "urn:xmpp:bookmarks:1#compat-pep";
/// XEP-0421: Anonymous unique occupant identifiers for MUCs /// XEP-0421: Anonymous unique occupant identifiers for MUCs
pub const OID: &str = "urn:xmpp:occupant-id:0"; pub const OID: &str = "urn:xmpp:occupant-id:0";