1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use core::fmt::{self, Debug, Display};
use serde::{de, ser};
use std::boxed::Box;

pub type Result<T> = core::result::Result<T, Error>;

pub struct Error {
    inner: Box<ErrorImpl>,
}

impl Error {
    fn new(inner: ErrorImpl) -> Self {
        Self {
            inner: Box::new(inner),
        }
    }

    #[cfg(feature = "std")]
    pub(crate) fn io(error: std::io::Error) -> Self {
        Self::new(ErrorImpl::Io(error))
    }

    pub(crate) fn unexpected_eof() -> Self {
        Self::new(ErrorImpl::UnexpectedEof)
    }

    pub(crate) fn any_unsupported() -> Self {
        Self::new(ErrorImpl::AnyUnsupported)
    }

    pub(crate) fn invalid_utf8() -> Self {
        Self::new(ErrorImpl::InvalidUtf8)
    }

    pub(crate) fn invalid_char() -> Self {
        Self::new(ErrorImpl::InvalidChar)
    }

    pub(crate) fn sequence_length_required() -> Self {
        Self::new(ErrorImpl::SequenceLengthRequired)
    }

    pub(crate) fn map_length_required() -> Self {
        Self::new(ErrorImpl::MapLengthRequired)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        Display::fmt(&self.inner, f)
    }
}

impl Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Debug::fmt(&self.inner, f)
    }
}

impl ser::Error for Error {
    fn custom<T: Display>(msg: T) -> Self {
        Self {
            inner: Box::new(ErrorImpl::Message(std::string::ToString::to_string(&msg))),
        }
    }
}

impl de::Error for Error {
    fn custom<T: Display>(msg: T) -> Self {
        Self {
            inner: Box::new(ErrorImpl::Message(std::string::ToString::to_string(&msg))),
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Category {
    Io,
    Data,
    Eof,
}

impl Category {
    /// Returns `true` if the category is [`Io`].
    ///
    /// [`Io`]: Category::Io
    pub fn is_io(&self) -> bool {
        matches!(self, Self::Io)
    }

    /// Returns `true` if the category is [`Data`].
    ///
    /// [`Data`]: Category::Data
    pub fn is_data(&self) -> bool {
        matches!(self, Self::Data)
    }

    /// Returns `true` if the category is [`Eof`].
    ///
    /// [`Eof`]: Category::Eof
    pub fn is_eof(&self) -> bool {
        matches!(self, Self::Eof)
    }
}

impl Error {
    pub fn classify(&self) -> Category {
        match self.inner.as_ref() {
            ErrorImpl::Message(_) => Category::Data,
            #[cfg(feature = "std")]
            ErrorImpl::Io(_) => Category::Io,
            ErrorImpl::UnexpectedEof => Category::Eof,
            ErrorImpl::AnyUnsupported
            | ErrorImpl::InvalidUtf8
            | ErrorImpl::InvalidChar
            | ErrorImpl::SequenceLengthRequired
            | ErrorImpl::MapLengthRequired => Category::Data,
        }
    }
}

#[cfg(feature = "std")]
impl From<Error> for std::io::Error {
    fn from(error: Error) -> Self {
        if let ErrorImpl::Io(error) = *error.inner {
            error
        } else {
            match error.classify() {
                Category::Io => unreachable!(),
                Category::Data => std::io::Error::new(std::io::ErrorKind::InvalidData, error),
                Category::Eof => std::io::Error::new(std::io::ErrorKind::UnexpectedEof, error),
            }
        }
    }
}

#[derive(Debug)]
enum ErrorImpl {
    Message(std::string::String),
    #[cfg(feature = "std")]
    Io(std::io::Error),
    UnexpectedEof,

    AnyUnsupported,

    InvalidUtf8,
    InvalidChar,

    SequenceLengthRequired,
    MapLengthRequired,
}

impl Display for ErrorImpl {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ErrorImpl::Message(msg) => formatter.write_str(msg),
            #[cfg(feature = "std")]
            ErrorImpl::Io(e) => Display::fmt(&e, formatter),
            ErrorImpl::UnexpectedEof => formatter.write_str("unexpected end of input"),
            ErrorImpl::AnyUnsupported => formatter.write_str("BARE does not support any"),
            ErrorImpl::InvalidUtf8 => formatter.write_str("invalid utf-8 in string"),
            ErrorImpl::InvalidChar => formatter.write_str("invalid unicode codepoint in char"),
            ErrorImpl::SequenceLengthRequired => formatter.write_str("sequence length required"),
            ErrorImpl::MapLengthRequired => formatter.write_str("map length required"),
        }
    }
}