error.rs

 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::convert::From;
 8use std::num;
 9use std::string;
10use std::fmt;
11use std::net;
12
13use base64;
14use jid;
15use chrono;
16
17/// Contains one of the potential errors triggered while parsing an
18/// [Element](../struct.Element.html) into a specialised struct.
19#[derive(Debug)]
20pub enum Error {
21    /// The usual error when parsing something.
22    ///
23    /// TODO: use a structured error so the user can report it better, instead
24    /// of a freeform string.
25    ParseError(&'static str),
26
27    /// Generated when some base64 content fails to decode, usually due to
28    /// extra characters.
29    Base64Error(base64::DecodeError),
30
31    /// Generated when text which should be an integer fails to parse.
32    ParseIntError(num::ParseIntError),
33
34    /// Generated when text which should be a string fails to parse.
35    ParseStringError(string::ParseError),
36
37    /// Generated when text which should be an IP address (IPv4 or IPv6) fails
38    /// to parse.
39    ParseAddrError(net::AddrParseError),
40
41    /// Generated when text which should be a [JID](../../jid/struct.Jid.html)
42    /// fails to parse.
43    JidParseError(jid::JidParseError),
44
45    /// Generated when text which should be a
46    /// [DateTime](../date/struct.DateTime.html) fails to parse.
47    ChronoParseError(chrono::ParseError),
48}
49
50impl fmt::Display for Error {
51    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
52        match *self {
53            Error::ParseError(s) => write!(fmt, "{}", s),
54            Error::Base64Error(ref e) => write!(fmt, "{}", e),
55            Error::ParseIntError(ref e) => write!(fmt, "{}", e),
56            Error::ParseStringError(ref e) => write!(fmt, "{}", e),
57            Error::ParseAddrError(ref e) => write!(fmt, "{}", e),
58            Error::JidParseError(_) => write!(fmt, "JID parse error"),
59            Error::ChronoParseError(ref e) => write!(fmt, "{}", e),
60        }
61    }
62}
63
64impl From<base64::DecodeError> for Error {
65    fn from(err: base64::DecodeError) -> Error {
66        Error::Base64Error(err)
67    }
68}
69
70impl From<num::ParseIntError> for Error {
71    fn from(err: num::ParseIntError) -> Error {
72        Error::ParseIntError(err)
73    }
74}
75
76impl From<string::ParseError> for Error {
77    fn from(err: string::ParseError) -> Error {
78        Error::ParseStringError(err)
79    }
80}
81
82impl From<net::AddrParseError> for Error {
83    fn from(err: net::AddrParseError) -> Error {
84        Error::ParseAddrError(err)
85    }
86}
87
88impl From<jid::JidParseError> for Error {
89    fn from(err: jid::JidParseError) -> Error {
90        Error::JidParseError(err)
91    }
92}
93
94impl From<chrono::ParseError> for Error {
95    fn from(err: chrono::ParseError) -> Error {
96        Error::ChronoParseError(err)
97    }
98}