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