Skip to content

Commit 9fe5d16

Browse files
committed
Stop returning error strings in From{Base64,Hex}
An enum allows callers to deal with errors in a more reasonable way.
1 parent a5ed0c5 commit 9fe5d16

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/libextra/base64.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,25 @@ impl<'a> ToBase64 for &'a [u8] {
154154
pub trait FromBase64 {
155155
/// Converts the value of `self`, interpreted as base64 encoded data, into
156156
/// an owned vector of bytes, returning the vector.
157-
fn from_base64(&self) -> Result<~[u8], ~str>;
157+
fn from_base64(&self) -> Result<~[u8], FromBase64Error>;
158+
}
159+
160+
/// Errors that can occur when decoding a base64 encoded string
161+
pub enum FromBase64Error {
162+
/// The input contained a character not part of the base64 format
163+
InvalidBase64Character(char, uint),
164+
/// The input had an invalid length
165+
InvalidBase64Length,
166+
}
167+
168+
impl ToStr for FromBase64Error {
169+
fn to_str(&self) -> ~str {
170+
match *self {
171+
InvalidBase64Character(ch, idx) =>
172+
format!("Invalid character '{}' at position {}", ch, idx),
173+
InvalidBase64Length => ~"Invalid length",
174+
}
175+
}
158176
}
159177

160178
impl<'a> FromBase64 for &'a str {
@@ -188,7 +206,7 @@ impl<'a> FromBase64 for &'a str {
188206
* }
189207
* ```
190208
*/
191-
fn from_base64(&self) -> Result<~[u8], ~str> {
209+
fn from_base64(&self) -> Result<~[u8], FromBase64Error> {
192210
let mut r = ~[];
193211
let mut buf: u32 = 0;
194212
let mut modulus = 0;
@@ -205,8 +223,7 @@ impl<'a> FromBase64 for &'a str {
205223
'/'|'_' => buf |= 0x3F,
206224
'\r'|'\n' => continue,
207225
'=' => break,
208-
_ => return Err(format!("Invalid character '{}' at position {}",
209-
self.char_at(idx), idx))
226+
_ => return Err(InvalidBase64Character(self.char_at(idx), idx)),
210227
}
211228

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

222239
for (idx, byte) in it {
223240
if (byte as char) != '=' {
224-
return Err(format!("Invalid character '{}' at position {}",
225-
self.char_at(idx), idx));
241+
return Err(InvalidBase64Character(self.char_at(idx), idx));
226242
}
227243
}
228244

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

241257
Ok(r)

src/libextra/comm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ mod test {
127127
// Rendezvous streams should be able to handle any number of messages being sent
128128
let (port, chan) = rendezvous();
129129
do spawn {
130-
1000000.times(|| { chan.send(()) })
130+
10000.times(|| { chan.send(()) })
131131
}
132-
1000000.times(|| { port.recv() })
132+
10000.times(|| { port.recv() })
133133
}
134134

135135
#[test]

src/libextra/hex.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,25 @@ impl<'a> ToHex for &'a [u8] {
5353
pub trait FromHex {
5454
/// Converts the value of `self`, interpreted as hexadecimal encoded data,
5555
/// into an owned vector of bytes, returning the vector.
56-
fn from_hex(&self) -> Result<~[u8], ~str>;
56+
fn from_hex(&self) -> Result<~[u8], FromHexError>;
57+
}
58+
59+
/// Errors that can occur when decoding a hex encoded string
60+
pub enum FromHexError {
61+
/// The input contained a character not part of the hex format
62+
InvalidHexCharacter(char, uint),
63+
/// The input had a invalid length
64+
InvalidHexLength,
65+
}
66+
67+
impl ToStr for FromHexError {
68+
fn to_str(&self) -> ~str {
69+
match *self {
70+
InvalidHexCharacter(ch, idx) =>
71+
format!("Invalid character '{}' at position {}", ch, idx),
72+
InvalidHexLength => ~"Invalid input length",
73+
}
74+
}
5775
}
5876

5977
impl<'a> FromHex for &'a str {
@@ -83,7 +101,7 @@ impl<'a> FromHex for &'a str {
83101
* }
84102
* ```
85103
*/
86-
fn from_hex(&self) -> Result<~[u8], ~str> {
104+
fn from_hex(&self) -> Result<~[u8], FromHexError> {
87105
// This may be an overestimate if there is any whitespace
88106
let mut b = vec::with_capacity(self.len() / 2);
89107
let mut modulus = 0;
@@ -100,8 +118,7 @@ impl<'a> FromHex for &'a str {
100118
buf >>= 4;
101119
continue
102120
}
103-
_ => return Err(format!("Invalid character '{}' at position {}",
104-
self.char_at(idx), idx))
121+
_ => return Err(InvalidHexCharacter(self.char_at(idx), idx)),
105122
}
106123

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

114131
match modulus {
115132
0 => Ok(b),
116-
_ => Err(~"Invalid input length")
133+
_ => Err(InvalidHexLength),
117134
}
118135
}
119136
}

0 commit comments

Comments
 (0)