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
// Copyright 2018 Serde Developers // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. use serde::{de, ser}; use std::fmt::{self, Display}; pub type Result<T> = std::result::Result<T, Error>; // This is a bare-bones implementation. A real library would provide additional // information in its error type, for example the line and column at which the // error occurred, the byte offset into the input, or the current key being // processed. #[derive(Clone, Debug, PartialEq)] pub enum Error { // One or more variants that can be created by data structures through the // `ser::Error` and `de::Error` traits. For example the Serialize impl for // Mutex<T> might return an error because the mutex is poisoned, or the // Deserialize impl for a struct may return an error because a required // field is missing. Message(String), // Zero or more variants that can be created directly by the Serializer and // Deserializer without going through `ser::Error` and `de::Error`. These // are specific to the format, in this case JSON. Eof, Syntax, ExpectedBoolean, ExpectedInteger, ExpectedString, ExpectedNull, ExpectedArray, ExpectedArrayComma, ExpectedArrayEnd, ExpectedMap, ExpectedMapColon, ExpectedMapComma, ExpectedMapEnd, ExpectedEnum, TrailingCharacters, } impl ser::Error for Error { fn custom<T: Display>(msg: T) -> Self { Error::Message(msg.to_string()) } } impl de::Error for Error { fn custom<T: Display>(msg: T) -> Self { Error::Message(msg.to_string()) } } impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Error::Message(msg) => write!(f, "{}", msg), Error::Eof => f.write_str("unexpected end of input"), /* and so forth */ _ => unimplemented!(), } } } impl std::error::Error for Error {}