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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use std::fmt;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

pub mod v4;
use v4::*;

pub mod v6;
use v6::*;

/// An IP address smart iterator, either IPv4 or IPv6.
///
/// A smart iterator is an iterator that doesn't store all the items in memory. It's usefull for long series.
///
/// # Examples
///
/// ```
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// use net_adds::{IpAddrSmartIterator, Ipv4AddrSmartIterator, Ipv6AddrSmartIterator};
///
/// let mut iter = IpAddrSmartIterator::V4(Ipv4AddrSmartIterator::new(Ipv4Addr::from(0), Ipv4Addr::from(1)));
///
/// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))));
/// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 1))));
/// assert_eq!(iter.next(), None);
///
/// let mut iter = IpAddrSmartIterator::V6(Ipv6AddrSmartIterator::new(Ipv6Addr::from(0), Ipv6Addr::from(1)));
///
/// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0))));
/// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))));
/// assert_eq!(iter.next(), None);
/// ```
#[derive(Clone, Debug)]
pub enum IpAddrSmartIterator {
    V4(Ipv4AddrSmartIterator),
    V6(Ipv6AddrSmartIterator)
}

impl IpAddrSmartIterator {
    /// Returns true if the iterator is an IPv4 iterator, else return false.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrSmartIterator, Ipv4AddrSmartIterator, Ipv6AddrSmartIterator};
    ///
    /// let start = Ipv4Addr::new(0, 0, 0, 0);
    /// let end = Ipv4Addr::new(0, 0, 0, 2);
    /// assert_eq!(IpAddrSmartIterator::V4(Ipv4AddrSmartIterator::new(start, end)).is_ipv4(), true);
    ///
    /// let start = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
    /// let end = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2);
    /// assert_eq!(IpAddrSmartIterator::V6(Ipv6AddrSmartIterator::new(start, end)).is_ipv4(), false);
    /// ```
    pub fn is_ipv4 (&self) -> bool {
        match self {
            IpAddrSmartIterator::V4(_) => true,
            IpAddrSmartIterator::V6(_) => false
        }
    }

    /// Returns true if the iterator is an IPv6 iterator, else return false.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrSmartIterator, Ipv4AddrSmartIterator, Ipv6AddrSmartIterator};
    ///
    /// let start = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
    /// let end = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2);
    /// assert_eq!(IpAddrSmartIterator::V6(Ipv6AddrSmartIterator::new(start, end)).is_ipv6(), true);
    ///
    /// let start = Ipv4Addr::new(0, 0, 0, 0);
    /// let end = Ipv4Addr::new(0, 0, 0, 2);
    /// assert_eq!(IpAddrSmartIterator::V4(Ipv4AddrSmartIterator::new(start, end)).is_ipv6(), false);
    /// ```
    pub fn is_ipv6 (&self) -> bool {
        match self {
            IpAddrSmartIterator::V4(_) => false,
            IpAddrSmartIterator::V6(_) => true
        }
    }
}

impl Iterator for IpAddrSmartIterator {
    type Item = IpAddr;

    fn next (&mut self) -> Option<Self::Item> {
        match self {
            IpAddrSmartIterator::V4(iter) => Some(IpAddr::V4(iter.next()?)),
            IpAddrSmartIterator::V6(iter) => Some(IpAddr::V6(iter.next()?))
        }
    }
}

impl fmt::Display for IpAddrSmartIterator {
    fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IpAddrSmartIterator::V4(iter) => write!(f, "V4({})", iter),
            IpAddrSmartIterator::V6(iter) => write!(f, "V6({})", iter)
        }
    }
}

impl From<Ipv4AddrSmartIterator> for IpAddrSmartIterator {
    /// Create an `IpAddrSmartIterator::V4` from an `Ipv4AddrSmartIterator`.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr};
    ///
    /// use net_adds::{IpAddrSmartIterator, Ipv4AddrSmartIterator};
    ///
    /// let mut iter = IpAddrSmartIterator::from(Ipv4AddrSmartIterator::new(Ipv4Addr::from(0), Ipv4Addr::from(1)));
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::from(0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::from(1))));
    /// assert_eq!(iter.next(), None);
    /// ```
    fn from (iter: Ipv4AddrSmartIterator) -> IpAddrSmartIterator {
        IpAddrSmartIterator::V4(iter)
    }
}

