use serde::de;
use std::fmt::Display;
use std::io;
use std::num;
use std::str;
use std::string;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("{0}")]
Custom(String),
#[error("parsing failed with error: '{0}' at position: {1}")]
Parse(String, usize),
#[error("unsupported type for serialization")]
Unsupported,
#[error(transparent)]
FromUtf8(#[from] string::FromUtf8Error),
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
ParseInt(#[from] num::ParseIntError),
#[error(transparent)]
Utf8(#[from] str::Utf8Error),
}
impl Error {
pub fn top_level(object: &'static str) -> Self {
Error::Custom(format!(
"cannot deserialize {} at the top level.\
Try deserializing into a struct.",
object
))
}
pub fn parse_err<T>(msg: T, position: usize) -> Self
where
T: Display,
{
Error::Parse(msg.to_string(), position)
}
}
impl de::Error for Error {
fn custom<T>(msg: T) -> Self
where
T: Display,
{
Error::Custom(msg.to_string())
}
}
pub type Result<T, E = Error> = core::result::Result<T, E>;