parsers: add starttls XSOs

Jonas Schรคfer created

Change summary

parsers/src/lib.rs             |  2 +
parsers/src/starttls.rs        | 68 ++++++++++++++++++++++++++++++++++++
parsers/src/stream_features.rs | 19 ---------
3 files changed, 71 insertions(+), 18 deletions(-)

Detailed changes

parsers/src/lib.rs ๐Ÿ”—

@@ -55,6 +55,8 @@ pub mod sasl;
 /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
 pub mod stanza_error;
 /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
+pub mod starttls;
+/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
 pub mod stream;
 /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
 pub mod stream_features;

parsers/src/starttls.rs ๐Ÿ”—

@@ -0,0 +1,68 @@
+// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+use xso::{AsXml, FromXml};
+
+use crate::ns;
+
+/// Request to start TLS.
+#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::TLS, name = "starttls")]
+pub struct Request;
+
+/// Information that TLS may now commence.
+#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::TLS, name = "proceed")]
+pub struct Proceed;
+
+/// Stream feature for StartTLS
+///
+/// Used in [`crate::stream_features::StreamFeatures`].
+#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::TLS, name = "starttls")]
+pub struct StartTls {
+    /// Marker for mandatory StartTLS.
+    // TODO: replace with `#[xml(flag)]` once we have it
+    #[xml(child(default))]
+    pub required: Option<RequiredStartTls>,
+}
+
+/// Marker for mandatory StartTLS.
+#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::TLS, name = "required")]
+pub struct RequiredStartTls;
+
+/// Enum which allows parsing/serialising any STARTTLS element.
+#[derive(FromXml, AsXml, Debug, Clone)]
+#[xml()]
+pub enum Nonza {
+    /// Request to start TLS
+    #[xml(transparent)]
+    Request(Request),
+
+    /// Information that TLS may now commence
+    #[xml(transparent)]
+    Proceed(Proceed),
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[cfg(target_pointer_width = "32")]
+    #[test]
+    fn test_size() {
+        assert_size!(RequiredStartTls, 0);
+        assert_size!(StartTls, 1);
+    }
+
+    #[cfg(target_pointer_width = "64")]
+    #[test]
+    fn test_size() {
+        assert_size!(RequiredStartTls, 0);
+        assert_size!(StartTls, 1);
+    }
+}

parsers/src/stream_features.rs ๐Ÿ”—

@@ -11,6 +11,7 @@ use crate::bind::BindFeature;
 use crate::ns;
 use crate::sasl2::Authentication;
 use crate::sasl_cb::SaslChannelBinding;
+use crate::starttls::StartTls;
 use crate::stream_limits::Limits;
 
 /// Wraps `<stream:features/>`, usually the very first nonza of a
@@ -50,20 +51,6 @@ pub struct StreamFeatures {
     pub others: Vec<Element>,
 }
 
-/// StartTLS is supported, and may be mandatory.
-#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
-#[xml(namespace = ns::TLS, name = "starttls")]
-pub struct StartTls {
-    /// Marker for mandatory StartTLS.
-    #[xml(child(default))]
-    pub required: Option<RequiredStartTls>,
-}
-
-/// Marker for mandatory StartTLS.
-#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
-#[xml(namespace = ns::TLS, name = "required")]
-pub struct RequiredStartTls;
-
 /// List of supported SASL mechanisms
 #[derive(FromXml, AsXml, PartialEq, Debug, Clone, Default)]
 #[xml(namespace = ns::SASL, name = "mechanisms")]
@@ -94,8 +81,6 @@ mod tests {
     #[test]
     fn test_size() {
         assert_size!(SaslMechanisms, 12);
-        assert_size!(RequiredStartTls, 0);
-        assert_size!(StartTls, 1);
         assert_size!(StreamFeatures, 92);
     }
 
@@ -103,8 +88,6 @@ mod tests {
     #[test]
     fn test_size() {
         assert_size!(SaslMechanisms, 24);
-        assert_size!(RequiredStartTls, 0);
-        assert_size!(StartTls, 1);
         assert_size!(StreamFeatures, 168);
     }