diff options
author | Sam Scott <sam.scott89@gmail.com> | 2017-11-01 15:18:51 +0000 |
---|---|---|
committer | Sam Scott <sam.scott89@gmail.com> | 2017-11-01 15:18:51 +0000 |
commit | c6bed85425022aaf63acaf081670e16b88dfcaf2 (patch) | |
tree | bcefc50fc7bc68028b0c1105be9b446b06b618bd /src/error.rs | |
parent | 8a18724f53ac35095ed603a2fb259f0abdf56daf (diff) |
Refactor and clean parsing code, improve error messages.
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/error.rs b/src/error.rs index ebffe7f..c46fc8d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,6 +10,10 @@ use std::string; error_chain! { errors { Custom(msg: String) + Parse(msg: String, pos: (usize, usize)) { + description("parsing failure") + display("parsing failed with error: '{}' at position: {:?}", msg, pos) + } Unsupported } @@ -17,18 +21,24 @@ error_chain! { Decoding(data_encoding::decode::Error); FromUtf8(string::FromUtf8Error); Io(io::Error); - Parse(num::ParseIntError); + ParseInt(num::ParseIntError); Utf8(str::Utf8Error); } } impl Error { /// Generate error to show top-level type cannot be deserialized. - pub fn top_level(object: &'static str) -> Error { + pub fn top_level(object: &'static str) -> Self { ErrorKind::Custom(format!("cannot deserialize {} at the top level.\ Try deserializing into a struct.", object)).into() } + + /// Generate a parsing error message with position. + pub fn parse_err<T>(msg: T, position: (usize, usize)) -> Self + where T: Display { + ErrorKind::Parse(msg.to_string(), position).into() + } } impl de::Error for Error { |