1// Copyright (c) 2017-2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use std::error::Error as StdError;
8use std::fmt;
9
10/// Contains one of the potential errors triggered while parsing an
11/// [Element](../struct.Element.html) into a specialised struct.
12#[derive(Debug)]
13pub enum Error {
14 /// The usual error when parsing something.
15 ///
16 /// TODO: use a structured error so the user can report it better, instead
17 /// of a freeform string.
18 ParseError(&'static str),
19
20 /// Generated when some base64 content fails to decode, usually due to
21 /// extra characters.
22 Base64Error(base64::DecodeError),
23
24 /// Generated when text which should be an integer fails to parse.
25 ParseIntError(std::num::ParseIntError),
26
27 /// Generated when text which should be a string fails to parse.
28 ParseStringError(std::string::ParseError),
29
30 /// Generated when text which should be an IP address (IPv4 or IPv6) fails
31 /// to parse.
32 ParseAddrError(std::net::AddrParseError),
33
34 /// Generated when text which should be a [JID](../../jid/struct.Jid.html)
35 /// fails to parse.
36 JidParseError(jid::JidParseError),
37
38 /// Generated when text which should be a
39 /// [DateTime](../date/struct.DateTime.html) fails to parse.
40 ChronoParseError(chrono::ParseError),
41}
42
43impl StdError for Error {
44 fn cause(&self) -> Option<&dyn StdError> {
45 match self {
46 Error::ParseError(_) => None,
47 Error::Base64Error(e) => Some(e),
48 Error::ParseIntError(e) => Some(e),
49 Error::ParseStringError(e) => Some(e),
50 Error::ParseAddrError(e) => Some(e),
51 Error::JidParseError(e) => Some(e),
52 Error::ChronoParseError(e) => Some(e),
53 }
54 }
55}
56
57impl fmt::Display for Error {
58 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
59 match self {
60 Error::ParseError(s) => write!(fmt, "parse error: {}", s),
61 Error::Base64Error(e) => write!(fmt, "base64 error: {}", e),
62 Error::ParseIntError(e) => write!(fmt, "integer parsing error: {}", e),
63 Error::ParseStringError(e) => write!(fmt, "string parsing error: {}", e),
64 Error::ParseAddrError(e) => write!(fmt, "IP address parsing error: {}", e),
65 Error::JidParseError(e) => write!(fmt, "JID parsing error: {}", e),
66 Error::ChronoParseError(e) => write!(fmt, "time parsing error: {}", e),
67 }
68 }
69}
70
71impl From<base64::DecodeError> for Error {
72 fn from(err: base64::DecodeError) -> Error {
73 Error::Base64Error(err)
74 }
75}
76
77impl From<std::num::ParseIntError> for Error {
78 fn from(err: std::num::ParseIntError) -> Error {
79 Error::ParseIntError(err)
80 }
81}
82
83impl From<std::string::ParseError> for Error {
84 fn from(err: std::string::ParseError) -> Error {
85 Error::ParseStringError(err)
86 }
87}
88
89impl From<std::net::AddrParseError> for Error {
90 fn from(err: std::net::AddrParseError) -> Error {
91 Error::ParseAddrError(err)
92 }
93}
94
95impl From<jid::JidParseError> for Error {
96 fn from(err: jid::JidParseError) -> Error {
97 Error::JidParseError(err)
98 }
99}
100
101impl From<chrono::ParseError> for Error {
102 fn from(err: chrono::ParseError) -> Error {
103 Error::ChronoParseError(err)
104 }
105}