impl From<Ipv6AddrSmartIterator> for IpAddrSmartIterator {
    /// Create an `IpAddrSmartIterator::V6` from an `Ipv6AddrSmartIterator`.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrSmartIterator, Ipv6AddrSmartIterator};
    ///
    /// let mut iter = IpAddrSmartIterator::from(Ipv6AddrSmartIterator::new(Ipv6Addr::from(0), Ipv6Addr::from(1)));
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::from(0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::from(1))));
    /// assert_eq!(iter.next(), None);
    /// ```
    fn from (iter: Ipv6AddrSmartIterator) -> IpAddrSmartIterator {
        IpAddrSmartIterator::V6(iter)
    }
}

impl From<(IpAddr, IpAddr)> for IpAddrSmartIterator {
    /// Create an `IpAddrSmartIterator` V4 or V6 from a tuple `(IpAddr, IpAddr)`.
    ///
    /// Panic if IPv4 and IPv6 are mixed.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::IpAddrSmartIterator;
    ///
    /// let mut iter = IpAddrSmartIterator::from((IpAddr::V4(Ipv4Addr::from(0)), IpAddr::V4(Ipv4Addr::from(1))));
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::from(0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::from(1))));
    /// assert_eq!(iter.next(), None);
    ///
    /// let mut iter = IpAddrSmartIterator::from((IpAddr::V6(Ipv6Addr::from(0)), IpAddr::V6(Ipv6Addr::from(1))));
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::from(0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::from(1))));
    /// assert_eq!(iter.next(), None);
    /// ```
    fn from (ips: (IpAddr, IpAddr)) -> IpAddrSmartIterator {
        match ips {
            (IpAddr::V4(a), IpAddr::V4(b)) => {
                IpAddrSmartIterator::V4(Ipv4AddrSmartIterator::new(a, b))
            },
            (IpAddr::V6(a), IpAddr::V6(b)) => {
                IpAddrSmartIterator::V6(Ipv6AddrSmartIterator::new(a, b))
            },
            _ => {
                panic!("cannot mix IPv4 and IPv6 to build a range ip")
            }
        }
    }
}

impl From<(Ipv4Addr, Ipv4Addr)> for IpAddrSmartIterator {
    /// Create an `IpAddrSmartIterator` from a tuple `(Ipv4Addr, Ipv4Addr)`.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr};
    ///
    /// use net_adds::IpAddrSmartIterator;
    ///
    /// let mut iter = IpAddrSmartIterator::from((Ipv4Addr::from(0), Ipv4Addr::from(1)));
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::from(0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::from(1))));
    /// assert_eq!(iter.next(), None);
    /// ```
    fn from (ips: (Ipv4Addr, Ipv4Addr)) -> IpAddrSmartIterator {
        IpAddrSmartIterator::V4(Ipv4AddrSmartIterator::new(ips.0, ips.1))
    }
}


impl From<(Ipv6Addr, Ipv6Addr)> for IpAddrSmartIterator {
    /// Create an `IpAddrSmartIterator` from a tuple `(Ipv6Addr, Ipv6Addr)`.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv6Addr};
    ///
    /// use net_adds::IpAddrSmartIterator;
    ///
    /// let mut iter = IpAddrSmartIterator::from((Ipv6Addr::from(0), Ipv6Addr::from(1)));
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::from(0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::from(1))));
    /// assert_eq!(iter.next(), None);
    /// ```
    fn from (ips: (Ipv6Addr, Ipv6Addr)) -> IpAddrSmartIterator {
        IpAddrSmartIterator::V6(Ipv6AddrSmartIterator::new(ips.0, ips.1))
    }
}