Skip to content

Commit 77d4388

Browse files
authored
variable-length-quantity: Use domain-specific error (#452)
We prefer domain-specific errors to strings because they are programmatically inspectable. We would prefer to encourage students to use domain-specific errors rather than strings. As discussed in #444: #444
1 parent a45894e commit 77d4388

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

exercises/variable-length-quantity/example.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#[derive(Debug, PartialEq)]
2+
pub enum Error {
3+
IncompleteNumber,
4+
Overflow,
5+
}
6+
17
/// Convert a list of numbers to a stream of bytes encoded with variable length encoding.
28
pub fn to_bytes(values: &[u32]) -> Vec<u8> {
39
let mut res = vec![];
@@ -68,13 +74,13 @@ fn to_bytes_single(mut value: u32) -> Vec<u8> {
6874
// }
6975

7076
/// Given a stream of bytes, extract all numbers which are encoded in there.
71-
pub fn from_bytes(bytes: &[u8]) -> Result<Vec<u32>, &'static str> {
77+
pub fn from_bytes(bytes: &[u8]) -> Result<Vec<u32>, Error> {
7278
let mut res = vec![];
7379
let mut tmp = 0;
7480
for (i,b) in bytes.iter().enumerate() {
7581
// test if first 7 bit are set, to check for overflow
7682
if (tmp & 0xfe_00_00_00) > 0 {
77-
return Err("Would overflow");
83+
return Err(Error::Overflow);
7884
}
7985

8086
// append bytes of b to tmp
@@ -89,7 +95,7 @@ pub fn from_bytes(bytes: &[u8]) -> Result<Vec<u32>, &'static str> {
8995
if i+1 == bytes.len() {
9096
// the next index would be past the end,
9197
// i.e. there are no more bytes.
92-
return Err("Incomplete byte sequence");
98+
return Err(Error::IncompleteNumber);
9399
}
94100
}
95101
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
#[derive(Debug, PartialEq)]
2+
pub enum Error {
3+
IncompleteNumber,
4+
Overflow,
5+
}
6+
17
/// Convert a list of numbers to a stream of bytes encoded with variable length encoding.
28
pub fn to_bytes(values: &[u32]) -> Vec<u8> {
39
unimplemented!()
410
}
511

612
/// Given a stream of bytes, extract all numbers which are encoded in there.
7-
pub fn from_bytes(bytes: &[u8]) -> Result<Vec<u32>, &'static str> {
13+
pub fn from_bytes(bytes: &[u8]) -> Result<Vec<u32>, Error> {
814
unimplemented!()
915
}

exercises/variable-length-quantity/tests/variable-length-quantity.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ fn from_bytes_multiple_values() {
8282
#[test]
8383
#[ignore]
8484
fn incomplete_byte_sequence() {
85-
assert!(vlq::from_bytes(&[0xff]).is_err());
85+
assert_eq!(Err(vlq::Error::IncompleteNumber), vlq::from_bytes(&[0xff]));
8686
}
8787

8888
#[test]
8989
#[ignore]
9090
fn zero_incomplete_byte_sequence() {
91-
assert!(vlq::from_bytes(&[0x80]).is_err());
91+
assert_eq!(Err(vlq::Error::IncompleteNumber), vlq::from_bytes(&[0x80]));
9292
}
9393

9494
#[test]
9595
#[ignore]
9696
fn overflow_u32() {
97-
assert!(vlq::from_bytes(&[0xff, 0xff, 0xff, 0xff, 0x7f]).is_err());
97+
assert_eq!(Err(vlq::Error::Overflow), vlq::from_bytes(&[0xff, 0xff, 0xff, 0xff, 0x7f]));
9898
}
9999

100100
#[test]

0 commit comments

Comments
 (0)