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
use core::fmt;

#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use alloc::string::ToString;

use musli::de::{NumberHint, TypeHint};
use musli_common::reader::SliceUnderflow;

/// An error raised when encoding or decoding [`Value`][crate::Value].
#[derive(Debug)]
pub struct Error {
    err: ErrorImpl,
}

impl Error {
    #[inline(always)]
    pub(crate) fn new(kind: ErrorKind) -> Self {
        Self {
            err: ErrorImpl::ValueError(kind),
        }
    }
}

impl fmt::Display for Error {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.err.fmt(f)
    }
}

/// Errors specifically produced by value decoding.
#[derive(Debug)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum ErrorKind {
    #[cfg(feature = "alloc")]
    ArrayOutOfBounds,
    ExpectedPackValue,
    ExpectedUnit(TypeHint),
    ExpectedBool(TypeHint),
    ExpectedChar(TypeHint),
    ExpectedNumber(NumberHint, TypeHint),
    #[cfg(feature = "alloc")]
    ExpectedBytes(TypeHint),
    #[cfg(feature = "alloc")]
    ExpectedString(TypeHint),
    #[cfg(feature = "alloc")]
    ExpectedOption(TypeHint),
    #[cfg(feature = "alloc")]
    ExpectedSequence(TypeHint),
    #[cfg(feature = "alloc")]
    ExpectedPack(TypeHint),
    #[cfg(feature = "alloc")]
    ExpectedMap(TypeHint),
    #[cfg(feature = "alloc")]
    ExpectedVariant(TypeHint),
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            #[cfg(feature = "alloc")]
            ErrorKind::ArrayOutOfBounds => {
                write!(f, "tried to decode array that is out-of-bounds")
            }
            ErrorKind::ExpectedPackValue => write!(f, "Expected pack value"),
            ErrorKind::ExpectedUnit(hint) => write!(f, "Expected unit, but found {hint}"),
            ErrorKind::ExpectedBool(hint) => write!(f, "Expected boolean, but found {hint}"),
            ErrorKind::ExpectedChar(hint) => write!(f, "Expected character, but found {hint}"),
            ErrorKind::ExpectedNumber(number, hint) => {
                write!(f, "Expected {number}, but found {hint}")
            }
            #[cfg(feature = "alloc")]
            ErrorKind::ExpectedBytes(hint) => write!(f, "Expected bytes, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorKind::ExpectedString(hint) => write!(f, "Expected string, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorKind::ExpectedOption(hint) => write!(f, "Expected option, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorKind::ExpectedSequence(hint) => write!(f, "Expected sequence, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorKind::ExpectedPack(hint) => write!(f, "Expected pack, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorKind::ExpectedMap(hint) => write!(f, "Expected map, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorKind::ExpectedVariant(hint) => write!(f, "Expected struct, but found {hint}"),
        }
    }
}

#[allow(missing_docs)]
#[derive(Debug)]
#[non_exhaustive]
pub(crate) enum ErrorImpl {
    ValueError(ErrorKind),
    SliceUnderflow(SliceUnderflow),
    #[cfg(feature = "alloc")]
    Message(Box<str>),
    #[cfg(not(feature = "alloc"))]
    Message,
}

impl fmt::Display for ErrorImpl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ErrorImpl::ValueError(error) => error.fmt(f),
            ErrorImpl::SliceUnderflow(error) => error.fmt(f),
            #[cfg(feature = "alloc")]
            ErrorImpl::Message(message) => message.fmt(f),
            #[cfg(not(feature = "alloc"))]
            ErrorImpl::Message => write!(f, "Message error (see diagnostics)"),
        }
    }
}

impl From<SliceUnderflow> for Error {
    #[inline]
    fn from(error: SliceUnderflow) -> Self {
        Self {
            err: ErrorImpl::SliceUnderflow(error),
        }
    }
}

impl From<ErrorKind> for Error {
    #[inline]
    fn from(error: ErrorKind) -> Self {
        Self::new(error)
    }
}

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

impl musli::error::Error for Error {
    #[inline]
    fn custom<T>(error: T) -> Self
    where
        T: fmt::Display,
    {
        Self::message(error)
    }

    #[inline]
    #[allow(unused_variables)]
    fn message<T>(message: T) -> Self
    where
        T: fmt::Display,
    {
        Self {
            #[cfg(feature = "alloc")]
            err: ErrorImpl::Message(message.to_string().into()),
            #[cfg(not(feature = "alloc"))]
            err: ErrorImpl::Message,
        }
    }
}