xmpp-rs/src/util/error.rs

98 lines
3 KiB
Rust
Raw Normal View History

// 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;
use std::fmt;
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.
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.
ParseIntError(num::ParseIntError),
2018-08-08 16:40:01 +00:00
/// Generated when text which should be a string fails to parse.
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.
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.
ChronoParseError(chrono::ParseError),
2017-04-18 19:44:36 +00:00
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
2019-09-07 14:15:32 +00:00
match self {
Error::ParseError(s) => write!(fmt, "parse error: {}", s),
Error::Base64Error(e) => write!(fmt, "base64 error: {}", e),
Error::ParseIntError(e) => write!(fmt, "integer parsing error: {}", e),
Error::ParseStringError(e) => write!(fmt, "string parsing error: {}", e),
Error::ParseAddrError(e) => write!(fmt, "IP address parsing error: {}", e),
Error::JidParseError(e) => write!(fmt, "JID parsing error: {}", e),
Error::ChronoParseError(e) => write!(fmt, "time parsing error: {}", e),
}
}
}
2017-04-27 17:33:02 +00:00
impl From<base64::DecodeError> for Error {
fn from(err: base64::DecodeError) -> Error {
Error::Base64Error(err)
}
}
impl From<num::ParseIntError> for Error {
fn from(err: num::ParseIntError) -> Error {
Error::ParseIntError(err)
}
}
2017-04-29 05:06:41 +00:00
impl From<string::ParseError> for Error {
fn from(err: string::ParseError) -> Error {
Error::ParseStringError(err)
}
}
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)
}
}
impl From<chrono::ParseError> for Error {
fn from(err: chrono::ParseError) -> Error {
Error::ChronoParseError(err)
}
}