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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
use std::fmt; use std::net::IpAddr; use std::str::FromStr; use std::vec::IntoIter; use crate::errors::NetAddsError; use crate::iter::{IntoSmartIterator, IpAddrSmartIterator, Ipv4AddrSmartIterator, Ipv6AddrSmartIterator}; mod v4; pub use v4::*; mod v6; pub use v6::*; /// An IP address network, either IPv4 or IPv6. /// /// This enum can contain either an [`Ipv4AddrNetwork`] or an [`Ipv6AddrNetwork`], see their /// respective documentation for more details. /// /// The size of an `IpAddrNetwork` struct may vary depending on the target operating system. /// /// # Textual representation /// /// `IpAddrNetwork` provides a [`FromStr`] implementation. The two parts are divided by `/`. /// /// For IPv4, the first part must contain an IPv4. The second part can either contain an IPv4 /// or an u8 between 0 and 32 which is valid as a netmask prefix. /// /// For IPv6, the first part must contain an IPv6. The second part can either contain an IPv6 /// or an u8 between 0 and 128 which is valid as a netmask prefix. /// /// [`FromStr`]: std::str::FromStr /// /// # Examples /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap()); /// /// assert!(network.is_ipv4()); /// assert_eq!(Ok(network), "192.168.0.10/24".parse()); /// assert_eq!(Ok(network), "192.168.0.10/255.255.255.0".parse()); /// /// let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFF00); /// let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new_with_addr(Ipv6Addr::from(0x1), netmask).unwrap()); /// /// assert!(network.is_ipv6()); /// assert_eq!(Ok(network), "::1/120".parse()); /// assert_eq!(Ok(network), "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00".parse()); /// ``` #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum IpAddrNetwork { V4(Ipv4AddrNetwork), V6(Ipv6AddrNetwork) } impl IpAddrNetwork { /// Returns the ip addr. pub fn ip (self) -> IpAddr { match self { IpAddrNetwork::V4(v4) => IpAddr::V4(v4.ip()), IpAddrNetwork::V6(v6) => IpAddr::V6(v6.ip()) } } /// Returns the netmask prefix. pub fn prefix (self) -> u8 { match self { IpAddrNetwork::V4(v4) => v4.prefix(), IpAddrNetwork::V6(v6) => v6.prefix() } } /// Returns the netmask addr. pub fn netmask (self) -> IpAddr { match self { IpAddrNetwork::V4(v4) => IpAddr::V4(v4.netmask()), IpAddrNetwork::V6(v6) => IpAddr::V6(v6.netmask()) } } /// Returns the network addr. pub fn network (self) -> IpAddr { match self { IpAddrNetwork::V4(v4) => IpAddr::V4(v4.network()), IpAddrNetwork::V6(v6) => IpAddr::V6(v6.network()) } } /// Returns the broadcast addr. pub fn broadcast (self) -> IpAddr { match self { IpAddrNetwork::V4(v4) => IpAddr::V4(v4.broadcast()), IpAddrNetwork::V6(v6) => IpAddr::V6(v6.broadcast()) } } /// Returns all ip of the network including the network and the broadcast addr. /// /// # Examples: /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 162, 0, 10), 30).unwrap()); /// /// assert_eq!(network.all(), vec![ /// Ipv4Addr::new(192, 162, 0, 8), /// Ipv4Addr::new(192, 162, 0, 9), /// Ipv4Addr::new(192, 162, 0, 10), /// Ipv4Addr::new(192, 162, 0, 11) /// ]); /// /// let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap()); /// /// assert_eq!(network.all(), vec![ /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2), /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3) /// ]); /// ``` pub fn all (self) -> Vec<IpAddr> { match self { IpAddrNetwork::V4(v4) => v4.all().into_iter().map(IpAddr::V4).collect(), IpAddrNetwork::V6(v6) => v6.all().into_iter().map(IpAddr::V6).collect() } } /// Returns all hosts (exclude network & broadcast addr). /// /// # Examples: /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 162, 0, 10), 30).unwrap()); /// /// assert_eq!(network.hosts(), vec![ /// Ipv4Addr::new(192, 162, 0, 9), /// Ipv4Addr::new(192, 162, 0, 10) /// ]); /// /// let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap()); /// /// assert_eq!(network.hosts(), vec![ /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2) /// ]); /// ``` pub fn hosts (self) -> Vec<IpAddr> { match self { IpAddrNetwork::V4(v4) => v4.hosts().into_iter().map(IpAddr::V4).collect(), IpAddrNetwork::V6(v6) => v6.hosts().into_iter().map(IpAddr::V6).collect() } } /// Returns the number of ip's included in the network including the network and the broadcast addr. /// /// # Examples: /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap()); /// assert_eq!(network.size(), 256); /// /// let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 120).unwrap()); /// assert_eq!(network.size(), 256); /// ``` pub fn size (&self) -> u128 { match self { IpAddrNetwork::V4(v4) => u128::from(v4.size()), IpAddrNetwork::V6(v6) => v6.size() } } /// Returns true if the ip argument is included in the network, else returns false. /// /// Panic if IPv4 and IPv6 are mixed. /// /// # Examples: /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap()); /// /// assert!(network.has(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 0)))); /// assert!(network.has(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 142)))); /// assert!(network.has(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 255)))); /// /// assert!(!network.has(IpAddr::V4(Ipv4Addr::new(192, 169, 0, 0)))); /// /// let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 64).unwrap()); /// /// assert!(network.has(IpAddr::V6(Ipv6Addr::from(0x1)))); /// assert!(network.has(IpAddr::V6(Ipv6Addr::from(0xA)))); /// assert!(network.has(IpAddr::V6(Ipv6Addr::from(0x00FF)))); /// /// assert!(!network.has(IpAddr::V6(Ipv6Addr::from(0xFFFFFFFFFFFFFFFFFF00000000000000)))); /// ``` pub fn has (&self, ip: IpAddr) -> bool { match (self, ip) { (IpAddrNetwork::V4(v4), IpAddr::V4(iv4)) => v4.has(iv4), (IpAddrNetwork::V6(v6), IpAddr::V6(iv6)) => v6.has(iv6), _ => panic!("cannot mix IPv4 and IPv6 to check if network includes ip") } } /// Returns true if the network contains IPv4, else return false. /// /// # Examples: /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 162, 0, 10), 30).unwrap()); /// assert!(network.is_ipv4()); /// /// let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap()); /// assert!(!network.is_ipv4()); /// ``` pub fn is_ipv4 (&self) -> bool { match self { IpAddrNetwork::V4(_) => true, IpAddrNetwork::V6(_) => false } } /// Returns true if the network contains IPv6, else return false. /// /// # Examples: /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap()); /// assert!(network.is_ipv6()); /// /// let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 162, 0, 10), 30).unwrap()); /// assert!(!network.is_ipv6()); /// ``` pub fn is_ipv6 (&self) -> bool { match self { IpAddrNetwork::V4(_) => false, IpAddrNetwork::V6(_) => true } } } impl fmt::Display for IpAddrNetwork { fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { IpAddrNetwork::V4(v4) => write!(f, "V4({})", v4), IpAddrNetwork::V6(v6) => write!(f, "V6({})", v6) } } } impl From<Ipv4AddrNetwork> for IpAddrNetwork { /// Create an `IpAddrNetwork::V4` from an `Ipv4AddrNetwork`. /// /// # Examples: /// /// ``` /// use std::net::Ipv4Addr; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork}; /// /// let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap(); /// /// assert_eq!(IpAddrNetwork::from(network), IpAddrNetwork::V4(network)); /// ``` fn from (network: Ipv4AddrNetwork) -> IpAddrNetwork { IpAddrNetwork::V4(network) } } impl From<Ipv6AddrNetwork> for IpAddrNetwork { /// Create an `IpAddrNetwork::V6` from an `Ipv6AddrNetwork`. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::{IpAddrNetwork, Ipv6AddrNetwork}; /// /// let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 120).unwrap(); /// /// assert_eq!(IpAddrNetwork::from(network), IpAddrNetwork::V6(network)); /// ``` fn from (network: Ipv6AddrNetwork) -> IpAddrNetwork { IpAddrNetwork::V6(network) } } impl FromStr for IpAddrNetwork { type Err = NetAddsError; /// Parse a string as `IpAddrNetwork`. /// /// If the string representation is not valid return an `NetAddsErrorAddrParse(NetworkAddrParseError)`. /// /// # Examples: /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// let ip = Ipv4Addr::new(192, 168, 0, 0); /// let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(ip, 24).unwrap()); /// /// assert_eq!(Ok(network), "192.168.0.0/24".parse()); /// assert_eq!(Ok(network), "192.168.0.0/255.255.255.0".parse()); /// /// let ip = Ipv6Addr::new(0xFFFF, 0, 0, 0, 0, 0, 0, 0xFF); /// let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(ip, 120).unwrap()); /// /// assert_eq!(Ok(network), "ffff::ff/120".parse()); /// assert_eq!(Ok(network), "ffff::ff/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00".parse()); /// ``` fn from_str (s: &str) -> Result<Self, Self::Err> { Ipv4AddrNetwork::from_str(s) .map(IpAddrNetwork::V4) .or_else(move |_| Ipv6AddrNetwork::from_str(s).map(IpAddrNetwork::V6)) } } impl IntoIterator for IpAddrNetwork { type Item = IpAddr; type IntoIter = IntoIter<Self::Item>; /// Create a `IpAddr` iterator. /// /// # Examples: /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// /// let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 30).unwrap(); /// let mut iter = IpAddrNetwork::V4(network).into_iter(); /// /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 8)))); /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 9)))); /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10)))); /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 11)))); /// assert_eq!(iter.next(), None); /// /// let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap(); /// let mut iter = IpAddrNetwork::V6(network).into_iter(); /// /// 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(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2)))); /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3)))); /// assert_eq!(iter.next(), None); /// ``` fn into_iter (self) -> Self::IntoIter { self.all().into_iter() } } impl IntoSmartIterator for IpAddrNetwork { type Item = IpAddr; type IntoSmartIter = IpAddrSmartIterator; /// Create a smart `IpAddr` iterator. /// /// # Examples: /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; /// /// /// let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 30).unwrap(); /// let mut iter = IpAddrNetwork::V4(network).into_iter(); /// /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 8)))); /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 9)))); /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10)))); /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 11)))); /// assert_eq!(iter.next(), None); /// /// let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap(); /// let mut iter = IpAddrNetwork::V6(network).into_iter(); /// /// 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(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2)))); /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3)))); /// assert_eq!(iter.next(), None); /// ``` fn into_smart_iter (self) -> Self::IntoSmartIter { match self { IpAddrNetwork::V4(v4) => { IpAddrSmartIterator::V4(Ipv4AddrSmartIterator::new(v4.network(), v4.broadcast())) }, IpAddrNetwork::V6(v6) => { IpAddrSmartIterator::V6(Ipv6AddrSmartIterator::new(v6.network(), v6.broadcast())) } } } } #[derive(Clone, Debug, PartialEq, Eq)] pub struct NetworkAddrParseError (); impl std::error::Error for NetworkAddrParseError {} impl fmt::Display for NetworkAddrParseError { fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", "invalid IP address network syntax") } } #[derive(Clone, Debug, PartialEq, Eq)] pub struct InvalidNetmaskError (pub IpAddr); impl std::error::Error for InvalidNetmaskError {} impl fmt::Display for InvalidNetmaskError { fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", format!("invalid netmask ({})", self.0)) } } #[derive(Clone, Debug, PartialEq, Eq)] pub struct InvalidNetmaskPrefixError (pub u8); impl std::error::Error for InvalidNetmaskPrefixError {} impl fmt::Display for InvalidNetmaskPrefixError { fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", format!("invalid netmask prefix ({})", self.0)) } } #[cfg(test)] mod test { use std::net::{Ipv4Addr, Ipv6Addr}; use crate::{ IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork, NetAddsError, NetworkAddrParseError, InvalidNetmaskPrefixError }; #[test] fn from_str_ip_addr_v4_network () { let ip = Ipv4Addr::new(192, 168, 0, 10); let network = Ok(IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(ip, 0).unwrap())); assert_eq!(network, "192.168.0.10/0".parse()); assert_eq!(network, "192.168.0.10/0.0.0.0".parse()); let network = Ok(IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(ip, 24).unwrap())); assert_eq!(network, "192.168.0.10/255.255.255.0".parse()); assert_eq!(network, "192.168.0.10/24".parse()); let network = Ok(IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(ip, 32).unwrap())); assert_eq!(network, "192.168.0.10/255.255.255.255".parse()); assert_eq!(network, "192.168.0.10/32".parse()); let err = Err(NetAddsError::NetworkAddrParse(NetworkAddrParseError())); // invalid prefix. assert_eq!(err, "0.0.0.1/33".parse::<IpAddrNetwork>()); // ip is out of range. assert_eq!(err, "256.0.0.1/24".parse::<IpAddrNetwork>()); // ip is to short. assert_eq!(err, "127.0.0/24".parse::<IpAddrNetwork>()); // no netmask. assert_eq!(err, "127.0.0.1".parse::<IpAddrNetwork>()); // too many ip. assert_eq!(err, "255.0.0.1/255.255.255.0/255.255.255.0".parse::<IpAddrNetwork>()); // no ip before `/`. assert_eq!(err, "/24".parse::<IpAddrNetwork>()); // no netmask after `/`. assert_eq!(err, "127.0.0.1/".parse::<IpAddrNetwork>()); } #[test] fn from_str_ip_addr_v6_network () { let ip = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1); let network = Ok(IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(ip, 0).unwrap())); assert_eq!(network, "::1/0".parse()); assert_eq!(network, "::1/::".parse()); assert_eq!(network, "::1/0:0:0:0:0:0:0:0".parse()); assert_eq!(network, "::1/0000:0000:0000:0000:0000:0000:0000:0000".parse()); assert_eq!(network, "::1/0000:0000:0000::0000:0000".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/0".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/::".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/0000:0000:0000::0000:0000".parse()); let network = Ok(IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(ip, 96).unwrap())); assert_eq!(network, "::1/96".parse()); assert_eq!(network, "::1/ffff:ffff:ffff:ffff:ffff:ffff:0000:0000".parse()); assert_eq!(network, "::1/ffff:ffff:ffff:ffff:ffff:ffff::".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/96".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/ffff:ffff:ffff:ffff:ffff:ffff:0000:0000".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/ffff:ffff:ffff:ffff:ffff:ffff::".parse()); let network = Ok(IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(ip, 128).unwrap())); assert_eq!(network, "::1/128".parse()); assert_eq!(network, "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/128".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff".parse()); // invalid prefix. let err = Err(NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError(129))); assert_eq!(err, "::1/129".parse::<IpAddrNetwork>()); let err = Err(NetAddsError::NetworkAddrParse(NetworkAddrParseError())); // ip is out of range (invalid char "z"). assert_eq!(err, "::fffz/24".parse::<IpAddrNetwork>()); // ip is to short. assert_eq!(err, "0:0:0:0:0:0:0/24".parse::<IpAddrNetwork>()); // no netmask. assert_eq!(err, "::1".parse::<IpAddrNetwork>()); // too many ip. let s = "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"; assert_eq!(err, s.parse::<IpAddrNetwork>()); // no ip before `/`. assert_eq!(err, "/128".parse::<IpAddrNetwork>()); // no netmask after `/`. assert_eq!(err, "::1/".parse::<IpAddrNetwork>()); } }