1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! Macros for use in rdata definitions.
//!
//! These macros are not public but are used by the super module only. They
//! are here so that `mod.rs` doesn’t become too unwieldly.

macro_rules! master_types {
    ( $( $module:ident::{  $( $rtype:ident => $full_rtype:ty, )*  })* ) => {
        $(
            pub use self::$module::{ $( $rtype ),* };
        )*

        /// An enum with all the record data that can appear in master files.
        ///
        /// This enum contains variants for all the implemented record data
        /// types in their owned form plus the `Generic` variant record data
        /// of any other type.
        #[derive(Clone, Debug)]
        pub enum MasterRecordData {
            $(
                $(
                    $rtype($full_rtype),
                )*
            )*
            Generic(::iana::Rtype, Vec<u8>),
        }

        impl MasterRecordData {
            pub fn scan<S>(rtype: ::iana::Rtype, scanner: &mut S,
                           origin: Option<&::bits::name::DNameSlice>)
                           -> ::master::ScanResult<Self>
                        where S: ::master::Scanner {
                // First try the generic format for everything.
                let err = match ::rdata::generic::scan(scanner) {
                    Ok(some) => {
                        return Ok(MasterRecordData::Generic(rtype, some))
                    }
                    Err(err) => err
                };
                // Now see if we have a master type that can parse this for
                // real.
                match rtype {
                    $(
                        $(
                            ::iana::Rtype::$rtype => {
                                $rtype::scan(scanner, origin)
                                    .map(MasterRecordData::$rtype)
                            }
                        )*
                    )*
                    // We don’t. Good thing we kept the error.
                    _ => Err(err)
                }
            }
        }

        impl ::bits::RecordData for MasterRecordData {
            fn rtype(&self) -> ::iana::Rtype {
                match *self {
                    $(
                        $(
                            MasterRecordData::$rtype(ref data) => {
                                data.rtype()
                            }
                        )*
                    )*
                    MasterRecordData::Generic(rtype, _) => rtype
                }
            }

            fn compose<C>(&self, mut target: C) -> ::bits::ComposeResult<()>
                       where C: AsMut<::bits::Composer> {
                match *self {
                    $(
                        $(
                            MasterRecordData::$rtype(ref data) => {
                                data.compose(target)
                            }
                        )*
                    )*
                    MasterRecordData::Generic(_, ref data) => {
                        target.as_mut().compose_bytes(data)
                    }
                }
            }
        }

        impl ::std::fmt::Display for MasterRecordData {
            fn fmt(&self, f: &mut ::std::fmt::Formatter)
                   -> ::std::fmt::Result {
                match *self {
                    $(
                        $(
                            MasterRecordData::$rtype(ref data) => {
                                ::std::fmt::Display::fmt(data, f)
                            }
                        )*
                    )*
                    MasterRecordData::Generic(_, ref data) => {
                        ::rdata::generic::fmt(data, f)
                    }
                }
            }
        }

        /// Helper function for `fmt_rdata()`.
        ///
        /// This function contains the part of `fmt_rdata()` that needs to
        /// be generated via the `master_types!` macro.
        fn fmt_master_data(rtype: ::iana::Rtype,
                           parser: &mut ::bits::Parser,
                           f: &mut ::std::fmt::Formatter)
                           -> Result<Option<()>, ::std::fmt::Error> {
            use bits::rdata::ParsedRecordData;

            match rtype {
                $(
                    $(
                        ::iana::Rtype::$rtype => {
                            match ::rdata::parsed::$rtype::parse(rtype,
                                                                 parser) {
                                Ok(None) => unreachable!(),
                                Ok(Some(data)) => {
                                    ::std::fmt::Display::fmt(&data, f)
                                                        .map(Some)
                                }
                                Err(err) => {
                                    write!(f, "<invalid data: {}>", err)
                                        .map(Some)
                                }
                            }
                        }
                    )*
                )*
                _ => Ok(None)
            }
        }
    }
}

macro_rules! pseudo_types {
    ( $( $module:ident::{  $( $rtype:ident ),*  };)* ) => {
        $(
            pub use self::$module::{ $( $rtype ),* };
        )*
    }
}