From 977f0a89b96e508dce232ff3d5a4e9f9cc572231 Mon Sep 17 00:00:00 2001 From: azam Date: Wed, 12 Apr 2023 04:38:26 +0900 Subject: [PATCH] Release v0.1.2 (#7) * Change macros to accept unlimited types (#6) * CI changes --- .github/release.yml | 27 +++++ .github/workflows/bench.yml | 7 +- .github/workflows/build.yml | 2 - Cargo.toml | 2 +- src/decode.rs | 235 ++++++++++-------------------------- src/encode.rs | 35 +----- 6 files changed, 98 insertions(+), 210 deletions(-) create mode 100644 .github/release.yml diff --git a/.github/release.yml b/.github/release.yml new file mode 100644 index 0000000..0cf65c4 --- /dev/null +++ b/.github/release.yml @@ -0,0 +1,27 @@ +# .github/release.yml + +changelog: + categories: + - title: "⚠️ Breaking Changes" + labels: + - Semver-Major + - breaking-change + - title: "🎁 New Features" + labels: + - Semver-Minor + - enhancement + - title: "🕷 Fixes" + labels: + - fix + - title: "🕷 Others" + labels: + - '*' + exclude: + labels: + - dependencies + - fix + - breaking-change + - enhancement + - title: "👒 Dependencies" + labels: + - dependencies diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 1e14051..0f9d1b7 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -2,13 +2,10 @@ name: bench on: push: + branches: + - main tags: - 'v[0-9]+.[0-9]+.[0-9]+' - pull_request: - types: - - opened - - reopened - - synchronize schedule: - cron: '0 9 * * 5' workflow_dispatch: diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 276cbef..12db4c3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,8 +5,6 @@ on: branches: - main - develop - - feature/* - - fix/* tags: - 'v[0-9]+.[0-9]+.[0-9]+' pull_request: diff --git a/Cargo.toml b/Cargo.toml index 6b0c12a..097a040 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "azamcodec" -version = "0.1.1" +version = "0.1.2" edition = "2021" authors = [ "Azamshul Azizy " ] license = "MIT" 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() }}; }