diff options
| author | Andrew Poelstra <apoelstra@wpsoftware.net> | 2014-07-30 19:36:24 -0700 |
|---|---|---|
| committer | Andrew Poelstra <apoelstra@wpsoftware.net> | 2014-07-31 21:41:19 -0700 |
| commit | dac9a1c5207cb33a0b40813896b74d00bbbd1d36 (patch) | |
| tree | 25ecb89241b9081954eb2ea6e79d058650c25cfa | |
| parent | 5bd8edc1121a5736994d69b2dc9cf3efb6fbc116 (diff) | |
| download | rust-dac9a1c5207cb33a0b40813896b74d00bbbd1d36.tar.gz rust-dac9a1c5207cb33a0b40813896b74d00bbbd1d36.zip | |
libuuid: use `Decoder::error()` rather than failing on bad decode
| -rw-r--r-- | src/libuuid/lib.rs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/libuuid/lib.rs b/src/libuuid/lib.rs index 2ffed792abd..d922dde6f85 100644 --- a/src/libuuid/lib.rs +++ b/src/libuuid/lib.rs @@ -501,7 +501,10 @@ impl<T: Encoder<E>, E> Encodable<T, E> for Uuid { impl<T: Decoder<E>, E> Decodable<T, E> for Uuid { /// Decode a UUID from a string fn decode(d: &mut T) -> Result<Uuid, E> { - Ok(from_str(try!(d.read_str()).as_slice()).unwrap()) + match from_str(try!(d.read_str()).as_slice()) { + Some(decode) => Ok(decode), + None => Err(d.error("Unable to decode UUID")) + } } } @@ -803,6 +806,23 @@ mod test { } #[test] + fn test_bad_decode() { + use serialize::json; + use serialize::{Encodable, Decodable}; + + let js_good = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a8".to_string()); + let js_bad1 = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7ah".to_string()); + let js_bad2 = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a".to_string()); + + let u_good: Result<Uuid, _> = Decodable::decode(&mut json::Decoder::new(js_good)); + let u_bad1: Result<Uuid, _> = Decodable::decode(&mut json::Decoder::new(js_bad1)); + let u_bad2: Result<Uuid, _> = Decodable::decode(&mut json::Decoder::new(js_bad2)); + assert!(u_good.is_ok()); + assert!(u_bad1.is_err()); + assert!(u_bad2.is_err()); + } + + #[test] fn test_iterbytes_impl_for_uuid() { use std::collections::HashSet; let mut set = HashSet::new(); |
