From 36cfe76e4df2ee04d517509e7287bfdf8b4e94aa Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 2 Aug 2018 19:07:07 +0200 Subject: [PATCH] macros: Merge generate_element_with_text!() into generate_element!(). --- src/component.rs | 19 ++++++------- src/delay.rs | 10 ++++--- src/hashes.rs | 8 +++--- src/ibb.rs | 10 ++++--- src/macros.rs | 64 ++++++++++++-------------------------------- src/media_element.rs | 11 ++++---- src/sasl.rs | 32 ++++++++++++++-------- 7 files changed, 71 insertions(+), 83 deletions(-) diff --git a/src/component.rs b/src/component.rs index 4c9c2121..31c54d4f 100644 --- a/src/component.rs +++ b/src/component.rs @@ -10,17 +10,18 @@ use helpers::PlainText; use sha1::Sha1; use digest::Digest; -generate_element_with_text!( +generate_element!( /// The main authentication mechanism for components. Handshake, "handshake", COMPONENT, - - /// If Some, contains the hex-encoded SHA-1 of the concatenation of the - /// stream id and the password, and is used to authenticate against the - /// server. - /// - /// If None, it is the successful reply from the server, the stream is now - /// fully established and both sides can now exchange stanzas. - data: PlainText> + text: ( + /// If Some, contains the hex-encoded SHA-1 of the concatenation of the + /// stream id and the password, and is used to authenticate against the + /// server. + /// + /// If None, it is the successful reply from the server, the stream is now + /// fully established and both sides can now exchange stanzas. + data: PlainText> + ) ); impl Handshake { diff --git a/src/delay.rs b/src/delay.rs index e44912a0..0437ea11 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -12,18 +12,20 @@ use jid::Jid; use helpers::PlainText; -generate_element_with_text!( +generate_element!( /// Notes when and by whom a message got stored for later delivery. Delay, "delay", DELAY, - [ + attributes: [ /// The entity which delayed this message. from: Option = "from" => optional, /// The time at which this message got stored. stamp: DateTime = "stamp" => required ], - /// The optional reason this message got delayed. - data: PlainText> + text: ( + /// The optional reason this message got delayed. + data: PlainText> + ) ); #[cfg(test)] diff --git a/src/hashes.rs b/src/hashes.rs index 06249ab2..eb5a5368 100644 --- a/src/hashes.rs +++ b/src/hashes.rs @@ -66,13 +66,15 @@ impl IntoAttributeValue for Algo { } } -generate_element_with_text!( +generate_element!( #[derive(PartialEq)] Hash, "hash", HASHES, - [ + attributes: [ algo: Algo = "algo" => required ], - hash: Base64> + text: ( + hash: Base64> + ) ); impl Hash { diff --git a/src/ibb.rs b/src/ibb.rs index d67ffc6c..da4f9256 100644 --- a/src/ibb.rs +++ b/src/ibb.rs @@ -42,18 +42,20 @@ attributes: [ impl IqSetPayload for Open {} -generate_element_with_text!( +generate_element!( /// Exchange a chunk of data in an open stream. Data, "data", IBB, - [ + attributes: [ /// Sequence number of this chunk, must wraparound after 65535. seq: u16 = "seq" => required, /// The identifier of the stream on which data is being exchanged. sid: StreamId = "sid" => required ], - /// Vector of bytes to be exchanged. - data: Base64> + text: ( + /// Vector of bytes to be exchanged. + data: Base64> + ) ); impl IqSetPayload for Data {} diff --git a/src/macros.rs b/src/macros.rs index 68e840c5..5793fef0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -338,52 +338,6 @@ macro_rules! generate_elem_id { ); } -macro_rules! generate_element_with_text { - ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, $(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >) => ( - generate_element_with_text!($(#[$meta])* $elem, $name, $ns, [], $(#[$text_meta])* $text_ident: $codec<$text_type>); - ); - ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*], $(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >) => ( - $(#[$meta])* - #[derive(Debug, Clone)] - pub struct $elem { - $( - $(#[$attr_meta])* - pub $attr: $attr_type, - )* - $(#[$text_meta])* - pub $text_ident: $text_type, - } - - impl ::try_from::TryFrom<::minidom::Element> for $elem { - type Err = ::error::Error; - - fn try_from(elem: ::minidom::Element) -> Result<$elem, ::error::Error> { - check_self!(elem, $name, $ns); - check_no_children!(elem, $name); - check_no_unknown_attributes!(elem, $name, [$($attr_name),*]); - Ok($elem { - $( - $attr: get_attr!(elem, $attr_name, $attr_action), - )* - $text_ident: $codec::decode(&elem.text())?, - }) - } - } - - impl From<$elem> for ::minidom::Element { - fn from(elem: $elem) -> ::minidom::Element { - ::minidom::Element::builder($name) - .ns(::ns::$ns) - $( - .attr($attr_name, elem.$attr) - )* - .append($codec::encode(&elem.$text_ident)) - .build() - } - } - ); -} - macro_rules! start_decl { (Vec, $type:ty) => ( Vec<$type> @@ -482,7 +436,13 @@ macro_rules! generate_element { ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*,], children: [$($(#[$child_meta:meta])* $child_ident:ident: $coucou:tt<$child_type:ty> = ($child_name:tt, $child_ns:ident) => $child_constructor:ident),*]) => ( generate_element!($(#[$meta])* $elem, $name, $ns, attributes: [$($(#[$attr_meta])* $attr: $attr_type = $attr_name => $attr_action),*], children: [$($(#[$child_meta])* $child_ident: $coucou<$child_type> = ($child_name, $child_ns) => $child_constructor),*]); ); - ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*], children: [$($(#[$child_meta:meta])* $child_ident:ident: $coucou:tt<$child_type:ty> = ($child_name:tt, $child_ns:ident) => $child_constructor:ident),*]) => ( + ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, text: ($(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >)) => ( + generate_element!($(#[$meta])* $elem, $name, $ns, attributes: [], children: [], text: ($(#[$text_meta])* $text_ident: $codec<$text_type>)); + ); + ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),+], text: ($(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >)) => ( + generate_element!($(#[$meta])* $elem, $name, $ns, attributes: [$($(#[$attr_meta])* $attr: $attr_type = $attr_name => $attr_action),*], children: [], text: ($(#[$text_meta])* $text_ident: $codec<$text_type>)); + ); + ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*], children: [$($(#[$child_meta:meta])* $child_ident:ident: $coucou:tt<$child_type:ty> = ($child_name:tt, $child_ns:ident) => $child_constructor:ident),*] $(, text: ($(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >))*) => ( $(#[$meta])* #[derive(Debug, Clone)] pub struct $elem { @@ -494,6 +454,10 @@ macro_rules! generate_element { $(#[$child_meta])* pub $child_ident: start_decl!($coucou, $child_type), )* + $( + $(#[$text_meta])* + pub $text_ident: $text_type, + )* } impl ::try_from::TryFrom<::minidom::Element> for $elem { @@ -521,6 +485,9 @@ macro_rules! generate_element { $( $child_ident: finish_parse_elem!($child_ident: $coucou = $child_name, $name), )* + $( + $text_ident: $codec::decode(&elem.text())?, + )* }) } } @@ -535,6 +502,9 @@ macro_rules! generate_element { $( .append(generate_serialiser!(elem, $child_ident, $coucou, $child_constructor, ($child_name, $child_ns))) )* + $( + .append($codec::encode(&elem.$text_ident)) + )* .build() } } diff --git a/src/media_element.rs b/src/media_element.rs index b74e7f9d..52a45b5c 100644 --- a/src/media_element.rs +++ b/src/media_element.rs @@ -8,10 +8,10 @@ use helpers::TrimmedPlainText; -generate_element_with_text!( +generate_element!( /// Represents an URI used in a media element. URI, "uri", MEDIA_ELEMENT, - [ + attributes: [ /// The MIME type of the URI referenced. /// /// See the [IANA MIME Media Types Registry][1] for a list of @@ -21,9 +21,10 @@ generate_element_with_text!( /// [1]: https://www.iana.org/assignments/media-types/media-types.xhtml type_: String = "type" => required ], - - /// The actual URI contained. - uri: TrimmedPlainText + text: ( + /// The actual URI contained. + uri: TrimmedPlainText + ) ); generate_element!( diff --git a/src/sasl.rs b/src/sasl.rs index 67dac685..8e4151e2 100644 --- a/src/sasl.rs +++ b/src/sasl.rs @@ -13,27 +13,37 @@ generate_attribute!(Mechanism, "mechanism", { Anonymous => "ANONYMOUS", }); -generate_element_with_text!(Auth, "auth", SASL, - [ +generate_element!(Auth, "auth", SASL, + attributes: [ mechanism: Mechanism = "mechanism" => required ], - data: Base64> + text: ( + data: Base64> + ) ); -generate_element_with_text!(Challenge, "challenge", SASL, - data: Base64> +generate_element!(Challenge, "challenge", SASL, + text: ( + data: Base64> + ) ); -generate_element_with_text!(Response, "response", SASL, - data: Base64> +generate_element!(Response, "response", SASL, + text: ( + data: Base64> + ) ); -generate_element_with_text!(Success, "success", SASL, - data: Base64> +generate_element!(Success, "success", SASL, + text: ( + data: Base64> + ) ); -generate_element_with_text!(Failure, "failure", SASL, - data: TrimmedPlainText +generate_element!(Failure, "failure", SASL, + text: ( + data: TrimmedPlainText + ) ); #[cfg(test)]