use std::collections::HashSet;
use std::error::Error;
use std::fmt;
use std::io;
use std::result::Result;
use controls::Control;
use exop::Exop;
use protocol::LdapResultExt;
use search::ResultEntry;
use lber::structures::Tag;
#[derive(Clone, Debug)]
pub struct LdapResult {
pub rc: u32,
pub matched: String,
pub text: String,
pub refs: Vec<HashSet<String>>,
pub ctrls: Vec<Control>,
}
#[doc(hidden)]
impl From<Tag> for LdapResult {
fn from(t: Tag) -> LdapResult {
<LdapResultExt as From<Tag>>::from(t).0
}
}
impl Error for LdapResult {
fn description(&self) -> &'static str {
match self.rc {
0 => "success",
1 => "operationsError",
2 => "protocolError",
3 => "timeLimitExceeded",
4 => "sizeLimitExceeded",
5 => "compareFalse",
6 => "compareTrue",
7 => "authMethodNotSupported",
8 => "strongerAuthRequired",
10 => "referral",
11 => "adminLimitExceeded",
12 => "unavailableCriticalExtension",
13 => "confidentialityRequired",
14 => "saslBindInProgress",
16 => "noSuchAttribute",
17 => "undefinedAttributeType",
18 => "inappropriateMatching",
19 => "constraintViolation",
20 => "attributeOrValueExists",
21 => "invalidAttributeSyntax",
32 => "noSuchObject",
33 => "aliasProblem",
34 => "invalidDNSyntax",
36 => "aliasDereferencingProblem",
48 => "inappropriateAuthentication",
49 => "invalidCredentials",
50 => "insufficientAccessRights",
51 => "busy",
52 => "unavailable",
53 => "unwillingToPerform",
54 => "loopDetect",
64 => "namingViolation",
65 => "objectClassViolation",
66 => "notAllowedOnNonLeaf",
67 => "notAllowedOnRDN",
68 => "entryAlreadyExists",
69 => "objectClassModsProhibited",
71 => "affectsMultipleDSAs",
80 => "other",
88 => "abandoned",
122 => "assertionFailed",
_ => "unknown",
}
}
}
impl fmt::Display for LdapResult {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f,
"rc={} ({}), dn: \"{}\", text: \"{}\"",
self.rc,
self.description(),
self.matched,
self.text
)
}
}
impl LdapResult {
pub fn success(self) -> Result<Self, io::Error> {
if self.rc == 0 {
Ok(self)
} else {
Err(io::Error::new(io::ErrorKind::Other, self))
}
}
pub fn non_error(self) -> Result<Self, io::Error> {
if self.rc == 0 || self.rc == 10 {
Ok(self)
} else {
Err(io::Error::new(io::ErrorKind::Other, self))
}
}
}
#[derive(Clone, Debug)]
pub struct SearchResult(pub Vec<ResultEntry>, pub LdapResult);
impl SearchResult {
pub fn success(self) -> Result<(Vec<ResultEntry>, LdapResult), io::Error> {
if self.1.rc == 0 {
Ok((self.0, self.1))
} else {
Err(io::Error::new(io::ErrorKind::Other, self.1))
}
}
pub fn non_error(self) -> Result<(Vec<ResultEntry>, LdapResult), io::Error> {
if self.1.rc == 0 || self.1.rc == 10 {
Ok((self.0, self.1))
} else {
Err(io::Error::new(io::ErrorKind::Other, self.1))
}
}
}
#[derive(Clone, Debug)]
pub struct CompareResult(pub LdapResult);
impl CompareResult {
pub fn equal(self) -> Result<bool, io::Error> {
match self.0.rc {
5 => Ok(false),
6 => Ok(true),
_ => Err(io::Error::new(io::ErrorKind::Other, self.0))
}
}
pub fn non_error(self) -> Result<LdapResult, io::Error> {
if self.0.rc == 5 || self.0.rc == 6 || self.0.rc == 10 {
Ok(self.0)
} else {
Err(io::Error::new(io::ErrorKind::Other, self.0))
}
}
}
#[derive(Clone, Debug)]
pub struct ExopResult(pub Exop, pub LdapResult);
impl ExopResult {
pub fn success(self) -> Result<(Exop, LdapResult), io::Error> {
if self.1.rc == 0 {
Ok((self.0, self.1))
} else {
Err(io::Error::new(io::ErrorKind::Other, self.1))
}
}
pub fn non_error(self) -> Result<(Exop, LdapResult), io::Error> {
if self.1.rc == 0 || self.1.rc == 10 {
Ok((self.0, self.1))
} else {
Err(io::Error::new(io::ErrorKind::Other, self.1))
}
}
}