2022-11-18 13:15:30 +00:00
|
|
|
use std;
|
|
|
|
use std::fmt::{self, Display};
|
|
|
|
|
|
|
|
use serde::{de, ser};
|
|
|
|
|
|
|
|
use crate::{dec, enc};
|
|
|
|
|
2022-11-18 18:11:40 +00:00
|
|
|
/// Result of a serialization/deserialization operation
|
2022-11-18 13:15:30 +00:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
2022-11-18 18:11:40 +00:00
|
|
|
/// Error representing a serialization/deserialization error
|
2022-11-18 13:15:30 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2022-11-18 18:11:40 +00:00
|
|
|
/// Custom error message
|
2022-11-18 13:15:30 +00:00
|
|
|
Message(String),
|
2022-11-18 18:11:40 +00:00
|
|
|
/// Nettext encoding error
|
2022-11-18 13:15:30 +00:00
|
|
|
Encode(enc::Error),
|
2022-11-18 18:11:40 +00:00
|
|
|
/// Nettext decoding error
|
2022-11-18 15:26:44 +00:00
|
|
|
Decode(String),
|
2022-11-18 18:11:40 +00:00
|
|
|
/// Nettext interpretation error
|
2022-11-18 13:15:30 +00:00
|
|
|
Type(dec::TypeError),
|
2022-11-18 18:11:40 +00:00
|
|
|
/// Cannot parse term as integer
|
2022-11-18 15:26:44 +00:00
|
|
|
ParseInt(std::num::ParseIntError),
|
2022-11-18 18:11:40 +00:00
|
|
|
/// Cannot parse term as float
|
2022-11-18 15:26:44 +00:00
|
|
|
ParseFloat(std::num::ParseFloatError),
|
2022-11-18 18:11:40 +00:00
|
|
|
/// Invalid utf8 byte string
|
2022-11-18 15:26:44 +00:00
|
|
|
Utf8(std::string::FromUtf8Error),
|
2022-11-18 13:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<enc::Error> for Error {
|
|
|
|
fn from(e: enc::Error) -> Self {
|
|
|
|
Error::Encode(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:26:44 +00:00
|
|
|
impl<'a> From<dec::DecodeError<'a>> for Error {
|
|
|
|
fn from(e: dec::DecodeError) -> Self {
|
|
|
|
Error::Decode(e.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 13:15:30 +00:00
|
|
|
impl From<dec::TypeError> for Error {
|
|
|
|
fn from(e: dec::TypeError) -> Self {
|
|
|
|
Error::Type(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ser::Error for Error {
|
|
|
|
fn custom<T: Display>(msg: T) -> Self {
|
|
|
|
Error::Message(msg.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:26:44 +00:00
|
|
|
impl From<std::num::ParseIntError> for Error {
|
|
|
|
fn from(x: std::num::ParseIntError) -> Error {
|
|
|
|
Error::ParseInt(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::num::ParseFloatError> for Error {
|
|
|
|
fn from(x: std::num::ParseFloatError) -> Error {
|
|
|
|
Error::ParseFloat(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::string::FromUtf8Error> for Error {
|
|
|
|
fn from(x: std::string::FromUtf8Error) -> Error {
|
|
|
|
Error::Utf8(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 13:15:30 +00:00
|
|
|
impl de::Error for Error {
|
|
|
|
fn custom<T: Display>(msg: T) -> Self {
|
|
|
|
Error::Message(msg.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Error {
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Error::Message(msg) => formatter.write_str(msg),
|
|
|
|
Error::Encode(err) => write!(formatter, "Encode: {}", err),
|
2022-11-18 15:26:44 +00:00
|
|
|
Error::Decode(err) => write!(formatter, "Decode: {}", err),
|
2022-11-18 13:15:30 +00:00
|
|
|
Error::Type(err) => write!(formatter, "Type: {}", err),
|
2022-11-18 15:26:44 +00:00
|
|
|
Error::ParseInt(err) => write!(formatter, "Parse (int): {}", err),
|
|
|
|
Error::ParseFloat(err) => write!(formatter, "Parse (float): {}", err),
|
|
|
|
Error::Utf8(err) => write!(formatter, "Invalid UTF-8 byte sequnence: {}", err),
|
2022-11-18 13:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for Error {}
|