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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! Error structures for Rudano.
//!
//! Serialization and Deserialization errors are different types as they do not have much in
//! common, after all.

use std::fmt::{self, Display};
use std::num::NonZeroUsize;

use serde::{de, ser};

/// Result of deserialization operation.
pub type DeserializationResult<T> = std::result::Result<T, DeserializationError>;
/// Result of serialization operation.
pub type SerializationResult<T> = std::result::Result<T, SerializationError>;

/// Information on data deserialization error.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DeserializationError {
    /// Error code, possibly with related data.
    pub code: DeserializationErrorCode,
    /// Line where the error ocurred.
    pub line: Option<NonZeroUsize>,
    /// Column where the error ocurred.
    pub column: Option<NonZeroUsize>,
}

/// Deserialization Error Code, possibly with related data (such as invalid char).
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DeserializationErrorCode {
    CustomError(String),

    UnexpectedEof,
    UnexpectedTrailingCharacters,

    ExpectedAny,
    ExpectedBoolean(char),
    ExpectedInteger(char),
    IntegerOutOfRange(String),
    InvalidSuffix(String),
    ExpectedFloat(char),
    ExpectedCharStart(char),
    ExpectedChar,
    ExpectedCharEnd(char),
    ExpectedStringStart(char),
    ExpectedEscapeStart(char),
    EscapeInvalid(char),
    HexEscapeInvalid(String),
    UnicodeEscapeInvalid(String),
    ExpectedEscapeStop,
    ExpectedOption(char),
    ExpectedUnitStart(char),
    ExpectedUnitEnd(char),
    ExpectedTupleStart(char),
    ExpectedTupleEnd(char),
    ExpectedIdentifier(char),
    WrongStructName(String, String),
    ExpectedStructOpening(char),
    ExpectedStructEnd(char),
    ExpectedArrayStart(char),
    ExpectedArrayComma(char),
    ExpectedArrayEnd(char),
    ExpectedMapStart(char),
    ExpectedMapEnd(char),
    ExpectedMapStructColon(char),
    ExpectedMapStructComma(char),
}

impl de::Error for DeserializationError {
    fn custom<T: Display>(msg: T) -> Self {
        DeserializationError {
            code: DeserializationErrorCode::CustomError(msg.to_string()),
            line: None,
            column: None,
        }
    }
}

impl Display for DeserializationError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        use DeserializationErrorCode::*;

        if let (Some(line), Some(column)) = (self.line, self.column) {
            formatter.write_fmt(format_args!("error at line {}, column {}: ", line, column))?;
        } else {
            formatter.write_str("error at unknown line and column")?;
        }

        match self.code {
            CustomError(ref msg) => formatter.write_str(msg),
            UnexpectedEof => formatter.write_str("unexpected end of input"),
            UnexpectedTrailingCharacters => formatter.write_str("unexpected trailing characters"),

            ExpectedAny => formatter.write_str("expected any value, found invalid syntax"),
            ExpectedBoolean(c) => {
                formatter.write_fmt(format_args!("expected boolean, found {:?}", c))
            }
            ExpectedInteger(c) => {
                formatter.write_fmt(format_args!("expected integer, found {:?}", c))
            }
            IntegerOutOfRange(ref s) => {
                formatter.write_fmt(format_args!("integer out of range {:?}", s))
            }
            InvalidSuffix(ref s) => {
                formatter.write_fmt(format_args!("invalid number suffix {:?}", s))
            }
            ExpectedFloat(c) => formatter.write_fmt(format_args!("expected float, found {:?}", c)),
            ExpectedCharStart(c) => {
                formatter.write_fmt(format_args!("expected '\\'', found {:?}", c))
            }
            ExpectedChar => formatter.write_str("expected char, found nothing"),
            ExpectedCharEnd(c) => {
                formatter.write_fmt(format_args!("expected '\\'', found {:?}", c))
            }
            ExpectedStringStart(c) => {
                formatter.write_fmt(format_args!("expected '\"', found {:?}", c))
            }
            ExpectedEscapeStart(c) => {
                formatter.write_fmt(format_args!("expected '{{', found {:?}", c))
            }
            EscapeInvalid(c) => {
                formatter.write_fmt(format_args!("expected escape character, found {:?}", c))
            }
            HexEscapeInvalid(ref s) => {
                formatter.write_fmt(format_args!("hex escape invalid {:?}", s))
            }
            UnicodeEscapeInvalid(ref s) => {
                formatter.write_fmt(format_args!("unicode escape invalid {:?}", s))
            }
            ExpectedEscapeStop => formatter.write_str("expected '}}'"),
            ExpectedOption(c) => {
                formatter.write_fmt(format_args!("expected option, found {:?}", c))
            }
            ExpectedUnitStart(c) => {
                formatter.write_fmt(format_args!("expected '(', found {:?}", c))
            }
            ExpectedUnitEnd(c) => formatter.write_fmt(format_args!("expected ')', found {:?}", c)),
            ExpectedTupleStart(c) => {
                formatter.write_fmt(format_args!("expected '(', found {:?}", c))
            }
            ExpectedTupleEnd(c) => formatter.write_fmt(format_args!("expected ')', found {:?}", c)),
            ExpectedIdentifier(c) => {
                formatter.write_fmt(format_args!("expected identifier, found {:?}", c))
            }
            WrongStructName(ref expected, ref found) => formatter.write_fmt(format_args!(
                "wrong struct name, expected {:?}, found {:?}",
                expected, found
            )),
            ExpectedStructOpening(c) => {
                formatter.write_fmt(format_args!("expected '{{', found {:?}", c))
            }
            ExpectedStructEnd(c) => {
                formatter.write_fmt(format_args!("expected '}}', found {:?}", c))
            }
            ExpectedArrayStart(c) => {
                formatter.write_fmt(format_args!("expected '[', found {:?}", c))
            }
            ExpectedArrayComma(c) => {
                formatter.write_fmt(format_args!("expected ',', found {:?}", c))
            }
            ExpectedArrayEnd(c) => formatter.write_fmt(format_args!("expected ']', found {:?}", c)),
            ExpectedMapStart(c) => formatter.write_fmt(format_args!("expected '[', found {:?}", c)),
            ExpectedMapEnd(c) => formatter.write_fmt(format_args!("expected ']', found {:?}", c)),
            ExpectedMapStructColon(c) => {
                formatter.write_fmt(format_args!("expected ':', found {:?}", c))
            }
            ExpectedMapStructComma(c) => {
                formatter.write_fmt(format_args!("expected ',', found {:?}", c))
            }
        }
    }
}

impl std::error::Error for DeserializationError {}

/// All possible errors that may occur when Serializing Rudano.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SerializationError {
    /// Custom Error. This is usually created by the Serialize implementation of the data being
    /// serialized.
    CustomError(String),
}

impl ser::Error for SerializationError {
    fn custom<T: Display>(msg: T) -> Self {
        SerializationError::CustomError(msg.to_string())
    }
}

impl Display for SerializationError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let SerializationError::CustomError(ref msg) = *self;

        formatter.write_str(msg)
    }
}

impl std::error::Error for SerializationError {}

impl From<fmt::Error> for SerializationError {
    fn from(_err: fmt::Error) -> SerializationError {
        SerializationError::CustomError("could not format number".to_string())
    }
}