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
use std::fmt::Display;
use std::{error, fmt, io, string};

use serde::de::{self, Expected, Unexpected};

/// Possible errors that can arise during decoding.
#[derive(Debug)]
pub enum DecoderError {
    IoError(io::Error),
    FromUtf8Error(string::FromUtf8Error),
    UnrecognizedElementType(u8),
    InvalidArrayKey(usize, String),
    // A field was expected, but none was found.
    ExpectedField(&'static str),
    // An unexpected field was found.
    UnknownField(String),
    // There was an error with the syntactical structure of the BSON.
    SyntaxError(String),
    // The end of the BSON input was reached too soon.
    EndOfStream,
    // Invalid Type
    InvalidType(String),
    // Invalid Length
    InvalidLength(usize, String),
    // Duplicated Field
    DuplicatedField(&'static str),
    // Unknown Variant
    UnknownVariant(String),
    // Invalid value
    InvalidValue(String),
    // Invalid timestamp
    InvalidTimestamp(i64),
    // Ambiguous timestamp
    AmbiguousTimestamp(i64),

    Unknown(String),
}

impl From<io::Error> for DecoderError {
    fn from(err: io::Error) -> DecoderError {
        DecoderError::IoError(err)
    }
}

impl From<string::FromUtf8Error> for DecoderError {
    fn from(err: string::FromUtf8Error) -> DecoderError {
        DecoderError::FromUtf8Error(err)
    }
}

impl fmt::Display for DecoderError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            DecoderError::IoError(ref inner) => inner.fmt(fmt),
            DecoderError::FromUtf8Error(ref inner) => inner.fmt(fmt),
            DecoderError::UnrecognizedElementType(tag) => write!(fmt, "unrecognized element type `{}`", tag),
            DecoderError::InvalidArrayKey(ref want, ref got) => {
                write!(fmt, "invalid array key: expected `{}`, got `{}`", want, got)
            }
            DecoderError::ExpectedField(field_type) => write!(fmt, "expected a field of type `{}`", field_type),
            DecoderError::UnknownField(ref field) => write!(fmt, "unknown field `{}`", field),
            DecoderError::SyntaxError(ref inner) => inner.fmt(fmt),
            DecoderError::EndOfStream => fmt.write_str("end of stream"),
            DecoderError::InvalidType(ref desc) => desc.fmt(fmt),
            DecoderError::InvalidLength(ref len, ref desc) => write!(fmt, "expecting length {}, {}", len, desc),
            DecoderError::DuplicatedField(ref field) => write!(fmt, "duplicated field `{}`", field),
            DecoderError::UnknownVariant(ref var) => write!(fmt, "unknown variant `{}`", var),
            DecoderError::InvalidValue(ref desc) => desc.fmt(fmt),
            DecoderError::Unknown(ref inner) => inner.fmt(fmt),
            DecoderError::InvalidTimestamp(ref i) => write!(fmt, "no such local time {}", i),
            DecoderError::AmbiguousTimestamp(ref i) => write!(fmt, "ambiguous local time {}", i),
        }
    }
}

impl error::Error for DecoderError {
    fn description(&self) -> &str {
        match *self {
            DecoderError::IoError(ref inner) => inner.description(),
            DecoderError::FromUtf8Error(ref inner) => inner.description(),
            DecoderError::UnrecognizedElementType(_) => "unrecognized element type",
            DecoderError::InvalidArrayKey(..) => "invalid array key",
            DecoderError::ExpectedField(_) => "expected a field",
            DecoderError::UnknownField(_) => "found an unknown field",
            DecoderError::SyntaxError(ref inner) => inner,
            DecoderError::EndOfStream => "end of stream",
            DecoderError::InvalidType(ref desc) => desc,
            DecoderError::InvalidLength(_, ref desc) => desc,
            DecoderError::DuplicatedField(_) => "duplicated field",
            DecoderError::UnknownVariant(_) => "unknown variant",
            DecoderError::InvalidValue(ref desc) => desc,
            DecoderError::Unknown(ref inner) => inner,
            DecoderError::InvalidTimestamp(..) => "no such local time",
            DecoderError::AmbiguousTimestamp(..) => "ambiguous local time",
        }
    }
    fn cause(&self) -> Option<&error::Error> {
        match *self {
            DecoderError::IoError(ref inner) => Some(inner),
            DecoderError::FromUtf8Error(ref inner) => Some(inner),
            _ => None,
        }
    }
}

impl de::Error for DecoderError {
    fn custom<T: Display>(msg: T) -> DecoderError {
        DecoderError::Unknown(msg.to_string())
    }

    fn invalid_type(_unexp: Unexpected, exp: &Expected) -> DecoderError {
        DecoderError::InvalidType(exp.to_string())
    }

    fn invalid_value(_unexp: Unexpected, exp: &Expected) -> DecoderError {
        DecoderError::InvalidValue(exp.to_string())
    }

    fn invalid_length(len: usize, exp: &Expected) -> DecoderError {
        DecoderError::InvalidLength(len, exp.to_string())
    }

    fn unknown_variant(variant: &str, _expected: &'static [&'static str]) -> DecoderError {
        DecoderError::UnknownVariant(variant.to_string())
    }

    fn unknown_field(field: &str, _expected: &'static [&'static str]) -> DecoderError {
        DecoderError::UnknownField(String::from(field))
    }

    fn missing_field(field: &'static str) -> DecoderError {
        DecoderError::ExpectedField(field)
    }

    fn duplicate_field(field: &'static str) -> DecoderError {
        DecoderError::DuplicatedField(field)
    }
}

/// Alias for `Result<T, DecoderError>`.
pub type DecoderResult<T> = Result<T, DecoderError>;