Skip to content

Commit

Permalink
Change macros to accept unlimited types (#6)
Browse files Browse the repository at this point in the history
* Change decode macros to accept unlimited types

* Change encode macro to accept unlimited types
  • Loading branch information
azam authored Apr 11, 2023
1 parent 549677c commit 1703910
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 202 deletions.
235 changes: 62 additions & 173 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
),*
))
}
};
}
Expand All @@ -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),
}
),*
))
}
};
}
Expand All @@ -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),
Expand All @@ -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)
Expand Down
35 changes: 6 additions & 29 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>::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::<u8>::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::<u8>::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::<u8>::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()
}};
}
Expand Down

0 comments on commit 1703910

Please sign in to comment.