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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use core::fmt;
use std::net::IpAddr;

use crate::{
	errors::*,
	internal_traits::PrivInetPair,
	num::NumberOfAddresses,
	Family,
	InetIterator,
	InetPair,
	IpCidr,
	IpInet,
	IpInetPair,
	Ipv4InetPair,
	Ipv6InetPair,
};

impl IpInetPair {
	/// Whether representing an IPv4 network
	pub const fn is_ipv4(&self) -> bool {
		match self {
			Self::V4(_) => true,
			Self::V6(_) => false,
		}
	}

	/// Whether representing an IPv6 network
	pub const fn is_ipv6(&self) -> bool {
		match self {
			Self::V4(_) => false,
			Self::V6(_) => true,
		}
	}

	// --- copy of trait API

	/// Create new pair from two addresses in the same network
	///
	/// Fails if the addresses are not in the same network.
	pub const fn new(first: IpInet, second: IpInet) -> Result<Self, InetTupleError> {
		match (first, second) {
			(IpInet::V4(first), IpInet::V4(second)) => match Ipv4InetPair::new(first, second) {
				Ok(pair) => Ok(Self::V4(pair)),
				Err(e) => Err(e),
			},
			(IpInet::V6(first), IpInet::V6(second)) => match Ipv6InetPair::new(first, second) {
				Ok(pair) => Ok(Self::V6(pair)),
				Err(e) => Err(e),
			},
			_ => Err(InetTupleError::NotInSharedNetwork),
		}
	}

	/// Create new pair from two addresses and a common length
	///
	/// Fails if the network length is invalid for the addresses or the addresses are not in the same network.
	pub const fn new_from_addresses(
		first: IpAddr,
		second: IpAddr,
		len: u8,
	) -> Result<Self, InetTupleError> {
		match (first, second) {
			(IpAddr::V4(first), IpAddr::V4(second)) => {
				match Ipv4InetPair::new_from_addresses(first, second, len) {
					Ok(pair) => Ok(Self::V4(pair)),
					Err(e) => Err(e),
				}
			},
			(IpAddr::V6(first), IpAddr::V6(second)) => {
				match Ipv6InetPair::new_from_addresses(first, second, len) {
					Ok(pair) => Ok(Self::V6(pair)),
					Err(e) => Err(e),
				}
			},
			_ => Err(InetTupleError::NotInSharedNetwork),
		}
	}

	/// First address
	pub const fn first(&self) -> IpInet {
		match self {
			Self::V4(p) => IpInet::V4(p.first()),
			Self::V6(p) => IpInet::V6(p.first()),
		}
	}

	/// Second address
	pub const fn second(&self) -> IpInet {
		match self {
			Self::V4(p) => IpInet::V4(p.second()),
			Self::V6(p) => IpInet::V6(p.second()),
		}
	}

	/// network (i.e. drops the host information)
	pub const fn network(&self) -> IpCidr {
		match self {
			Self::V4(p) => IpCidr::V4(p.network()),
			Self::V6(p) => IpCidr::V6(p.network()),
		}
	}

	/// length in bits of the shared prefix of the contained addresses
	pub const fn network_length(&self) -> u8 {
		match self {
			Self::V4(p) => p.network_length(),
			Self::V6(p) => p.network_length(),
		}
	}

	/// IP family of the contained address ([`Ipv4`] or [`Ipv6`]).
	///
	/// [`Ipv4`]: Family::Ipv4
	/// [`Ipv6`]: Family::Ipv6
	pub const fn family(&self) -> Family {
		match self {
			Self::V4(_) => Family::Ipv4,
			Self::V6(_) => Family::Ipv6,
		}
	}

	/// Iterate over `first..=second` (inclusive)
	pub const fn iter(self) -> InetIterator<IpAddr> {
		InetIterator::_new(self)
	}
}

impl PrivInetPair for IpInetPair {
	fn _covered_addresses(&self) -> NumberOfAddresses {
		match self {
			Self::V4(p) => p._covered_addresses(),
			Self::V6(p) => p._covered_addresses(),
		}
	}

	fn _inc_first(&mut self) -> bool {
		match self {
			Self::V4(p) => p._inc_first(),
			Self::V6(p) => p._inc_first(),
		}
	}

	fn _dec_second(&mut self) -> bool {
		match self {
			Self::V4(p) => p._dec_second(),
			Self::V6(p) => p._dec_second(),
		}
	}
}

impl InetPair for IpInetPair {
	type Address = IpAddr;

	fn new(first: IpInet, second: IpInet) -> Result<Self, InetTupleError> {
		match (first, second) {
			(IpInet::V4(first), IpInet::V4(second)) => {
				Ok(Self::V4(Ipv4InetPair::new(first, second)?))
			},
			(IpInet::V6(first), IpInet::V6(second)) => {
				Ok(Self::V6(Ipv6InetPair::new(first, second)?))
			},
			_ => Err(InetTupleError::NotInSharedNetwork),
		}
	}

	fn new_from_addresses(first: IpAddr, second: IpAddr, len: u8) -> Result<Self, InetTupleError> {
		match (first, second) {
			(IpAddr::V4(first), IpAddr::V4(second)) => Ok(Self::V4(
				Ipv4InetPair::new_from_addresses(first, second, len)?,
			)),
			(IpAddr::V6(first), IpAddr::V6(second)) => Ok(Self::V6(
				Ipv6InetPair::new_from_addresses(first, second, len)?,
			)),
			_ => Err(InetTupleError::NotInSharedNetwork),
		}
	}

	fn first(&self) -> IpInet {
		match self {
			Self::V4(p) => IpInet::V4(p.first()),
			Self::V6(p) => IpInet::V6(p.first()),
		}
	}

	fn second(&self) -> IpInet {
		match self {
			Self::V4(p) => IpInet::V4(p.second()),
			Self::V6(p) => IpInet::V6(p.second()),
		}
	}

	fn network(&self) -> IpCidr {
		match self {
			Self::V4(p) => IpCidr::V4(p.network()),
			Self::V6(p) => IpCidr::V6(p.network()),
		}
	}

	fn network_length(&self) -> u8 {
		match self {
			Self::V4(p) => p.network_length(),
			Self::V6(p) => p.network_length(),
		}
	}

	fn family(&self) -> Family {
		match self {
			Self::V4(_) => Family::Ipv4,
			Self::V6(_) => Family::Ipv6,
		}
	}

	fn iter(self) -> InetIterator<IpAddr> {
		self.iter()
	}
}

impl fmt::Display for IpInetPair {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::V4(c) => fmt::Display::fmt(c, f),
			Self::V6(c) => fmt::Display::fmt(c, f),
		}
	}
}

impl From<Ipv4InetPair> for IpInetPair {
	fn from(c: Ipv4InetPair) -> Self {
		Self::V4(c)
	}
}

impl From<Ipv6InetPair> for IpInetPair {
	fn from(c: Ipv6InetPair) -> Self {
		Self::V6(c)
	}
}

impl IntoIterator for IpInetPair {
	type IntoIter = InetIterator<IpAddr>;
	type Item = IpInet;

	fn into_iter(self) -> Self::IntoIter {
		self.iter()
	}
}