Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added needed traits to NetworkSize #175

Merged
merged 3 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ use std::{convert::TryFrom, fmt, net::IpAddr, str::FromStr};
mod common;
mod ipv4;
mod ipv6;
mod size;

pub use crate::common::IpNetworkError;
pub use crate::ipv4::Ipv4NetworkIterator;
pub use crate::ipv4::{ipv4_mask_to_prefix, Ipv4Network};
pub use crate::ipv6::Ipv6NetworkIterator;
pub use crate::ipv6::{ipv6_mask_to_prefix, Ipv6Network};
pub use crate::size::{NetworkIsTooLargeError, NetworkSize};

/// Represents a generic network range. This type can have two variants:
/// the v4 and the v6 case.
Expand Down Expand Up @@ -117,13 +119,6 @@ impl schemars::JsonSchema for IpNetwork {
}
}

/// Represents a generic network size. For IPv4, the max size is a u32 and for IPv6, it is a u128
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum NetworkSize {
Comment on lines -121 to -122
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This auto-derivation is wrong. I implemented Eq correctly in size.rs.

V4(u32),
V6(u128),
}

impl IpNetwork {
/// Constructs a new `IpNetwork` from a given `IpAddr` and a prefix denoting the
/// network size. If the prefix is larger than 32 (for IPv4) or 128 (for IPv6), this
Expand Down
153 changes: 153 additions & 0 deletions src/size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
use std::{cmp::Ordering, error::Error, fmt::Display};

/// Represents a generic network size. For IPv4, the max size is a u32 and for IPv6, it is a u128
#[derive(Debug, Clone, Copy, Hash)]
pub enum NetworkSize {
V4(u32),
V6(u128),
}
use NetworkSize::*;

// Conversions

impl From<u128> for NetworkSize {
fn from(value: u128) -> Self {
V6(value)
}
}

impl From<u32> for NetworkSize {
fn from(value: u32) -> Self {
V4(value)
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// Cannot convert an IPv6 network size to a u32 as it is a 128-bit value.
pub struct NetworkIsTooLargeError;

impl Display for NetworkIsTooLargeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Network is too large to fit into an unsigned 32-bit integer!")
}
}
impl Error for NetworkIsTooLargeError {}

impl TryInto<u32> for NetworkSize {
type Error = NetworkIsTooLargeError;
fn try_into(self) -> Result<u32, Self::Error> {
match self {
V4(a) => Ok(a),
V6(_) => Err(NetworkIsTooLargeError),
}
}
}

impl Into<u128> for NetworkSize {
fn into(self) -> u128 {
match self {
V4(a) => a as u128,
V6(a) => a,
}
}
}

// Equality/comparisons

impl PartialEq for NetworkSize {
fn eq(&self, other: &Self) -> bool {
let a: u128 = (*self).into();
let b: u128 = (*other).into();
a == b
}
}

impl Ord for NetworkSize {
fn cmp(&self, other: &Self) -> Ordering {
let a: u128 = (*self).into();
let b: u128 = (*other).into();
return a.cmp(&b);
}
}

impl PartialOrd for NetworkSize {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Eq for NetworkSize {}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_from_u128() {
let value: u128 = 100;
let ns = NetworkSize::from(value);
assert_eq!(ns, V6(100));
}

#[test]
fn test_from_u32() {
let value: u32 = 100;
let ns = NetworkSize::from(value);
assert_eq!(ns, V4(100));
}

#[test]
fn test_try_into_u32() {
let value: u32 = 100;
let ns = V4(value);
let result: Result<u32, _> = ns.try_into();
assert!(result.is_ok());
assert_eq!(result.unwrap(), value);
}

#[test]
fn test_try_into_u32_error() {
let value: u128 = u32::MAX as u128 + 1;
let ns = V6(value);
let result: Result<u32, _> = ns.try_into();
assert!(result.is_err());
}

#[test]
fn test_into_u128() {
let value: u32 = 100;
let ns = V4(value);
let result: u128 = ns.into();
assert_eq!(result, value as u128);
}

#[test]
fn test_eq() {
let ns1 = V4(100);
let ns2 = V4(100);
assert_eq!(ns1, ns2);

let ns1 = V6(100);
let ns2 = V6(100);
assert_eq!(ns1, ns2);

let ns1 = V4(100);
let ns2 = V6(100);
assert_eq!(ns1, ns2);
}

#[test]
fn test_cmp() {
let ns1 = V4(100);
let ns2 = V4(200);
assert!(ns1 < ns2);

let ns1 = V6(200);
let ns2 = V6(100);
assert!(ns1 > ns2);

let ns1 = V4(100);
let ns2 = V6(200);
assert!(ns1 < ns2);
}
}