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

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

use musli::de::{NumberHint, TypeHint};

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

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

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

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

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

impl crate::context::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::Empty,
        }
    }
}

/// Errors specifically produced by value decoding.
#[derive(Debug)]
#[non_exhaustive]
#[allow(missing_docs)]
pub(crate) enum ErrorMessage {
    #[cfg(feature = "alloc")]
    ArrayOutOfBounds,
    ExpectedPackValue,
    ExpectedUnit(TypeHint),
    ExpectedBool(TypeHint),
    ExpectedChar(TypeHint),
    ExpectedNumber(NumberHint, TypeHint),
    ExpectedFieldName,
    ExpectedFieldValue,
    ExpectedMapValue,
    #[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 ErrorMessage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            #[cfg(feature = "alloc")]
            ErrorMessage::ArrayOutOfBounds => {
                write!(f, "tried to decode array that is out-of-bounds")
            }
            ErrorMessage::ExpectedPackValue => write!(f, "Expected pack value"),
            ErrorMessage::ExpectedUnit(hint) => write!(f, "Expected unit, but found {hint}"),
            ErrorMessage::ExpectedBool(hint) => write!(f, "Expected boolean, but found {hint}"),
            ErrorMessage::ExpectedChar(hint) => write!(f, "Expected character, but found {hint}"),
            ErrorMessage::ExpectedNumber(number, hint) => {
                write!(f, "Expected {number}, but found {hint}")
            }
            ErrorMessage::ExpectedFieldName => write!(f, "Expected field name"),
            ErrorMessage::ExpectedFieldValue => write!(f, "Expected field value"),
            ErrorMessage::ExpectedMapValue => write!(f, "Expected map value"),
            #[cfg(feature = "alloc")]
            ErrorMessage::ExpectedBytes(hint) => write!(f, "Expected bytes, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorMessage::ExpectedString(hint) => write!(f, "Expected string, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorMessage::ExpectedOption(hint) => write!(f, "Expected option, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorMessage::ExpectedSequence(hint) => {
                write!(f, "Expected sequence, but found {hint}")
            }
            #[cfg(feature = "alloc")]
            ErrorMessage::ExpectedPack(hint) => write!(f, "Expected pack, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorMessage::ExpectedMap(hint) => write!(f, "Expected map, but found {hint}"),
            #[cfg(feature = "alloc")]
            ErrorMessage::ExpectedVariant(hint) => write!(f, "Expected struct, but found {hint}"),
        }
    }
}