auto merge of #11597 : sfackler/rust/err-enums, r=alexcrichton
An enum allows callers to deal with errors in a more reasonable way.
This commit is contained in:
commit
fa91446b2b
@ -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 {
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
@ -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]
|
||||
|
@ -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 {
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -113,7 +130,7 @@ impl<'a> FromHex for &'a str {
|
||||
|
||||
match modulus {
|
||||
0 => Ok(b),
|
||||
_ => Err(~"Invalid input length")
|
||||
_ => Err(InvalidHexLength),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user