From aec2c57df17f9be81fa6f5d7abc923c86a3b8e2e Mon Sep 17 00:00:00 2001 From: Nathaniel Cook Date: Thu, 25 Jan 2024 11:04:02 -0700 Subject: [PATCH] fix: use match expression for cbor decode --- core/src/event_id.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/core/src/event_id.rs b/core/src/event_id.rs index 0cb822e48..c7b096ccd 100644 --- a/core/src/event_id.rs +++ b/core/src/event_id.rs @@ -148,7 +148,7 @@ impl EventId { }) } } -// Decode a cbor unsinged integer and return the remaining bytes from the buffer +// Decode a cbor unsigned integer and return the remaining bytes from the buffer fn cbor_uint_decode(data: &[u8]) -> (Option, &[u8]) { // From the spec: https://datatracker.ietf.org/doc/html/rfc7049#section-2.1 // @@ -163,45 +163,40 @@ fn cbor_uint_decode(data: &[u8]) -> (Option, &[u8]) { // information 25) followed by the two bytes 0x01f4, which is 500 in // decimal. - if data[0] <= 23 { + match data[0] { // 0 - 23 - (Some(data[0] as u64), &data[1..]) - } else if data[0] == 24 { + x if x <= 23 => (Some(x as u64), &data[1..]), // u8 - ( + 24 => ( data[1..2] .try_into() .ok() .map(|h| u8::from_le_bytes(h) as u64), &data[2..], - ) - } else if data[0] == 25 { + ), // u16 - ( + 25 => ( data[1..3] .try_into() .ok() .map(|h| u16::from_be_bytes(h) as u64), &data[3..], - ) - } else if data[0] == 26 { + ), // u32 - ( + 26 => ( data[1..5] .try_into() .ok() .map(|h| u32::from_be_bytes(h) as u64), &data[5..], - ) - } else if data[0] == 27 { + ), // u64 - ( + 27 => ( data[1..9].try_into().ok().map(u64::from_be_bytes), &data[9..], - ) - } else { + ), // not a cbor unsigned int - (None, data) + _ => (None, data), } }