Skip to content

Commit

Permalink
AresError is big enough to get its own file now
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Jul 30, 2015
1 parent a0387bb commit 35664a8
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 163 deletions.
6 changes: 2 additions & 4 deletions src/a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ use std::ptr;
use std::slice;
use std::str;

use types::{
AresError,
hostent,
};
use error::AresError;
use types::hostent;
use utils::ares_error;

/// The result of a successful A lookup.
Expand Down
6 changes: 2 additions & 4 deletions src/aaaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ use std::ptr;
use std::slice;
use std::str;

use types::{
AresError,
hostent,
};
use error::AresError;
use types::hostent;
use utils::ares_error;

/// The result of a successful AAAA lookup.
Expand Down
2 changes: 1 addition & 1 deletion src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use cname::{
CNameResult,
query_cname_callback,
};
use error::AresError;
use flags::Flags;
use host::{
HostResults,
Expand Down Expand Up @@ -56,7 +57,6 @@ use srv::{
};
use types::{
AddressFamily,
AresError,
DnsClass,
IpAddr,
QueryType,
Expand Down
6 changes: 2 additions & 4 deletions src/cname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use std::ptr;
use std::slice;
use std::str;

use types::{
AresError,
hostent,
};
use error::AresError;
use types::hostent;
use utils::ares_error;

/// The result of a successful CNAME lookup.
Expand Down
133 changes: 133 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
extern crate c_ares_sys;
extern crate libc;

use std::error;
use std::ffi::CStr;
use std::fmt::{
Display,
Error,
Formatter,
};
use std::str;

/// Error codes that the library might return.
#[derive(Debug, Clone, Copy)]
pub enum AresError {
/// DNS server returned answer with no data.
ENODATA = c_ares_sys::ARES_ENODATA as isize,

/// DNS server claims query was misformatted.
EFORMERR = c_ares_sys::ARES_EFORMERR as isize,

/// DNS server returned general failure.
ESERVFAIL = c_ares_sys::ARES_ESERVFAIL as isize,

/// Domain name not found.
ENOTFOUND = c_ares_sys::ARES_ENOTFOUND as isize,

/// DNS server does not implement requested operation.
ENOTIMP = c_ares_sys::ARES_ENOTIMP as isize,

/// DNS server refused query.
EREFUSED = c_ares_sys::ARES_EREFUSED as isize,

/// Misformatted DNS query.
EBADQUERY = c_ares_sys::ARES_EBADQUERY as isize,

/// Misformatted domain name.
EBADNAME = c_ares_sys::ARES_EBADNAME as isize,

/// Unsupported address family.
EBADFAMILY = c_ares_sys::ARES_EBADFAMILY as isize,

/// Misformatted DNS reply.
EBADRESP = c_ares_sys::ARES_EBADRESP as isize,

/// Could not contact DNS servers.
ECONNREFUSED = c_ares_sys::ARES_ECONNREFUSED as isize,

/// Timeout while contacting DNS servers.
ETIMEOUT = c_ares_sys::ARES_ETIMEOUT as isize,

/// End of file.
EOF = c_ares_sys::ARES_EOF as isize,

/// Error reading file.
EFILE = c_ares_sys::ARES_EFILE as isize,

/// Out of memory.
ENOMEM = c_ares_sys::ARES_ENOMEM as isize,

/// Channel is being destroyed.
EDESTRUCTION = c_ares_sys::ARES_EDESTRUCTION as isize,

/// Misformatted string.
EBADSTR = c_ares_sys::ARES_EBADSTR as isize,

/// Illegal flags specified.
EBADFLAGS = c_ares_sys::ARES_EBADFLAGS as isize,

/// Given hostname is not numeric.
ENONAME = c_ares_sys::ARES_ENONAME as isize,

/// Illegal hints flags specified.
EBADHINTS = c_ares_sys::ARES_EBADHINTS as isize,

/// c-ares library initialization not yet performed.
ENOTINITIALIZED = c_ares_sys::ARES_ENOTINITIALIZED as isize,

/// Error loading iphlpapi.dll.
ELOADIPHLPAPI = c_ares_sys::ARES_ELOADIPHLPAPI as isize,

/// Could not find GetNetworkParams function.
EADDRGETNETWORKPARAMS = c_ares_sys::ARES_EADDRGETNETWORKPARAMS as isize,

/// DNS query cancelled.
ECANCELLED = c_ares_sys::ARES_ECANCELLED as isize,

/// Unknown error.
UNKNOWN,
}

impl Display for AresError {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
let text = match *self {
AresError::ENODATA => "ENODATA",
AresError::EFORMERR => "EFORMERR",
AresError::ESERVFAIL => "ESERVFAIL",
AresError::ENOTFOUND => "ENOTFOUND",
AresError::ENOTIMP => "ENOTIMP",
AresError::EREFUSED => "EREFUSED",
AresError::EBADQUERY => "EBADQUERY",
AresError::EBADNAME => "EBADNAME",
AresError::EBADFAMILY => "EBADFAMILY",
AresError::EBADRESP => "EBADRESP",
AresError::ECONNREFUSED => "ECONNREFUSED",
AresError::ETIMEOUT => "ETIMEOUT",
AresError::EOF => "EOF",
AresError::EFILE => "EFILE",
AresError::ENOMEM => "ENOMEM",
AresError::EDESTRUCTION => "EDESTRUCTION",
AresError::EBADSTR => "EBADSTR",
AresError::EBADFLAGS => "EBADFLAGS",
AresError::ENONAME => "ENONAME",
AresError::EBADHINTS => "EBADHINTS",
AresError::ENOTINITIALIZED => "ENOTINITIALIZED",
AresError::ELOADIPHLPAPI => "ELOADIPHLPAPI",
AresError::EADDRGETNETWORKPARAMS => "EADDRGETNETWORKPARAMS",
AresError::ECANCELLED => "ECANCELLED",
AresError::UNKNOWN => "UNKNOWN",
};
formatter.write_str(text)
}
}

impl error::Error for AresError {
fn description(&self) -> &str {
unsafe {
let ptr = c_ares_sys::ares_strerror(*self as libc::c_int);
let buf = CStr::from_ptr(ptr).to_bytes();
str::from_utf8_unchecked(buf)
}
}
}
2 changes: 1 addition & 1 deletion src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use std::net::{
use std::ptr;
use std::str;

use error::AresError;
use types::{
AddressFamily,
AresError,
hostent,
IpAddr,
};
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod aaaa;
mod srv;
mod channel;
mod cname;
mod error;
pub mod flags;
mod host;
mod mx;
Expand Down Expand Up @@ -62,6 +63,7 @@ pub use channel::{
Options,
};
pub use cname::CNameResult;
pub use error::AresError;
pub use host::{
HostAddressResult,
HostAliasResult,
Expand All @@ -86,7 +88,6 @@ pub use ptr::{
};
pub use types::{
AddressFamily,
AresError,
INVALID_FD,
IpAddr,
};
Expand Down
2 changes: 1 addition & 1 deletion src/mx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ptr;
use std::slice;
use std::str;

use types::AresError;
use error::AresError;
use utils::ares_error;

/// The result of a successful MX lookup.
Expand Down
2 changes: 1 addition & 1 deletion src/nameinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::marker::PhantomData;
use std::mem;
use std::str;

use types::AresError;
use error::AresError;
use utils::ares_error;

/// The result of a successful name-info lookup.
Expand Down
2 changes: 1 addition & 1 deletion src/naptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::str;
use std::ptr;
use std::slice;

use types::AresError;
use error::AresError;
use utils::ares_error;

/// The result of a successful NAPTR lookup.
Expand Down
6 changes: 2 additions & 4 deletions src/ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use std::ptr;
use std::slice;
use std::str;

use types::{
AresError,
hostent,
};
use error::AresError;
use types::hostent;
use utils::ares_error;

/// The result of a successful NS lookup.
Expand Down
6 changes: 2 additions & 4 deletions src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use std::ptr;
use std::slice;
use std::str;

use types::{
AresError,
hostent,
};
use error::AresError;
use types::hostent;
use utils::ares_error;

/// The result of a successful PTR lookup.
Expand Down
2 changes: 1 addition & 1 deletion src/soa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ptr;
use std::slice;
use std::str;

use types::AresError ;
use error::AresError ;
use utils::ares_error;

/// The result of a successful SOA lookup.
Expand Down
2 changes: 1 addition & 1 deletion src/srv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::str;
use std::ptr;
use std::slice;

use types::AresError;
use error::AresError;
use utils::ares_error;

/// The result of a successful SRV lookup.
Expand Down
2 changes: 1 addition & 1 deletion src/txt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ptr;
use std::slice;
use std::str;

use types::AresError;
use error::AresError;
use utils::ares_error;

/// The result of a successful TXT lookup.
Expand Down
Loading

0 comments on commit 35664a8

Please sign in to comment.