Skip to content

Commit

Permalink
fix: use match expression for cbor decode
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Jan 25, 2024
1 parent ef5813d commit aec2c57
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions core/src/event_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>, &[u8]) {
// From the spec: https://datatracker.ietf.org/doc/html/rfc7049#section-2.1
//
Expand All @@ -163,45 +163,40 @@ fn cbor_uint_decode(data: &[u8]) -> (Option<u64>, &[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),
}
}

Expand Down

0 comments on commit aec2c57

Please sign in to comment.