serde_eth/
error.rs

1// Copyright 2018 Serde Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use serde::{de, ser};
10use std::fmt::{self, Display};
11
12pub type Result<T> = std::result::Result<T, Error>;
13
14// This is a bare-bones implementation. A real library would provide additional
15// information in its error type, for example the line and column at which the
16// error occurred, the byte offset into the input, or the current key being
17// processed.
18#[derive(Clone, Debug, PartialEq)]
19pub enum Error {
20    // One or more variants that can be created by data structures through the
21    // `ser::Error` and `de::Error` traits. For example the Serialize impl for
22    // Mutex<T> might return an error because the mutex is poisoned, or the
23    // Deserialize impl for a struct may return an error because a required
24    // field is missing.
25    Message(String),
26
27    // Zero or more variants that can be created directly by the Serializer and
28    // Deserializer without going through `ser::Error` and `de::Error`. These
29    // are specific to the format, in this case JSON.
30    Eof,
31    Syntax,
32    ExpectedBoolean,
33    ExpectedInteger,
34    ExpectedString,
35    ExpectedNull,
36    ExpectedArray,
37    ExpectedArrayComma,
38    ExpectedArrayEnd,
39    ExpectedMap,
40    ExpectedMapColon,
41    ExpectedMapComma,
42    ExpectedMapEnd,
43    ExpectedEnum,
44    TrailingCharacters,
45}
46
47impl ser::Error for Error {
48    fn custom<T: Display>(msg: T) -> Self {
49        Error::Message(msg.to_string())
50    }
51}
52
53impl de::Error for Error {
54    fn custom<T: Display>(msg: T) -> Self {
55        Error::Message(msg.to_string())
56    }
57}
58
59impl Display for Error {
60    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61        match self {
62            Error::Message(msg) => write!(f, "{}", msg),
63            Error::Eof => f.write_str("unexpected end of input"),
64            /* and so forth */
65            _ => unimplemented!(),
66        }
67    }
68}
69
70impl std::error::Error for Error {}