mhost 0.11.3

Fast, async DNS lookup library and CLI -- modern dig/host replacement with parallel multi-server queries, DoH, DoT, subdomain discovery, and zone verification
Documentation
// Copyright 2017-2021 Lukas Pustina <lukas@pustina.de>
//
// Derived from trust-dns by Benjamin Fry <benjaminfry@me.com>
// cf. https://github.com/bluejekyll/trust-dns
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use hickory_resolver::Name;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
#[allow(clippy::upper_case_acronyms)]
pub struct MX {
    preference: u16,
    exchange: Name,
}

impl MX {
    pub fn new(preference: u16, exchange: Name) -> MX {
        MX { preference, exchange }
    }

    pub fn preference(&self) -> u16 {
        self.preference
    }

    pub fn exchange(&self) -> &Name {
        &self.exchange
    }
}

#[doc(hidden)]
impl From<hickory_resolver::proto::rr::rdata::MX> for MX {
    fn from(mx: hickory_resolver::proto::rr::rdata::MX) -> Self {
        MX::new(mx.preference(), mx.exchange().clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mx_new_and_accessors() {
        let exchange = Name::from_utf8("mail.example.com.").unwrap();
        let mx = MX::new(10, exchange.clone());

        assert_eq!(mx.preference(), 10);
        assert_eq!(mx.exchange(), &exchange);
    }
}