From 16b232523d0d00c44b153e8f871b76fd4fc71178 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 20 Aug 2024 12:21:10 +0200 Subject: [PATCH] xmpp-parsers: Convert vCard-temp to xso This also keeps all additional elements in the vCard, so they get serialized back on the wire instead of being dropped. --- parsers/ChangeLog | 4 ++++ parsers/src/vcard.rs | 54 ++++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/parsers/ChangeLog b/parsers/ChangeLog index ce2d84090744cfe1ba1f96b57e496e9905eba239..51b382c15ed477b4220c5ab3e969fbc469f66cf6 100644 --- a/parsers/ChangeLog +++ b/parsers/ChangeLog @@ -28,6 +28,10 @@ XXXX-YY-ZZ RELEASER - The bookmarks2 Conference `extensions` child is now an `Option` instead of a `Vec`, to distinguish between it being absent or empty (!472). + * Improvements: + - Keep unsupported vCard elements as `minidom::Element`, so that they + get serialized back instead of being dropped. We now also test for + the size of these elements (!472). * New parsers/serialisers: - Stream Features (RFC 6120) (!400) - Extensible SASL Profile (XEP-0388) diff --git a/parsers/src/vcard.rs b/parsers/src/vcard.rs index c770a6b389b467f4c99a80496ca3befaad09c69f..bed45e33125d883fa9a639ac3f71a6f4dcdea70b 100644 --- a/parsers/src/vcard.rs +++ b/parsers/src/vcard.rs @@ -14,7 +14,6 @@ //! see [`vcard_update`][crate::vcard_update] module. use xso::{ - error::Error, text::{Base64, StripWhitespace, TextCodec}, AsXml, FromXml, }; @@ -55,39 +54,16 @@ pub struct Binval { } /// A `` element; only the `` element is supported for this legacy vCard ; the rest is ignored. +#[derive(FromXml, AsXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::VCARD, name = "vCard")] pub struct VCard { /// A photo element. + #[xml(child(default))] pub photo: Option, -} - -impl TryFrom for VCard { - type Error = xso::error::Error; - - fn try_from(value: Element) -> Result { - // Check that the root element is - if !value.is("vCard", ns::VCARD) { - return Err(Error::Other( - "Root element is not ", - )); - } - - // Parse the element, if any. - let photo = value - .get_child("PHOTO", ns::VCARD) - .map(|photo| Photo::try_from(photo.clone())) - .transpose()?; - - // Return the result. - Ok(VCard { photo }) - } -} -impl From for Element { - fn from(vcard: VCard) -> Element { - Element::builder("vCard", ns::VCARD) - .append_all(vcard.photo) - .build() - } + /// Every other element in the vCard. + #[xml(element(n = ..))] + pub payloads: Vec, } impl IqGetPayload for VCard {} @@ -100,6 +76,24 @@ mod tests { use base64::Engine; use std::str::FromStr; + #[cfg(target_pointer_width = "32")] + #[test] + fn test_size() { + assert_size!(Photo, 24); + assert_size!(Type, 12); + assert_size!(Binval, 12); + assert_size!(VCard, 36); + } + + #[cfg(target_pointer_width = "64")] + #[test] + fn test_size() { + assert_size!(Photo, 48); + assert_size!(Type, 24); + assert_size!(Binval, 24); + assert_size!(VCard, 72); + } + #[test] fn test_vcard() { // Create some bytes: