2018-03-01 09:01:35 +00:00
|
|
|
// Copyright (c) 2017-2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
2017-04-29 21:14:34 +00:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
use base64;
|
|
|
|
use chrono;
|
|
|
|
use jid;
|
2017-04-18 19:44:36 +00:00
|
|
|
use std::convert::From;
|
2017-06-17 02:36:12 +00:00
|
|
|
use std::fmt;
|
2018-05-04 19:19:40 +00:00
|
|
|
use std::net;
|
2018-12-18 14:32:05 +00:00
|
|
|
use std::num;
|
|
|
|
use std::string;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2018-08-08 16:40:01 +00:00
|
|
|
/// Contains one of the potential errors triggered while parsing an
|
|
|
|
/// [Element](../struct.Element.html) into a specialised struct.
|
2017-04-18 19:44:36 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2018-08-08 16:40:01 +00:00
|
|
|
/// The usual error when parsing something.
|
|
|
|
///
|
|
|
|
/// TODO: use a structured error so the user can report it better, instead
|
|
|
|
/// of a freeform string.
|
2017-04-21 01:46:41 +00:00
|
|
|
ParseError(&'static str),
|
2018-08-08 16:40:01 +00:00
|
|
|
|
|
|
|
/// Generated when some base64 content fails to decode, usually due to
|
|
|
|
/// extra characters.
|
2017-04-27 17:33:02 +00:00
|
|
|
Base64Error(base64::DecodeError),
|
2018-08-08 16:40:01 +00:00
|
|
|
|
|
|
|
/// Generated when text which should be an integer fails to parse.
|
2017-04-22 16:39:00 +00:00
|
|
|
ParseIntError(num::ParseIntError),
|
2018-08-08 16:40:01 +00:00
|
|
|
|
|
|
|
/// Generated when text which should be a string fails to parse.
|
2017-05-06 11:48:42 +00:00
|
|
|
ParseStringError(string::ParseError),
|
2018-08-08 16:40:01 +00:00
|
|
|
|
|
|
|
/// Generated when text which should be an IP address (IPv4 or IPv6) fails
|
|
|
|
/// to parse.
|
2018-05-04 19:19:40 +00:00
|
|
|
ParseAddrError(net::AddrParseError),
|
2018-08-08 16:40:01 +00:00
|
|
|
|
|
|
|
/// Generated when text which should be a [JID](../../jid/struct.Jid.html)
|
|
|
|
/// fails to parse.
|
2017-04-29 05:06:41 +00:00
|
|
|
JidParseError(jid::JidParseError),
|
2018-08-08 16:40:01 +00:00
|
|
|
|
|
|
|
/// Generated when text which should be a
|
|
|
|
/// [DateTime](../date/struct.DateTime.html) fails to parse.
|
2017-05-27 11:20:19 +00:00
|
|
|
ChronoParseError(chrono::ParseError),
|
2017-04-18 19:44:36 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 02:36:12 +00:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Error::ParseError(s) => write!(fmt, "{}", s),
|
|
|
|
Error::Base64Error(ref e) => write!(fmt, "{}", e),
|
|
|
|
Error::ParseIntError(ref e) => write!(fmt, "{}", e),
|
|
|
|
Error::ParseStringError(ref e) => write!(fmt, "{}", e),
|
2018-05-04 19:19:40 +00:00
|
|
|
Error::ParseAddrError(ref e) => write!(fmt, "{}", e),
|
2017-06-17 02:36:12 +00:00
|
|
|
Error::JidParseError(_) => write!(fmt, "JID parse error"),
|
|
|
|
Error::ChronoParseError(ref e) => write!(fmt, "{}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:33:02 +00:00
|
|
|
impl From<base64::DecodeError> for Error {
|
|
|
|
fn from(err: base64::DecodeError) -> Error {
|
2017-04-21 01:46:41 +00:00
|
|
|
Error::Base64Error(err)
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 16:39:00 +00:00
|
|
|
|
|
|
|
impl From<num::ParseIntError> for Error {
|
|
|
|
fn from(err: num::ParseIntError) -> Error {
|
|
|
|
Error::ParseIntError(err)
|
|
|
|
}
|
|
|
|
}
|
2017-04-29 05:06:41 +00:00
|
|
|
|
2017-05-06 11:48:42 +00:00
|
|
|
impl From<string::ParseError> for Error {
|
|
|
|
fn from(err: string::ParseError) -> Error {
|
|
|
|
Error::ParseStringError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 19:19:40 +00:00
|
|
|
impl From<net::AddrParseError> for Error {
|
|
|
|
fn from(err: net::AddrParseError) -> Error {
|
|
|
|
Error::ParseAddrError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 05:06:41 +00:00
|
|
|
impl From<jid::JidParseError> for Error {
|
|
|
|
fn from(err: jid::JidParseError) -> Error {
|
|
|
|
Error::JidParseError(err)
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 11:20:19 +00:00
|
|
|
|
|
|
|
impl From<chrono::ParseError> for Error {
|
|
|
|
fn from(err: chrono::ParseError) -> Error {
|
|
|
|
Error::ChronoParseError(err)
|
|
|
|
}
|
|
|
|
}
|