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
//! When serializing or deserializing from Postgres rows goes wrong.
use std::{fmt, error};

use serde::{de, ser};

/// Alias for a `Result` with the error type `serde_postgres::Error`.
pub type Result<T> = ::std::result::Result<T, Error>;

/// This type represents all possible error that can occur when deserializing
/// postgres rows.
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
    /// A custom defined error occured. Typically coming from `serde`.
    Message(String),
    /// Row contained a field unknown to the data structure.
    UnknownField,
    /// Row's column type was different from the Rust data structure.
    InvalidType,
    /// Rust data structure contained a type unsupported by `serde_postgres`.
    UnsupportedType,
}

impl de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error::Message(msg.to_string())
    }
}

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

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(error::Error::description(self))
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match self {
            Error::Message(ref msg) => msg,
            Error::UnknownField => "Unknown field",
            Error::InvalidType => "Invalid type",
            Error::UnsupportedType => "Type unsupported",
        }
    }
}