Skip to content

Commit

Permalink
Stop returning error strings in From{Base64,Hex}
Browse files Browse the repository at this point in the history
An enum allows callers to deal with errors in a more reasonable way.
  • Loading branch information
sfackler committed Jan 16, 2014
1 parent a5ed0c5 commit 9fe5d16
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
30 changes: 23 additions & 7 deletions src/libextra/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,25 @@ impl<'a> ToBase64 for &'a [u8] {
pub trait FromBase64 {
/// Converts the value of `self`, interpreted as base64 encoded data, into
/// an owned vector of bytes, returning the vector.
fn from_base64(&self) -> Result<~[u8], ~str>;
fn from_base64(&self) -> Result<~[u8], FromBase64Error>;
}

/// Errors that can occur when decoding a base64 encoded string
pub enum FromBase64Error {
/// The input contained a character not part of the base64 format
InvalidBase64Character(char, uint),
/// The input had an invalid length
InvalidBase64Length,
}

impl ToStr for FromBase64Error {
fn to_str(&self) -> ~str {
match *self {
InvalidBase64Character(ch, idx) =>
format!("Invalid character '{}' at position {}", ch, idx),
InvalidBase64Length => ~"Invalid length",
}
}
}

impl<'a> FromBase64 for &'a str {
Expand Down Expand Up @@ -188,7 +206,7 @@ impl<'a> FromBase64 for &'a str {
* }
* ```
*/
fn from_base64(&self) -> Result<~[u8], ~str> {
fn from_base64(&self) -> Result<~[u8], FromBase64Error> {
let mut r = ~[];
let mut buf: u32 = 0;
let mut modulus = 0;
Expand All @@ -205,8 +223,7 @@ impl<'a> FromBase64 for &'a str {
'/'|'_' => buf |= 0x3F,
'\r'|'\n' => continue,
'=' => break,
_ => return Err(format!("Invalid character '{}' at position {}",
self.char_at(idx), idx))
_ => return Err(InvalidBase64Character(self.char_at(idx), idx)),
}

buf <<= 6;
Expand All @@ -221,8 +238,7 @@ impl<'a> FromBase64 for &'a str {

for (idx, byte) in it {
if (byte as char) != '=' {
return Err(format!("Invalid character '{}' at position {}",
self.char_at(idx), idx));
return Err(InvalidBase64Character(self.char_at(idx), idx));
}
}

Expand All @@ -235,7 +251,7 @@ impl<'a> FromBase64 for &'a str {
r.push((buf >> 8 ) as u8);
}
0 => (),
_ => return Err(~"Invalid Base64 length")
_ => return Err(InvalidBase64Length),
}

Ok(r)
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ mod test {
// Rendezvous streams should be able to handle any number of messages being sent
let (port, chan) = rendezvous();
do spawn {
1000000.times(|| { chan.send(()) })
10000.times(|| { chan.send(()) })
}
1000000.times(|| { port.recv() })
10000.times(|| { port.recv() })
}

#[test]
Expand Down
27 changes: 22 additions & 5 deletions src/libextra/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,25 @@ impl<'a> ToHex for &'a [u8] {
pub trait FromHex {
/// Converts the value of `self`, interpreted as hexadecimal encoded data,
/// into an owned vector of bytes, returning the vector.
fn from_hex(&self) -> Result<~[u8], ~str>;
fn from_hex(&self) -> Result<~[u8], FromHexError>;
}

/// Errors that can occur when decoding a hex encoded string
pub enum FromHexError {
/// The input contained a character not part of the hex format
InvalidHexCharacter(char, uint),
/// The input had a invalid length
InvalidHexLength,
}

impl ToStr for FromHexError {
fn to_str(&self) -> ~str {
match *self {
InvalidHexCharacter(ch, idx) =>
format!("Invalid character '{}' at position {}", ch, idx),
InvalidHexLength => ~"Invalid input length",
}
}
}

impl<'a> FromHex for &'a str {
Expand Down Expand Up @@ -83,7 +101,7 @@ impl<'a> FromHex for &'a str {
* }
* ```
*/
fn from_hex(&self) -> Result<~[u8], ~str> {
fn from_hex(&self) -> Result<~[u8], FromHexError> {
// This may be an overestimate if there is any whitespace
let mut b = vec::with_capacity(self.len() / 2);
let mut modulus = 0;
Expand All @@ -100,8 +118,7 @@ impl<'a> FromHex for &'a str {
buf >>= 4;
continue
}
_ => return Err(format!("Invalid character '{}' at position {}",
self.char_at(idx), idx))
_ => return Err(InvalidHexCharacter(self.char_at(idx), idx)),
}

modulus += 1;
Expand All @@ -113,7 +130,7 @@ impl<'a> FromHex for &'a str {

match modulus {
0 => Ok(b),
_ => Err(~"Invalid input length")
_ => Err(InvalidHexLength),
}
}
}
Expand Down

5 comments on commit 9fe5d16

@bors
Copy link
Contributor

@bors bors commented on 9fe5d16 Jan 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at sfackler@9fe5d16

@bors
Copy link
Contributor

@bors bors commented on 9fe5d16 Jan 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging sfackler/rust/err-enums = 9fe5d16 into auto

@bors
Copy link
Contributor

@bors bors commented on 9fe5d16 Jan 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sfackler/rust/err-enums = 9fe5d16 merged ok, testing candidate = fa91446

@bors
Copy link
Contributor

@bors bors commented on 9fe5d16 Jan 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 9fe5d16 Jan 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = fa91446

Please sign in to comment.