|
10 | 10 |
|
11 | 11 | use std;
|
12 | 12 | use std::io::Write;
|
| 13 | +use std::string::FromUtf8Error; |
13 | 14 |
|
14 | 15 | use byteorder::{self, ByteOrder};
|
15 | 16 |
|
@@ -38,10 +39,18 @@ pub enum TupleError {
|
38 | 39 | InvalidType { value: u8 },
|
39 | 40 | #[fail(display = "Invalid data")]
|
40 | 41 | InvalidData,
|
| 42 | + #[fail(display = "UTF8 conversion error")] |
| 43 | + FromUtf8Error(FromUtf8Error), |
41 | 44 | }
|
42 | 45 |
|
43 | 46 | type Result<T> = std::result::Result<T, TupleError>;
|
44 | 47 |
|
| 48 | +impl From<FromUtf8Error> for TupleError { |
| 49 | + fn from(error: FromUtf8Error) -> Self { |
| 50 | + TupleError::FromUtf8Error(error) |
| 51 | + } |
| 52 | +} |
| 53 | + |
45 | 54 | trait SingleType: Copy {
|
46 | 55 | /// verifies the value matches this type
|
47 | 56 | fn expect(self, value: u8) -> Result<()>;
|
@@ -93,7 +102,8 @@ pub trait Single: Sized {
|
93 | 102 | fn encode_to_vec(&self) -> Vec<u8> {
|
94 | 103 | let mut v = Vec::new();
|
95 | 104 | // `self.encode` should not fail because undering `Write` does not return error.
|
96 |
| - self.encode(&mut v).unwrap(); |
| 105 | + self.encode(&mut v) |
| 106 | + .expect("single encoding should never fail"); |
97 | 107 | v
|
98 | 108 | }
|
99 | 109 |
|
@@ -215,7 +225,7 @@ impl Single for String {
|
215 | 225 | STRING.expect(buf[0])?;
|
216 | 226 |
|
217 | 227 | let (bytes, offset) = decode_bytes(&buf[1..])?;
|
218 |
| - Ok((String::from_utf8(bytes).unwrap(), offset + 1)) |
| 228 | + Ok((String::from_utf8(bytes)?, offset + 1)) |
219 | 229 | }
|
220 | 230 | }
|
221 | 231 |
|
@@ -437,7 +447,8 @@ pub trait Tuple: Sized {
|
437 | 447 | fn encode<W: Write>(&self, _w: &mut W) -> std::io::Result<()>;
|
438 | 448 | fn encode_to_vec(&self) -> Vec<u8> {
|
439 | 449 | let mut v = Vec::new();
|
440 |
| - self.encode(&mut v).unwrap(); |
| 450 | + self.encode(&mut v) |
| 451 | + .expect("tuple encoding should never fail"); |
441 | 452 | v
|
442 | 453 | }
|
443 | 454 |
|
|
0 commit comments