From 17039100f33ce56c0e8ebc3d4f7de71e4e69cf94 Mon Sep 17 00:00:00 2001 From: azam Date: Wed, 12 Apr 2023 04:16:54 +0900 Subject: [PATCH] Change macros to accept unlimited types (#6) * Change decode macros to accept unlimited types * Change encode macro to accept unlimited types --- src/decode.rs | 235 +++++++++++++------------------------------------- src/encode.rs | 35 ++------ 2 files changed, 68 insertions(+), 202 deletions(-) diff --git a/src/decode.rs b/src/decode.rs index ac0fb0a..ecd9374 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -288,93 +288,19 @@ azam_decode_uint_impl!(u128); /// ``` #[macro_export] macro_rules! azam_decode { - ($r:expr, $t1:ty) => { - 'block: { - let value1 = match <$t1>::azam_decode($r) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1)) - } - }; - ($r:expr, $t1:ty, $t2:ty) => { - 'block: { - let reader = &mut $r.as_bytes(); - let value1 = match <$t1>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value2 = match <$t2>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1, value2)) - } - }; - ($r:expr, $t1:ty, $t2:ty, $t3:ty) => { - 'block: { - let reader = &mut $r.as_bytes(); - let value1 = match <$t1>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value2 = match <$t2>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value3 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1, value2, value3)) - } - }; - ($r:expr, $t1:ty, $t2:ty, $t3:ty, $t4:ty) => { - 'block: { - let reader = &mut $r.as_bytes(); - let value1 = match <$t1>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value2 = match <$t2>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value3 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value4 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1, value2, value3, value4)) - } - }; - ($r:expr, $t1:ty, $t2:ty, $t3:ty, $t4:ty, $t5:ty) => { + () => {Result::<()>::Ok(())}; + ($r:expr) => {Result::<()>::Ok(())}; + ($r:expr $(,$t:ty)*) => { 'block: { let reader = &mut $r.as_bytes(); - let value1 = match <$t1>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value2 = match <$t2>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value3 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value4 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value5 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1, value2, value3, value4, value5)) + Ok(( + $( + match <$t>::azam_decode_read(reader) { + Ok(v) => v, + Err(e) => break 'block Err(e), + } + ),* + )) } }; } @@ -394,94 +320,19 @@ macro_rules! azam_decode { /// ``` #[macro_export] macro_rules! azam_decode_read { - ($r:expr, $t1:ty) => { - 'block: { - let reader = $r; - let value1 = match <$t1>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1)) - } - }; - ($r:expr, $t1:ty, $t2:ty) => { - 'block: { - let reader = $r; - let value1 = match <$t1>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value2 = match <$t2>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1, value2)) - } - }; - ($r:expr, $t1:ty, $t2:ty, $t3:ty) => { - 'block: { - let reader = $r; - let value1 = match <$t1>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value2 = match <$t2>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value3 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1, value2, value3)) - } - }; - ($r:expr, $t1:ty, $t2:ty, $t3:ty, $t4:ty) => { - 'block: { - let reader = $r; - let value1 = match <$t1>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value2 = match <$t2>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value3 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value4 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1, value2, value3, value4)) - } - }; - ($r:expr, $t1:ty, $t2:ty, $t3:ty, $t4:ty, $t5:ty) => { + () => {Result::<()>::Ok(())}; + ($r:expr) => {Result::<()>::Ok(())}; + ($r:expr $(,$t:ty)*) => { 'block: { let reader = $r; - let value1 = match <$t1>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value2 = match <$t2>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value3 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value4 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - let value5 = match <$t3>::azam_decode_read(reader) { - Ok(v) => v, - Err(e) => break 'block Err(e), - }; - Ok((value1, value2, value3, value4, value5)) + Ok(( + $( + match <$t>::azam_decode_read(reader) { + Ok(v) => v, + Err(e) => break 'block Err(e), + } + ),* + )) } }; } @@ -492,9 +343,41 @@ mod tests { #[test] fn test_azam_decode_macro() { + assert_eq!((), azam_decode!("123").unwrap()); + assert_eq!((0x01u8), azam_decode!("123", u8).unwrap()); + assert_eq!((0x01u8, 0x02u16), azam_decode!("123", u8, u16).unwrap()); + assert_eq!( + (0x01u8, 0x02u16, 0x03u32), + azam_decode!("123", u8, u16, u32).unwrap() + ); + } + + #[test] + fn test_azam_decode_macro_err() { + assert_eq!( + ErrorKind::UnexpectedEof, + azam_decode!("", u8, u16).unwrap_err().kind() + ); + assert_eq!( + ErrorKind::UnexpectedEof, + azam_decode!("12", u8, u16, u32).unwrap_err().kind() + ); + assert_eq!( + ErrorKind::InvalidData, + azam_decode!("_2", u8, u16).unwrap_err().kind() + ); + } + + #[test] + fn test_azam_decode_read_macro() { + assert_eq!((), azam_decode_read!("123").unwrap()); + assert_eq!( + (0x01u8), + azam_decode_read!(&mut "123".as_bytes(), u8).unwrap() + ); assert_eq!( (0x01u8, 0x02u16), - azam_decode_read!(&mut "12".as_bytes(), u8, u16).unwrap() + azam_decode_read!(&mut "123".as_bytes(), u8, u16).unwrap() ); assert_eq!( (0x01u8, 0x02u16, 0x03u32), @@ -503,13 +386,19 @@ mod tests { } #[test] - fn test_azam_decode_macro_err() { + fn test_azam_decode_read_macro_err() { assert_eq!( ErrorKind::UnexpectedEof, azam_decode_read!(&mut "".as_bytes(), u8, u16) .unwrap_err() .kind() ); + assert_eq!( + ErrorKind::UnexpectedEof, + azam_decode_read!(&mut "12".as_bytes(), u8, u16, u32) + .unwrap_err() + .kind() + ); assert_eq!( ErrorKind::InvalidData, azam_decode_read!(&mut "_2".as_bytes(), u8, u16) diff --git a/src/encode.rs b/src/encode.rs index b2628eb..8217420 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -248,37 +248,14 @@ azam_encode_impl!(u128); #[macro_export] macro_rules! azam_encode { () => {}; - ($v1:expr) => {{ - $v1.azam_encode() + ($value:expr) => {{ + $value.azam_encode() }}; - ($v1:expr, $v2:expr) => {{ + ($($values:expr),*) => {{ let mut bytes = Vec::::new(); - $v1.azam_encode_write(&mut bytes).unwrap(); - $v2.azam_encode_write(&mut bytes).unwrap(); - String::from_utf8(bytes).unwrap() - }}; - ($v1:expr, $v2:expr, $v3:expr) => {{ - let mut bytes = Vec::::new(); - $v1.azam_encode_write(&mut bytes).unwrap(); - $v2.azam_encode_write(&mut bytes).unwrap(); - $v3.azam_encode_write(&mut bytes).unwrap(); - String::from_utf8(bytes).unwrap() - }}; - ($v1:expr, $v2:expr, $v3:expr, $v4:expr) => {{ - let mut bytes = Vec::::new(); - $v1.azam_encode_write(&mut bytes).unwrap(); - $v2.azam_encode_write(&mut bytes).unwrap(); - $v3.azam_encode_write(&mut bytes).unwrap(); - $v4.azam_encode_write(&mut bytes).unwrap(); - String::from_utf8(bytes).unwrap() - }}; - ($v1:expr, $v2:expr, $v3:expr, $v4:expr, $v5:expr) => {{ - let mut bytes = Vec::::new(); - $v1.azam_encode_write(&mut bytes).unwrap(); - $v2.azam_encode_write(&mut bytes).unwrap(); - $v3.azam_encode_write(&mut bytes).unwrap(); - $v4.azam_encode_write(&mut bytes).unwrap(); - $v5.azam_encode_write(&mut bytes).unwrap(); + $( + $values.azam_encode_write(&mut bytes).unwrap(); + )* String::from_utf8(bytes).unwrap() }}; }