Skip to content

variable-length-quantity: Use domain-specific error #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions exercises/variable-length-quantity/example.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#[derive(Debug, PartialEq)]
pub enum Error {
IncompleteNumber,
Overflow,
}

/// Convert a list of numbers to a stream of bytes encoded with variable length encoding.
pub fn to_bytes(values: &[u32]) -> Vec<u8> {
let mut res = vec![];
Expand Down Expand Up @@ -68,13 +74,13 @@ fn to_bytes_single(mut value: u32) -> Vec<u8> {
// }

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

// append bytes of b to tmp
Expand All @@ -89,7 +95,7 @@ pub fn from_bytes(bytes: &[u8]) -> Result<Vec<u32>, &'static str> {
if i+1 == bytes.len() {
// the next index would be past the end,
// i.e. there are no more bytes.
return Err("Incomplete byte sequence");
return Err(Error::IncompleteNumber);
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion exercises/variable-length-quantity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#[derive(Debug, PartialEq)]
pub enum Error {
IncompleteNumber,
Overflow,
}

/// Convert a list of numbers to a stream of bytes encoded with variable length encoding.
pub fn to_bytes(values: &[u32]) -> Vec<u8> {
unimplemented!()
}

/// Given a stream of bytes, extract all numbers which are encoded in there.
pub fn from_bytes(bytes: &[u8]) -> Result<Vec<u32>, &'static str> {
pub fn from_bytes(bytes: &[u8]) -> Result<Vec<u32>, Error> {
unimplemented!()
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ fn from_bytes_multiple_values() {
#[test]
#[ignore]
fn incomplete_byte_sequence() {
assert!(vlq::from_bytes(&[0xff]).is_err());
assert_eq!(Err(vlq::Error::IncompleteNumber), vlq::from_bytes(&[0xff]));
}

#[test]
#[ignore]
fn zero_incomplete_byte_sequence() {
assert!(vlq::from_bytes(&[0x80]).is_err());
assert_eq!(Err(vlq::Error::IncompleteNumber), vlq::from_bytes(&[0x80]));
}

#[test]
#[ignore]
fn overflow_u32() {
assert!(vlq::from_bytes(&[0xff, 0xff, 0xff, 0xff, 0x7f]).is_err());
assert_eq!(Err(vlq::Error::Overflow), vlq::from_bytes(&[0xff, 0xff, 0xff, 0xff, 0x7f]));
}

#[test]
Expand Down