diff options
author | Alex Crichton <alex@alexcrichton.com> | 2014-08-01 10:26:49 -0700 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2014-08-01 10:26:49 -0700 |
commit | 7e1c1e127d952066bda5fbc94e3cbdb40e912119 (patch) | |
tree | cabcfa8bc2f05774dbb73e8b8318d4735c2770e1 /src | |
parent | a3c7f2c38ef795ad34e225ca54fa6bf625e8a842 (diff) | |
parent | e5763bc6b223a8fe33bbcefb1106022609f42db7 (diff) | |
download | milf-rs-7e1c1e127d952066bda5fbc94e3cbdb40e912119.tar.gz milf-rs-7e1c1e127d952066bda5fbc94e3cbdb40e912119.zip |
Merge pull request #10 from apoelstra/apperror
Add required `error` method to `Decoder`
Diffstat (limited to 'src')
-rw-r--r-- | src/serialization.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/serialization.rs b/src/serialization.rs index a344278..2eb9c24 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -72,6 +72,7 @@ pub enum Error { } /// Description for errors which can occur while decoding a type. +#[deriving(PartialEq)] pub struct DecodeError { /// Field that this error applies to. pub field: Option<String>, @@ -80,7 +81,10 @@ pub struct DecodeError { } /// Enumeration of possible errors which can occur while decoding a structure. +#[deriving(PartialEq)] pub enum DecodeErrorKind { + /// An error flagged by the application, e.g. value out of range + ApplicationError(String), /// A field was expected, but none was found. ExpectedField(/* type */ &'static str), /// A field was found, but it had the wrong type. @@ -695,11 +699,21 @@ impl serialize::Decoder<DecodeError> for Decoder { ref found => Err(self.mismatch("table", found)), } } + + fn error(&mut self, err: &str) -> DecodeError { + DecodeError { + field: self.cur_field.clone(), + kind: ApplicationError(err.to_string()) + } + } } impl fmt::Show for DecodeError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { try!(match self.kind { + ApplicationError(ref err) => { + write!(f, "{}", err) + } ExpectedField(expected_type) => { if expected_type == "table" { write!(f, "expected a section") @@ -809,6 +823,32 @@ mod tests { } #[test] + fn application_decode_error() { + #[deriving(PartialEq, Show)] + struct Range10(uint); + impl<D: ::serialize::Decoder<E>, E> Decodable<D, E> for Range10 { + fn decode(d: &mut D) -> Result<Range10, E> { + let x: uint = try!(Decodable::decode(d)); + if x > 10 { + Err(d.error("Value out of range!")) + } else { + Ok(Range10(x)) + } + } + } + let mut d_good = Decoder::new(Integer(5)); + let mut d_bad1 = Decoder::new(String("not an int".to_string())); + let mut d_bad2 = Decoder::new(Integer(11)); + + assert_eq!(Ok(Range10(5)), Decodable::decode(&mut d_good)); + + let err1: Result<Range10, _> = Decodable::decode(&mut d_bad1); + assert!(err1.is_err()); + let err2: Result<Range10, _> = Decodable::decode(&mut d_bad2); + assert!(err2.is_err()); + } + + #[test] fn array() { #[deriving(Encodable, Decodable, PartialEq, Show)] struct Foo { a: Vec<int> } |