use crate::AddressResolveErrorKind;
use std::{fmt, io};
#[derive(Debug)]
pub struct AddressResolveError {
pub kind: AddressResolveErrorKind,
pub code: Option<i32>,
pub message: String,
pub system_error: Option<io::Error>,
}
impl fmt::Display for AddressResolveError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.message.fmt(formatter)
}
}
impl std::error::Error for AddressResolveError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.system_error.as_ref().map(|error| error as _)
}
}
impl From<AddressResolveError> for io::Error {
fn from(error: AddressResolveError) -> Self {
use AddressResolveErrorKind::*;
let kind = match error.kind {
TemporaryFailure => io::ErrorKind::WouldBlock,
NameNotFound | ServiceUnavailable => io::ErrorKind::NotFound,
InvalidHints => io::ErrorKind::InvalidInput,
OutOfMemory => io::ErrorKind::OutOfMemory,
System => error
.system_error
.as_ref()
.map_or(io::ErrorKind::Other, io::Error::kind),
PermanentFailure | Other => io::ErrorKind::Other,
};
Self::new(kind, error)
}
}