cdns-rs 1.2.2

A native Sync/Async Rust implementation of client DNS resolver.
Documentation
/*-
 * cdns-rs - a simple sync/async DNS query library
 * 
 * Copyright (C) 2020  Aleksandr Morozov
 * 
 * Copyright (C) 2025 Aleksandr Morozov
 * 
 * The syslog-rs crate can be redistributed and/or modified
 * under the terms of either of the following licenses:
 *
 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
 *                     
 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
 */

use std::{error::Error, fmt::{self}};


pub(crate)
fn map_read_err(e: std::io::Error) -> CDnsError
{
    return CDnsError::new(CDnsErrorType::IoError, format!("{}", e));
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CDnsSuperError(CDnsError);

impl Error for CDnsSuperError
{
    fn source(&self) -> Option<&(dyn Error + 'static)> 
    {
        Some(&self.0)
    }
}

impl fmt::Display for CDnsSuperError
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
    {
        write!(f, "{}", self.source().unwrap())
    }
}

impl From<CDnsError> for CDnsSuperError
{
    fn from(value: CDnsError) -> Self 
    {
        return Self(value);
    }
}

#[derive(Clone, PartialEq, Eq)]
pub struct CDnsError
{
    pub err_code: CDnsErrorType,
    pub message: String,
}

impl CDnsError
{
    pub fn new(err_code: CDnsErrorType, msg: String) -> Self
    {
        return CDnsError{err_code: err_code, message: msg};
    }
}

impl Error for CDnsError {}

impl fmt::Display for CDnsError 
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result 
    {
        write!(f, "cdns: [{}], {}", self.err_code, self.message)
    }
}
impl fmt::Debug for CDnsError 
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result 
    {
        write!(f, "cdns: [{}], {}", self.err_code, self.message)
    }
}



#[derive(Clone, PartialEq, Eq)]
pub enum CDnsErrorType
{
    /// Error related to DNS response parsing
    DnsResponse,
    /// Received response with unknown ID in header
    RespIdMismatch,
    /// Internal error (assertions...)
    InternalError,
    /// Socket, File error
    IoError,
    /// Response was truncated
    MessageTruncated,
    /// Timeout event
    RequestTimeout,
    /// Error in configuraion file (format)
    ConfigError,
    /// Nameservers unreachable
    DnsNotAvailable,
    /// HTTP Errors
    HttpError,
    /// Connection type not supported
    SocketNotSupported,
    /// Received punycode is not ASCII
    PunycodeNotAscii,
}

impl fmt::Display for CDnsErrorType 
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result 
    {
        match *self 
        {
            Self::DnsResponse => 
                write!(f, "DNS response"),
            Self::RespIdMismatch => 
                write!(f, "Response ID mismatch"),
            Self::InternalError => 
                write!(f, "Internal Error"),
            Self::IoError => 
                write!(f, "IO Error"),
            Self::MessageTruncated => 
                write!(f, "Message was truncated"),
            Self::RequestTimeout => 
                write!(f, "Request receive timout"),
            Self::ConfigError => 
                write!(f, "Config file error"),
            Self::DnsNotAvailable => 
                write!(f, "DNS not available"),
            Self::HttpError => 
                write!(f, "HTTP Error"),
            Self::SocketNotSupported => 
                write!(f, "Socket connection is not supported"),
            Self::PunycodeNotAscii => 
                write!(f, "Received punycode with non ASCII characters"),
        }
    }
}

pub type CDnsResult<T> = Result<T, CDnsError>;

#[macro_export]
macro_rules! internal_error 
{
    ($src:expr,$($arg:tt)*) => (
        return std::result::Result::Err($crate::CDnsError::new($src, format!($($arg)*)))
    )
}

#[macro_export]
macro_rules! internal_error_map
{
    ($src:expr,$($arg:tt)*) => (
        $crate::CDnsError::new($src, format!($($arg)*))
    )
}

#[cfg(feature = "no_error_output")]
#[macro_export]
macro_rules! write_error 
{
    ($src:expr) => (
        {let _ = $src;}
    )
}

#[cfg(not(feature = "no_error_output"))]
#[macro_export]
macro_rules! write_error 
{
    ($src:expr) => (
        eprintln!("{}", $src)
    )
}