parsers: Add Message::extract_valid_payload helper

xmppftw created

Change summary

parsers/Cargo.toml     |  3 +++
parsers/ChangeLog      | 21 +++++++++++----------
parsers/src/message.rs | 18 ++++++++++++++++++
tokio-xmpp/Cargo.toml  |  2 +-
4 files changed, 33 insertions(+), 11 deletions(-)

Detailed changes

parsers/Cargo.toml 🔗

@@ -21,6 +21,7 @@ sha2 = "0.10"
 sha3 = "0.10"
 blake2 = "0.10.4"
 chrono = { version = "0.4.5", default-features = false, features = ["std"] }
+log = { version = "0.4", optional = true }
 # same repository dependencies
 jid = { version = "0.11", path = "../jid", features = ["minidom"] }
 minidom = { version = "0.16", path = "../minidom" }
@@ -34,6 +35,8 @@ component = []
 disable-validation = []
 # Enable serde support in jid crate
 serde = [ "jid/serde" ]
+# Enable some additional logging in helpers
+log = [ "dep:log" ]
 
 [package.metadata.docs.rs]
 rustdoc-args = [ "--sort-modules-by-appearance", "-Zunstable-options" ]

parsers/ChangeLog 🔗

@@ -36,20 +36,21 @@ XXXX-YY-ZZ RELEASER <admin@example.com>
         fast::Tls0Rtt, legacy_omemo::IsPreKey, mam::Complete, sm::ResumeAttr (!476)
       - bookmarks::Conference and bookmarks2::Conference use ResourcePart to store
         the optional nickname instead of a String (!485)
+    * New parsers/serialisers:
+      - Stream Features (RFC 6120) (!400)
+      - Extensible SASL Profile (XEP-0388)
+      - SASL Channel-Binding Type Capability (XEP-0440)
+      - Stream Limits Advertisement (XEP-0478)
+      - Message Displayed Synchronization (XEP-0490)
+      - RFC 6120 stream errors
     * Improvements:
+      - Add support for `<optional/> in XEP-0198 feature advertisment
+      - Add support application-specific error conditions in XEP-0198
       - 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)
-        - SASL Channel-Binding Type Capability (XEP-0440)
-        - Stream Limits Advertisement (XEP-0478)
-        - Message Displayed Synchronization (XEP-0490)
-        - RFC 6120 stream errors
-    * Improvements:
-        - Add support for `<optional/> in XEP-0198 feature advertisment
-        - Add support application-specific error conditions in XEP-0198
+	  - Add Message::extract_valid_payload method to warn in case of failure
+	    and return simply Option<T> instead of Result<Option<T>> (!497)
 
 Version 0.21.0:
 2024-07-25 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>

parsers/src/message.rs 🔗

@@ -238,6 +238,24 @@ impl Message {
         std::mem::swap(&mut buf, &mut self.payloads);
         result
     }
+
+    /// Tries to extract the payload, warning when parsing fails.
+    ///
+    /// This method uses [`Message::extract_payload`], but removes the error
+    /// case by simply warning to the current logger.
+    #[cfg(feature = "log")]
+    pub fn extract_valid_payload<T: TryFrom<Element, Error = FromElementError>>(
+        &mut self,
+    ) -> Option<T> {
+        match self.extract_payload::<T>() {
+            Ok(opt) => opt,
+            Err(e) => {
+                // TODO: xso should support human-readable name for T
+                log::warn!("Failed to parse payload: {e}");
+                None
+            }
+        }
+    }
 }
 
 impl TryFrom<Element> for Message {

tokio-xmpp/Cargo.toml 🔗

@@ -25,7 +25,7 @@ syntect = { version = "5", optional = true }
 pin-project-lite = { version = "0.2" }
 # same repository dependencies
 sasl = { version = "0.5", path = "../sasl" }
-xmpp-parsers = { version = "0.21", path = "../parsers" }
+xmpp-parsers = { version = "0.21", path = "../parsers", features = [ "log" ] }
 xso = { version = "0.1", path = "../xso" }
 
 # these are only needed for starttls ServerConnector support