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
use std::convert::From;
use std::error;
use std::fmt;
use std::io;
use std::string;

use byteorder;

pub type LDAPResult<Value> = Result<Value, LDAPError>;

pub enum LDAPError
{
    BindFailed,
    DecodingFailure,
    IndefiniteLength,
    InvalidLengthEncoding,
    Io(io::Error),
    Byteorder(byteorder::Error),
    UTF8Error(string::FromUtf8Error),
}

impl fmt::Display for LDAPError
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
    {
        write!(f, "{:?}", *self)
    }
}

impl fmt::Debug for LDAPError
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
    {
        write!(f, "Error: {}", error::Error::description(self))
    }
}

impl error::Error for LDAPError
{
    fn description(&self) -> &str
    {
        match *self
        {
            LDAPError::BindFailed =>
                "LDAP Bind failed.",
            LDAPError::DecodingFailure =>
                "Decoding failure, input is not valid in this situation.",
            LDAPError::IndefiniteLength =>
                "Indefinite length is not allowed in LDAP according to RFC 2551 Section 5.1",
            LDAPError::InvalidLengthEncoding =>
                "The long form of encoding type is not allowed for class Universal.",
            LDAPError::UTF8Error(ref x) =>
                error::Error::description(x),
            LDAPError::Io(ref x) =>
                error::Error::description(x),
            LDAPError::Byteorder(ref x) =>
                error::Error::description(x),
        }
    }
}

impl From<io::Error> for LDAPError
{
    fn from(err: io::Error) -> LDAPError
    {
        LDAPError::Io(err)
    }
}
impl From<byteorder::Error> for LDAPError
{
    fn from(err: byteorder::Error) -> LDAPError
    {
        LDAPError::Byteorder(err)
    }
}
impl From<string::FromUtf8Error> for LDAPError
{
    fn from(err: string::FromUtf8Error) -> LDAPError
    {
        LDAPError::UTF8Error(err)
    }
}