From d4f68123869e12f326f50f519e89b942ffa87b39 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 8 Aug 2024 16:57:46 +0200 Subject: [PATCH] xmpp-parsers: Convert cert_management to xso This introduces a breaking change by moving from a bool to an Option, which will be reverted eventually once we add support for #[xml(flag)] types of children. --- parsers/ChangeLog | 4 ++ parsers/src/cert_management.rs | 72 ++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/parsers/ChangeLog b/parsers/ChangeLog index b32a827b..423cfacf 100644 --- a/parsers/ChangeLog +++ b/parsers/ChangeLog @@ -15,6 +15,10 @@ XXXX-YY-ZZ RELEASER then included in `Action`. Also `Action::Erase::num` is now a wrapper type around u32, which lets us implement a custom Default on it. (!416) + - The cert_management module is now using `Option` instead of `bool` to + check whether a child element is present or not, as this is currently + implemented in xso. We might revert that change if we implement flag + metas in xso before the next release. * New parsers/serialisers: - Stream Features (RFC 6120) (!400) - Extensible SASL Profile (XEP-0388) diff --git a/parsers/src/cert_management.rs b/parsers/src/cert_management.rs index 33d8c223..a3a20682 100644 --- a/parsers/src/cert_management.rs +++ b/parsers/src/cert_management.rs @@ -25,20 +25,28 @@ pub struct Cert { pub data: Vec, } -generate_element!( - /// For the client to upload an X.509 certificate. - Append, "append", SASL_CERT, - children: [ - /// The name of this certificate. - name: Required = ("name", SASL_CERT) => Name, +/// Temporary zero-sized struct for when the no-cert-management element is present. +#[derive(FromXml, AsXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::SASL_CERT, name = "no-cert-management")] +pub struct NoCertManagement; - /// The X.509 certificate to set. - cert: Required = ("x509cert", SASL_CERT) => Cert, +/// For the client to upload an X.509 certificate. +#[derive(FromXml, AsXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::SASL_CERT, name = "append")] +pub struct Append { + /// The name of this certificate. + #[xml(child)] + pub name: Name, - /// This client is forbidden from managing certificates. - no_cert_management: Present<_> = ("no-cert-management", SASL_CERT) => bool - ] -); + /// The X.509 certificate to set. + #[xml(child)] + pub cert: Cert, + + /// This client is forbidden from managing certificates. + // TODO: replace with `#[xml(flag)]` once we have it. + #[xml(child(default))] + pub no_cert_management: Option, +} impl IqSetPayload for Append {} @@ -65,23 +73,27 @@ pub struct Users { pub resources: Vec, } -generate_element!( - /// An X.509 certificate being set for this user. - Item, "item", SASL_CERT, - children: [ - /// The name of this certificate. - name: Required = ("name", SASL_CERT) => Name, +/// An X.509 certificate being set for this user. +#[derive(FromXml, AsXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::SASL_CERT, name = "item")] +pub struct Item { + /// The name of this certificate. + #[xml(child)] + pub name: Name, - /// The X.509 certificate to set. - cert: Required = ("x509cert", SASL_CERT) => Cert, + /// The X.509 certificate to set. + #[xml(child)] + pub cert: Cert, - /// This client is forbidden from managing certificates. - no_cert_management: Present<_> = ("no-cert-management", SASL_CERT) => bool, + /// This client is forbidden from managing certificates. + #[xml(child(default))] + // TODO: replace with `#[xml(flag)]` once we have it. + pub no_cert_management: Option, - /// List of resources currently using this certificate. - users: Option = ("users", SASL_CERT) => Users - ] -); + /// List of resources currently using this certificate. + #[xml(child(default))] + pub users: Option, +} /// Server answers with the current list of X.509 certificates. #[derive(FromXml, AsXml, PartialEq, Debug, Clone)] @@ -126,6 +138,7 @@ mod tests { #[cfg(target_pointer_width = "32")] #[test] fn test_size() { + assert_size!(NoCertManagement, 0); assert_size!(Append, 28); assert_size!(Disable, 12); assert_size!(Revoke, 12); @@ -140,6 +153,7 @@ mod tests { #[cfg(target_pointer_width = "64")] #[test] fn test_size() { + assert_size!(NoCertManagement, 0); assert_size!(Append, 56); assert_size!(Disable, 24); assert_size!(Revoke, 24); @@ -211,7 +225,7 @@ mod tests { cert: Cert { data: b"\0\0\0".to_vec(), }, - no_cert_management: false, + no_cert_management: None, }; let elem: Element = append.into(); assert!(elem.is("append", ns::SASL_CERT)); @@ -237,7 +251,7 @@ mod tests { cert: Cert { data: b"\0\0\0".to_vec(), }, - no_cert_management: false, + no_cert_management: None, users: None, }; @@ -256,7 +270,7 @@ mod tests { cert: Cert { data: b"\0\0\0".to_vec(), }, - no_cert_management: false, + no_cert_management: None, }; let serialized: Element = append.into();