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
use std;
use std::borrow::Cow;

/// `Error` is the principal type of the `couchdb` crate.
#[derive(Debug)]
pub enum Error {
    BadDesignDocumentId,

    #[doc(hidden)]
    BadDigest,

    #[doc(hidden)]
    BadPath { what: &'static str },

    BadRevision,

    #[doc(hidden)]
    Io {
        what: Cow<'static, str>,
        cause: std::io::Error,
    },
}

impl Error {
    #[doc(hidden)]
    pub fn bad_path(what: &'static str) -> Self {
        Error::BadPath { what: what }
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let d = std::error::Error::description(self);
        match *self {
            Error::BadPath { what } => write!(f, "{}: {}", d, what),
            Error::Io { ref cause, .. } => write!(f, "{}: {}", d, cause),
            _ => f.write_str(d),
        }
    }
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::BadDesignDocumentId => "The string is not a valid CouchDB design document id",
            Error::BadDigest => "The string is not a valid CouchDB attachment digest",
            Error::BadPath { .. } => "The CouchDB path is not valid",
            Error::BadRevision => "The string is not a valid CouchDB document revision",
            Error::Io { ref what, .. } => what.as_ref(),
        }
    }

    fn cause(&self) -> Option<&std::error::Error> {
        match *self {
            Error::Io { ref cause, .. } => Some(cause),
            _ => None,
        }
    }
}

impl<T: Into<Cow<'static, str>>> From<(T, std::io::Error)> for Error {
    fn from((what, cause): (T, std::io::Error)) -> Self {
        Error::Io {
            what: what.into(),
            cause: cause,
        }
    }
}