From abd85cb17af5ed90064d7dfc91b5c10364b95502 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 28 Mar 2024 10:21:20 +0100 Subject: Simplify properties macro using paste Signed-off-by: Matthias Beyer --- Cargo.lock | 1 + mqtt-format/Cargo.toml | 1 + mqtt-format/src/v5/variable_header.rs | 56 ++++++++++------------------------- 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64ea5de..3197096 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -517,6 +517,7 @@ dependencies = [ "nom", "nom-supreme", "num_enum", + "paste", "pretty_assertions", "thiserror", "tokio", diff --git a/mqtt-format/Cargo.toml b/mqtt-format/Cargo.toml index f0cbb11..de47254 100644 --- a/mqtt-format/Cargo.toml +++ b/mqtt-format/Cargo.toml @@ -22,6 +22,7 @@ futures = { version = "0.3.28", optional = true } nom = { version = "7.1.3", optional = true } nom-supreme = { version = "0.8.0", optional = true } num_enum = "0.7.2" +paste = "1.0.14" thiserror = "1.0.40" winnow = { version = "0.6.5", optional = true } yoke = { version = "0.7.0", features = ["derive"], optional = true } diff --git a/mqtt-format/src/v5/variable_header.rs b/mqtt-format/src/v5/variable_header.rs index 8c62bcf..b01d7ad 100644 --- a/mqtt-format/src/v5/variable_header.rs +++ b/mqtt-format/src/v5/variable_header.rs @@ -57,7 +57,6 @@ macro_rules! define_properties { parse with $parser:path as $($lt:lifetime)? $kind:ty; write with $writer:path; with size $size_closure:expr; - testfnname: $testfnname:ident; testvalues: [ $($testvalue:expr),* $(,)? ] ),* $(,)? @@ -126,19 +125,22 @@ macro_rules! define_properties { #[cfg(test)] mod property_tests { $( - #[test] - fn $testfnname () { - use super::MqttProperties; - use super::$name; - $( - let mut writer = $crate::v5::test::TestWriter { buffer: Vec::new() }; - let instance = $name ($testvalue); - instance.write(&mut writer).unwrap(); - let output = $name::parse(&mut winnow::Bytes::new(&writer.buffer)).unwrap(); - - let expected = $name ( $testvalue ); - assert_eq!(output, expected, "Expected {expected:?}, but got: {:?}", &writer.buffer); - )* + paste::paste! { + #[allow(non_snake_case)] + #[test] + fn [] () { + use super::MqttProperties; + use super::$name; + $( + let mut writer = $crate::v5::test::TestWriter { buffer: Vec::new() }; + let instance = $name ($testvalue); + instance.write(&mut writer).unwrap(); + let output = $name::parse(&mut winnow::Bytes::new(&writer.buffer)).unwrap(); + + let expected = $name ( $testvalue ); + assert_eq!(output, expected, "Expected {expected:?}, but got: {:?}", &writer.buffer); + )* + } } )* } @@ -155,35 +157,30 @@ define_properties! {[ parse with winnow::binary::u8 as u8; write with write_u8; with size |_| 1; - testfnname: test_roundtrip_payloadformatindicator; testvalues: [0x00, 0x01], MessageExpiryInterval as 0x02 => parse with parse_u32 as u32; write with super::integers::write_u32; with size |_| 4; - testfnname: test_roundtrip_messageexpiryinterval; testvalues: [12u32], ContentType<'i> as 0x03 => parse with super::strings::parse_string as &'i str; write with super::strings::write_string; with size super::strings::string_binary_size; - testfnname: test_roundtrip_contenttype; testvalues: ["foo bar"], ResponseTopic<'i> as 0x08 => parse with super::strings::parse_string as &'i str; write with super::strings::write_string; with size super::strings::string_binary_size; - testfnname: test_roundtrip_responsetopic; testvalues: ["some topic name"], CorrelationData<'i> as 0x09 => parse with super::bytes::parse_binary_data as &'i [u8]; write with super::bytes::write_binary_data; with size super::bytes::binary_data_binary_size; - testfnname: test_roundtrip_correlationdata; testvalues: [ &[0x00, 0xFF], &[0x00, 0xFF, 0xAB], @@ -195,42 +192,36 @@ define_properties! {[ parse with parse_variable_u32 as u32; write with super::integers::write_variable_u32; with size |&v: &u32| super::integers::variable_u32_binary_size(v); - testfnname: test_roundtrip_subscriptionidentifier; testvalues: [12, 14, 42, 1337], SessionExpiryInterval as 0x11 => parse with parse_u32 as u32; write with super::integers::write_u32; with size |_| 4; - testfnname: test_roundtrip_sessionexpiryinterval; testvalues: [12, 14, 42, 1337], AssignedClientIdentifier<'i> as 0x12 => parse with super::strings::parse_string as &'i str; write with super::strings::write_string; with size super::strings::string_binary_size; - testfnname: test_roundtrip_assignedclientidentifier; testvalues: ["fooobarbar"], ServerKeepAlive as 0x13 => parse with parse_u16 as u16; write with super::integers::write_u16; with size |_| 2; - testfnname: test_roundtrip_serverkeepalive; testvalues: [12, 14, 42, 1337], AuthenticationMethod<'i> as 0x15 => parse with super::strings::parse_string as &'i str; write with super::strings::write_string; with size super::strings::string_binary_size; - testfnname: test_roundtrip_authenticationmethod; testvalues: ["fooobarbar"], AuthenticationData<'i> as 0x16 => parse with super::bytes::parse_binary_data as &'i [u8]; write with super::bytes::write_binary_data; with size super::bytes::binary_data_binary_size; - testfnname: test_roundtrip_authenticationdata; testvalues: [ &[0x00, 0xFF], &[0x00, 0xFF, 0xAB], @@ -242,49 +233,42 @@ define_properties! {[ parse with winnow::binary::u8 as u8; write with write_u8; with size |_| 1; - testfnname: test_roundtrip_requestprobleminformation; testvalues: [12, 14, 42, 137], WillDelayInterval as 0x18 => parse with parse_u32 as u32; write with super::integers::write_u32; with size |_| 4; - testfnname: test_roundtrip_willdelayinterval; testvalues: [12, 14, 42, 1337], RequestResponseInformation as 0x19 => parse with winnow::binary::u8 as u8; write with write_u8; with size |_| 1; - testfnname: test_roundtrip_requestresponseinformation; testvalues: [12, 14, 42, 137], ResponseInformation<'i> as 0x1A => parse with super::strings::parse_string as &'i str; write with super::strings::write_string; with size super::strings::string_binary_size; - testfnname: test_roundtrip_responseinformation; testvalues: ["fooobarbar"], ServerReference<'i> as 0x1C => parse with super::strings::parse_string as &'i str; write with super::strings::write_string; with size super::strings::string_binary_size; - testfnname: test_roundtrip_serverreference; testvalues: ["fooobarbar"], ReasonString<'i> as 0x1F => parse with super::strings::parse_string as &'i str; write with super::strings::write_string; with size super::strings::string_binary_size; - testfnname: test_roundtrip_reasonstring; testvalues: ["fooobarbar"], ReceiveMaximum as 0x21 => parse with parse_u16_nonzero as core::num::NonZeroU16; write with super::integers::write_u16_nonzero; with size |_| 2; - testfnname: test_roundtrip_receivemaximum; testvalues: [ core::num::NonZeroU16::new(12).unwrap(), core::num::NonZeroU16::new(14).unwrap(), @@ -296,21 +280,18 @@ define_properties! {[ parse with parse_u16 as u16; write with super::integers::write_u16; with size |_| 2; - testfnname: test_roundtrip_topicaliasmaximum; testvalues: [12, 14, 42, 1337], TopicAlias as 0x23 => parse with parse_u16 as u16; write with super::integers::write_u16; with size |_| 2; - testfnname: test_roundtrip_topicalias; testvalues: [12, 14, 42, 1337], MaximumQoS as 0x24 => parse with crate::v5::qos::parse_maximum_quality_of_service as crate::v5::qos::MaximumQualityOfService; write with crate::v5::qos::write_maximum_quality_of_service; with size |_| 1; - testfnname: test_roundtrip_maximumqos; testvalues: [ crate::v5::qos::MaximumQualityOfService::AtMostOnce, crate::v5::qos::MaximumQualityOfService::AtLeastOnce, @@ -320,35 +301,30 @@ define_properties! {[ parse with crate::v5::boolean::parse_bool as bool; write with crate::v5::boolean::write_bool; with size |_| 1; - testfnname: test_roundtrip_retainavailable; testvalues: [true, false], MaximumPacketSize as 0x27 => parse with parse_u32 as u32; write with super::integers::write_u32; with size |_| 4; - testfnname: test_roundtrip_maximumpacketsize; testvalues: [12, 14, 42, 1337], WildcardSubscriptionAvailable as 0x28 => parse with winnow::binary::u8 as u8; write with write_u8; with size |_| 1; - testfnname: test_roundtrip_wildcardsubscriptionavailable; testvalues: [12, 14, 42, 137], SubscriptionIdentifiersAvailable as 0x29 => parse with winnow::binary::u8 as u8; write with write_u8; with size |_| 1; - testfnname: test_roundtrip_subscriptionidentifiersavailable; testvalues: [12, 14, 42, 137], SharedSubscriptionAvailable as 0x2A => parse with winnow::binary::u8 as u8; write with write_u8; with size |_| 1; - testfnname: test_roundtrip_sharedsubscriptionavailable; testvalues: [12, 14, 42, 137], ]} -- cgit v1.2.3