Skip to content
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

Stop returning error strings in From{Base64,Hex} #11597

Merged
merged 1 commit into from
Jan 16, 2014
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
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