mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-16 16:58:50 +00:00
Merge pull request #103 from lucab/ups/parse-net-pair
ipnetwork: add netmask constructors
This commit is contained in:
@ -3,30 +3,40 @@ use ipnetwork::{Ipv4Network, Ipv6Network};
|
|||||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||||
|
|
||||||
fn parse_ipv4_benchmark(c: &mut Criterion) {
|
fn parse_ipv4_benchmark(c: &mut Criterion) {
|
||||||
c.bench_function("parse ipv4", |b| b.iter(|| {
|
c.bench_function("parse ipv4", |b| {
|
||||||
"127.1.0.0/24".parse::<Ipv4Network>().unwrap()
|
b.iter(|| "127.1.0.0/24".parse::<Ipv4Network>().unwrap())
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_ipv6_benchmark(c: &mut Criterion) {
|
fn parse_ipv6_benchmark(c: &mut Criterion) {
|
||||||
c.bench_function("parse ipv6", |b| b.iter(|| {
|
c.bench_function("parse ipv6", |b| {
|
||||||
"FF01:0:0:17:0:0:0:2/64".parse::<Ipv6Network>().unwrap()
|
b.iter(|| "FF01:0:0:17:0:0:0:2/64".parse::<Ipv6Network>().unwrap())
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn contains_ipv4_benchmark(c: &mut Criterion) {
|
fn contains_ipv4_benchmark(c: &mut Criterion) {
|
||||||
c.bench_function("contains ipv4", |b| b.iter(|| {
|
c.bench_function("contains ipv4", |b| {
|
||||||
|
b.iter(|| {
|
||||||
let cidr = "74.125.227.0/25".parse::<Ipv4Network>().unwrap();
|
let cidr = "74.125.227.0/25".parse::<Ipv4Network>().unwrap();
|
||||||
cidr.contains(Ipv4Addr::new(74, 125, 227, 4))
|
cidr.contains(Ipv4Addr::new(74, 125, 227, 4))
|
||||||
}));
|
})
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn contains_ipv6_benchmark(c: &mut Criterion) {
|
fn contains_ipv6_benchmark(c: &mut Criterion) {
|
||||||
c.bench_function("contains ipv6", |b| b.iter(|| {
|
c.bench_function("contains ipv6", |b| {
|
||||||
|
b.iter(|| {
|
||||||
let cidr = "FF01:0:0:17:0:0:0:2/65".parse::<Ipv6Network>().unwrap();
|
let cidr = "FF01:0:0:17:0:0:0:2/65".parse::<Ipv6Network>().unwrap();
|
||||||
cidr.contains(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0x7fff, 0, 0, 0x2))
|
cidr.contains(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0x7fff, 0, 0, 0x2))
|
||||||
}));
|
})
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group!(benches, parse_ipv4_benchmark, parse_ipv6_benchmark, contains_ipv4_benchmark, contains_ipv6_benchmark);
|
criterion_group!(
|
||||||
|
benches,
|
||||||
|
parse_ipv4_benchmark,
|
||||||
|
parse_ipv6_benchmark,
|
||||||
|
contains_ipv4_benchmark,
|
||||||
|
contains_ipv6_benchmark
|
||||||
|
);
|
||||||
criterion_main!(benches);
|
criterion_main!(benches);
|
||||||
|
@ -40,8 +40,7 @@ pub fn cidr_parts(cidr: &str) -> Result<(&str, Option<&str>), IpNetworkError> {
|
|||||||
"CIDR must contain a single '/': {}",
|
"CIDR must contain a single '/': {}",
|
||||||
cidr
|
cidr
|
||||||
)));
|
)));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Handle the case when cidr has exactly one slash
|
// Handle the case when cidr has exactly one slash
|
||||||
return Ok((ip, Some(&prefix[1..])));
|
return Ok((ip, Some(&prefix[1..])));
|
||||||
}
|
}
|
||||||
|
125
src/ipv4.rs
125
src/ipv4.rs
@ -32,6 +32,7 @@ impl Serialize for Ipv4Network {
|
|||||||
|
|
||||||
impl Ipv4Network {
|
impl Ipv4Network {
|
||||||
/// Constructs a new `Ipv4Network` from any `Ipv4Addr` and a prefix denoting the network size.
|
/// Constructs a new `Ipv4Network` from any `Ipv4Addr` and a prefix denoting the network size.
|
||||||
|
///
|
||||||
/// If the prefix is larger than 32 this will return an `IpNetworkError::InvalidPrefix`.
|
/// If the prefix is larger than 32 this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
pub fn new(addr: Ipv4Addr, prefix: u8) -> Result<Ipv4Network, IpNetworkError> {
|
pub fn new(addr: Ipv4Addr, prefix: u8) -> Result<Ipv4Network, IpNetworkError> {
|
||||||
if prefix > IPV4_BITS {
|
if prefix > IPV4_BITS {
|
||||||
@ -41,6 +42,21 @@ impl Ipv4Network {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructs a new `Ipv4Network` from a network address and a network mask.
|
||||||
|
///
|
||||||
|
/// If the netmask is not valid this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
|
pub fn with_netmask(
|
||||||
|
netaddr: Ipv4Addr,
|
||||||
|
netmask: Ipv4Addr,
|
||||||
|
) -> Result<Ipv4Network, IpNetworkError> {
|
||||||
|
let prefix = ipv4_mask_to_prefix(netmask)?;
|
||||||
|
let net = Self {
|
||||||
|
addr: netaddr,
|
||||||
|
prefix,
|
||||||
|
};
|
||||||
|
Ok(net)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns an iterator over `Ipv4Network`. Each call to `next` will return the next
|
/// Returns an iterator over `Ipv4Network`. Each call to `next` will return the next
|
||||||
/// `Ipv4Addr` in the given network. `None` will be returned when there are no more
|
/// `Ipv4Addr` in the given network. `None` will be returned when there are no more
|
||||||
/// addresses.
|
/// addresses.
|
||||||
@ -70,13 +86,9 @@ impl Ipv4Network {
|
|||||||
|
|
||||||
/// Checks if the given `Ipv4Network` is partly contained in other.
|
/// Checks if the given `Ipv4Network` is partly contained in other.
|
||||||
pub fn overlaps(self, other: Ipv4Network) -> bool {
|
pub fn overlaps(self, other: Ipv4Network) -> bool {
|
||||||
other.contains(self.ip()) || (
|
other.contains(self.ip())
|
||||||
other.contains(self.broadcast()) || (
|
|| (other.contains(self.broadcast())
|
||||||
self.contains(other.ip()) || (
|
|| (self.contains(other.ip()) || (self.contains(other.broadcast()))))
|
||||||
self.contains(other.broadcast())
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the mask for this `Ipv4Network`.
|
/// Returns the mask for this `Ipv4Network`.
|
||||||
@ -261,6 +273,7 @@ impl Iterator for Ipv4NetworkIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a `Ipv4Addr` network mask into a prefix.
|
/// Converts a `Ipv4Addr` network mask into a prefix.
|
||||||
|
///
|
||||||
/// If the mask is invalid this will return an `IpNetworkError::InvalidPrefix`.
|
/// If the mask is invalid this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
pub fn ipv4_mask_to_prefix(mask: Ipv4Addr) -> Result<u8, IpNetworkError> {
|
pub fn ipv4_mask_to_prefix(mask: Ipv4Addr) -> Result<u8, IpNetworkError> {
|
||||||
let mask = u32::from(mask);
|
let mask = u32::from(mask);
|
||||||
@ -453,6 +466,24 @@ mod test {
|
|||||||
assert!(prefix.is_err());
|
assert!(prefix.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ipv4network_with_netmask() {
|
||||||
|
{
|
||||||
|
// Positive test-case.
|
||||||
|
let addr = Ipv4Addr::new(127, 0, 0, 1);
|
||||||
|
let mask = Ipv4Addr::new(255, 0, 0, 0);
|
||||||
|
let net = Ipv4Network::with_netmask(addr, mask).unwrap();
|
||||||
|
let expected = Ipv4Network::new(Ipv4Addr::new(127, 0, 0, 1), 8).unwrap();
|
||||||
|
assert_eq!(net, expected);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// Negative test-case.
|
||||||
|
let addr = Ipv4Addr::new(127, 0, 0, 1);
|
||||||
|
let mask = Ipv4Addr::new(255, 0, 255, 0);
|
||||||
|
Ipv4Network::with_netmask(addr, mask).unwrap_err();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ipv4network_from_ipv4addr() {
|
fn ipv4network_from_ipv4addr() {
|
||||||
let net = Ipv4Network::from(Ipv4Addr::new(127, 0, 0, 1));
|
let net = Ipv4Network::from(Ipv4Addr::new(127, 0, 0, 1));
|
||||||
@ -477,14 +508,44 @@ mod test {
|
|||||||
fn test_is_subnet_of() {
|
fn test_is_subnet_of() {
|
||||||
let mut test_cases: HashMap<(Ipv4Network, Ipv4Network), bool> = HashMap::new();
|
let mut test_cases: HashMap<(Ipv4Network, Ipv4Network), bool> = HashMap::new();
|
||||||
|
|
||||||
test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.1.0/24".parse().unwrap()), false);
|
test_cases.insert(
|
||||||
test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.0.0/24".parse().unwrap()), true);
|
(
|
||||||
test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.1.0/24".parse().unwrap()), false);
|
"10.0.0.0/30".parse().unwrap(),
|
||||||
test_cases.insert(("10.0.1.0/24".parse().unwrap(), "10.0.0.0/30".parse().unwrap()), false);
|
"10.0.1.0/24".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"10.0.0.0/30".parse().unwrap(),
|
||||||
|
"10.0.0.0/24".parse().unwrap(),
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"10.0.0.0/30".parse().unwrap(),
|
||||||
|
"10.0.1.0/24".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"10.0.1.0/24".parse().unwrap(),
|
||||||
|
"10.0.0.0/30".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
for (key, val) in test_cases.iter() {
|
for (key, val) in test_cases.iter() {
|
||||||
let (src, dest) = (key.0, key.1);
|
let (src, dest) = (key.0, key.1);
|
||||||
assert_eq!(src.is_subnet_of(dest), *val, "testing with {} and {}", src, dest);
|
assert_eq!(
|
||||||
|
src.is_subnet_of(dest),
|
||||||
|
*val,
|
||||||
|
"testing with {} and {}",
|
||||||
|
src,
|
||||||
|
dest
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -492,14 +553,44 @@ mod test {
|
|||||||
fn test_is_supernet_of() {
|
fn test_is_supernet_of() {
|
||||||
let mut test_cases: HashMap<(Ipv4Network, Ipv4Network), bool> = HashMap::new();
|
let mut test_cases: HashMap<(Ipv4Network, Ipv4Network), bool> = HashMap::new();
|
||||||
|
|
||||||
test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.1.0/24".parse().unwrap()), false);
|
test_cases.insert(
|
||||||
test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.0.0/24".parse().unwrap()), false);
|
(
|
||||||
test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.1.0/24".parse().unwrap()), false);
|
"10.0.0.0/30".parse().unwrap(),
|
||||||
test_cases.insert(("10.0.0.0/24".parse().unwrap(), "10.0.0.0/30".parse().unwrap()), true);
|
"10.0.1.0/24".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"10.0.0.0/30".parse().unwrap(),
|
||||||
|
"10.0.0.0/24".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"10.0.0.0/30".parse().unwrap(),
|
||||||
|
"10.0.1.0/24".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"10.0.0.0/24".parse().unwrap(),
|
||||||
|
"10.0.0.0/30".parse().unwrap(),
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
for (key, val) in test_cases.iter() {
|
for (key, val) in test_cases.iter() {
|
||||||
let (src, dest) = (key.0, key.1);
|
let (src, dest) = (key.0, key.1);
|
||||||
assert_eq!(src.is_supernet_of(dest), *val, "testing with {} and {}", src, dest);
|
assert_eq!(
|
||||||
|
src.is_supernet_of(dest),
|
||||||
|
*val,
|
||||||
|
"testing with {} and {}",
|
||||||
|
src,
|
||||||
|
dest
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
122
src/ipv6.rs
122
src/ipv6.rs
@ -33,6 +33,7 @@ impl Serialize for Ipv6Network {
|
|||||||
|
|
||||||
impl Ipv6Network {
|
impl Ipv6Network {
|
||||||
/// Constructs a new `Ipv6Network` from any `Ipv6Addr` and a prefix denoting the network size.
|
/// Constructs a new `Ipv6Network` from any `Ipv6Addr` and a prefix denoting the network size.
|
||||||
|
///
|
||||||
/// If the prefix is larger than 128 this will return an `IpNetworkError::InvalidPrefix`.
|
/// If the prefix is larger than 128 this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
pub fn new(addr: Ipv6Addr, prefix: u8) -> Result<Ipv6Network, IpNetworkError> {
|
pub fn new(addr: Ipv6Addr, prefix: u8) -> Result<Ipv6Network, IpNetworkError> {
|
||||||
if prefix > IPV6_BITS {
|
if prefix > IPV6_BITS {
|
||||||
@ -42,6 +43,18 @@ impl Ipv6Network {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructs a new `Ipv6Network` from a network address and a network mask.
|
||||||
|
///
|
||||||
|
/// If the netmask is not valid this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
|
pub fn with_netmask(netaddr: Ipv6Addr, netmask: Ipv6Addr) -> Result<Self, IpNetworkError> {
|
||||||
|
let prefix = ipv6_mask_to_prefix(netmask)?;
|
||||||
|
let net = Self {
|
||||||
|
addr: netaddr,
|
||||||
|
prefix,
|
||||||
|
};
|
||||||
|
Ok(net)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns an iterator over `Ipv6Network`. Each call to `next` will return the next
|
/// Returns an iterator over `Ipv6Network`. Each call to `next` will return the next
|
||||||
/// `Ipv6Addr` in the given network. `None` will be returned when there are no more
|
/// `Ipv6Addr` in the given network. `None` will be returned when there are no more
|
||||||
/// addresses.
|
/// addresses.
|
||||||
@ -118,13 +131,9 @@ impl Ipv6Network {
|
|||||||
|
|
||||||
/// Checks if the given `Ipv6Network` is partly contained in other.
|
/// Checks if the given `Ipv6Network` is partly contained in other.
|
||||||
pub fn overlaps(self, other: Ipv6Network) -> bool {
|
pub fn overlaps(self, other: Ipv6Network) -> bool {
|
||||||
other.contains(self.ip()) || (
|
other.contains(self.ip())
|
||||||
other.contains(self.broadcast()) || (
|
|| (other.contains(self.broadcast())
|
||||||
self.contains(other.ip()) || (
|
|| (self.contains(other.ip()) || (self.contains(other.broadcast()))))
|
||||||
self.contains(other.broadcast())
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the mask for this `Ipv6Network`.
|
/// Returns the mask for this `Ipv6Network`.
|
||||||
@ -370,6 +379,25 @@ mod test {
|
|||||||
assert!(prefix.is_err());
|
assert!(prefix.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ipv6network_with_netmask() {
|
||||||
|
{
|
||||||
|
// Positive test-case.
|
||||||
|
let addr = Ipv6Addr::new(0xff01, 0, 0, 0x17, 0, 0, 0, 0x2);
|
||||||
|
let mask = Ipv6Addr::new(0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0);
|
||||||
|
let net = Ipv6Network::with_netmask(addr, mask).unwrap();
|
||||||
|
let expected =
|
||||||
|
Ipv6Network::new(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0, 0, 0, 0x2), 48).unwrap();
|
||||||
|
assert_eq!(net, expected);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// Negative test-case.
|
||||||
|
let addr = Ipv6Addr::new(0xff01, 0, 0, 0x17, 0, 0, 0, 0x2);
|
||||||
|
let mask = Ipv6Addr::new(0, 0, 0xffff, 0xffff, 0, 0, 0, 0);
|
||||||
|
Ipv6Network::with_netmask(addr, mask).unwrap_err();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn iterator_v6() {
|
fn iterator_v6() {
|
||||||
let cidr: Ipv6Network = "2001:db8::/126".parse().unwrap();
|
let cidr: Ipv6Network = "2001:db8::/126".parse().unwrap();
|
||||||
@ -459,14 +487,44 @@ mod test {
|
|||||||
fn test_is_subnet_of() {
|
fn test_is_subnet_of() {
|
||||||
let mut test_cases: HashMap<(Ipv6Network, Ipv6Network), bool> = HashMap::new();
|
let mut test_cases: HashMap<(Ipv6Network, Ipv6Network), bool> = HashMap::new();
|
||||||
|
|
||||||
test_cases.insert(("2000:999::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false);
|
test_cases.insert(
|
||||||
test_cases.insert(("2000:aaa::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), true);
|
(
|
||||||
test_cases.insert(("2000:bbb::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false);
|
"2000:999::/56".parse().unwrap(),
|
||||||
test_cases.insert(("2000:aaa::/48".parse().unwrap(), "2000:aaa::/56".parse().unwrap()), false);
|
"2000:aaa::/48".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"2000:aaa::/56".parse().unwrap(),
|
||||||
|
"2000:aaa::/48".parse().unwrap(),
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"2000:bbb::/56".parse().unwrap(),
|
||||||
|
"2000:aaa::/48".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"2000:aaa::/48".parse().unwrap(),
|
||||||
|
"2000:aaa::/56".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
for (key, val) in test_cases.iter() {
|
for (key, val) in test_cases.iter() {
|
||||||
let (src, dest) = (key.0, key.1);
|
let (src, dest) = (key.0, key.1);
|
||||||
assert_eq!(src.is_subnet_of(dest), *val, "testing with {} and {}", src, dest);
|
assert_eq!(
|
||||||
|
src.is_subnet_of(dest),
|
||||||
|
*val,
|
||||||
|
"testing with {} and {}",
|
||||||
|
src,
|
||||||
|
dest
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -474,14 +532,44 @@ mod test {
|
|||||||
fn test_is_supernet_of() {
|
fn test_is_supernet_of() {
|
||||||
let mut test_cases: HashMap<(Ipv6Network, Ipv6Network), bool> = HashMap::new();
|
let mut test_cases: HashMap<(Ipv6Network, Ipv6Network), bool> = HashMap::new();
|
||||||
|
|
||||||
test_cases.insert(("2000:999::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false);
|
test_cases.insert(
|
||||||
test_cases.insert(("2000:aaa::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false);
|
(
|
||||||
test_cases.insert(("2000:bbb::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false);
|
"2000:999::/56".parse().unwrap(),
|
||||||
test_cases.insert(("2000:aaa::/48".parse().unwrap(), "2000:aaa::/56".parse().unwrap()), true);
|
"2000:aaa::/48".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"2000:aaa::/56".parse().unwrap(),
|
||||||
|
"2000:aaa::/48".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"2000:bbb::/56".parse().unwrap(),
|
||||||
|
"2000:aaa::/48".parse().unwrap(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
test_cases.insert(
|
||||||
|
(
|
||||||
|
"2000:aaa::/48".parse().unwrap(),
|
||||||
|
"2000:aaa::/56".parse().unwrap(),
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
for (key, val) in test_cases.iter() {
|
for (key, val) in test_cases.iter() {
|
||||||
let (src, dest) = (key.0, key.1);
|
let (src, dest) = (key.0, key.1);
|
||||||
assert_eq!(src.is_supernet_of(dest), *val, "testing with {} and {}", src, dest);
|
assert_eq!(
|
||||||
|
src.is_supernet_of(dest),
|
||||||
|
*val,
|
||||||
|
"testing with {} and {}",
|
||||||
|
src,
|
||||||
|
dest
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
src/lib.rs
15
src/lib.rs
@ -63,6 +63,14 @@ impl IpNetwork {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructs a new `IpNetwork` from a network address and a network mask.
|
||||||
|
///
|
||||||
|
/// If the netmask is not valid this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
|
pub fn with_netmask(netaddr: IpAddr, netmask: IpAddr) -> Result<Self, IpNetworkError> {
|
||||||
|
let prefix = ip_mask_to_prefix(netmask)?;
|
||||||
|
Self::new(netaddr, prefix)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the IP part of a given `IpNetwork`
|
/// Returns the IP part of a given `IpNetwork`
|
||||||
pub fn ip(&self) -> IpAddr {
|
pub fn ip(&self) -> IpAddr {
|
||||||
match *self {
|
match *self {
|
||||||
@ -313,8 +321,9 @@ mod test {
|
|||||||
fn deserialize_from_serde_json_value() {
|
fn deserialize_from_serde_json_value() {
|
||||||
use super::*;
|
use super::*;
|
||||||
let network = IpNetwork::from_str("0.0.0.0/0").unwrap();
|
let network = IpNetwork::from_str("0.0.0.0/0").unwrap();
|
||||||
let val: serde_json::value::Value = serde_json::from_str(&serde_json::to_string(&network).unwrap()).unwrap();
|
let val: serde_json::value::Value =
|
||||||
let _deser: IpNetwork =
|
serde_json::from_str(&serde_json::to_string(&network).unwrap()).unwrap();
|
||||||
serde_json::from_value(val).expect("Fails to deserialize from json_value::value::Value");
|
let _deser: IpNetwork = serde_json::from_value(val)
|
||||||
|
.expect("Fails to deserialize from json_value::value::Value");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user