subnet_calculator/src/ip.rs

259 lines
7.2 KiB
Rust

use super::NetworkingErr;
use std::str::FromStr;
#[derive(Debug, PartialEq, Eq)]
pub struct InvalidIPErr;
/// Ip address enum that includes associated type
///
/// # Example
/// ```
/// //Loopback Addresses:
/// use subnet_calculator::ip::IpAddr;
/// use std::str::FromStr;
///
/// IpAddr::V4(127, 0, 0, 1);
/// IpAddr::V6(String::from("::1"));
/// ```
#[allow(unused)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum IpAddr {
V4(u8, u8, u8, u8),
V6(String),
}
impl IpAddr {
/// Function that generates ar array of 4 octets / error
/// from an IpAddr
///
/// # Limitation
/// Currently only works for IPs of type IPv4
/// # Example
/// ```
/// use subnet_calculator::ip::IpAddr;
///
/// let ip: IpAddr = IpAddr::V4(127, 0, 0, 1);
///
/// let ip_add: [u8; 4] = ip.to_arr().unwrap();
/// assert_eq!(ip_add, [127, 0, 0, 1]);
/// ```
pub fn to_arr(&self) -> Result<[u8; 4], NetworkingErr> {
match self {
IpAddr::V4(o1, o2, o3, o4) => Ok([*o1, *o2, *o3, *o4]),
_ => Err(NetworkingErr::InvalidIPErr),
}
}
/// Function that generates a IpAddr / Err from an array of
/// a octests
///
/// # Limitation
/// Currently only works for IPs of type IPv4
/// # Example
/// ```
/// use subnet_calculator::ip::IpAddr;
///
/// let ip_add: [u8; 4] = [127, 0, 0, 1];
///
/// let ip: IpAddr = IpAddr::from_arr(&ip_add).unwrap();
/// assert_eq!(ip, IpAddr::V4(127, 0, 0, 1));
/// ```
pub fn from_arr(arr: &[u8; 4]) -> Result<IpAddr, NetworkingErr> {
Ok(IpAddr::V4(arr[0], arr[1], arr[2], arr[3]))
}
}
impl From<IpAddr> for u32 {
/// Function that converts an Ip address
/// into an Unsigned 32 bit integer.
///
/// # Limitation
/// Currently only works for IPs of type IPv4
/// # Example
/// ```
/// use subnet_calculator::ip::IpAddr;
///
/// let ip: IpAddr = IpAddr::V4(127, 0, 0, 1);
///
/// let ip_u32: u32 = u32::from(ip);
/// assert_eq!(ip_u32, 2130706433);
/// ```
fn from(ip: IpAddr) -> Self {
u32::from_be_bytes(ip.to_arr().unwrap())
}
}
impl From<u32> for IpAddr {
/// Function that converts an Unsigned 32-bit Ip address
/// into an IpAddr
///
/// # Limitation
/// Currently only works for IPs of type IPv4
/// # Example
/// ```
/// use subnet_calculator::ip::IpAddr;
///
/// let ip: u32 = 2_130_706_433;
///
/// let ip_addr: IpAddr = IpAddr::from(ip);
/// assert_eq!(ip_addr, IpAddr::V4(127, 0, 0, 1));
/// ```
fn from(ip: u32) -> Self {
IpAddr::from_arr(&(ip.to_be_bytes() as [u8; 4])).unwrap()
}
}
impl ToString for IpAddr {
/// Function that returns an IP address in string form
fn to_string(&self) -> String {
match self {
IpAddr::V4(oct1, oct2, oct3, oct4) => format!("{}.{}.{}.{}", oct1, oct2, oct3, oct4),
IpAddr::V6(addr) => format!("{}", addr),
}
}
}
impl FromStr for IpAddr {
type Err = NetworkingErr;
/// Function that generates a IpAddr / Err from a string
///
/// # Limitation
/// Currently only works for IPs of type IPv4
/// # Example
/// ```
/// use subnet_calculator::ip::IpAddr;
/// use std::str::FromStr;
///
/// let ip: &str = "127.0.0.1";
///
/// let parsed_ip: IpAddr = IpAddr::from_str(ip).unwrap();
/// assert_eq!(parsed_ip, IpAddr::V4(127,0,0,1));
/// ```
fn from_str(s: &str) -> Result<Self, Self::Err> {
let split_ip = s.split('.');
if split_ip.clone().count() != 4 {
//Invalid IP address entered
return Err(super::NetworkingErr::InvalidIPErr);
}
let mut ip: [u8; 4] = Default::default();
//Go through each octet and ensure it can be parsed;
for (i, oct) in split_ip.into_iter().enumerate() {
if i > ip.len() {
//Ip string is out of the range of the 4 octets in an IPv4 Address
return Err(NetworkingErr::InvalidIPErr);
}
match oct.parse::<u8>() {
Ok(parsed_oct) => ip[i] = parsed_oct,
Err(_) => return Err(NetworkingErr::InvalidIPErr),
}
}
Ok(IpAddr::from_arr(&ip)?)
}
}
/// Function that takes in an IP address and then prints a formatted string to the CLI
///
/// # Example
/// ```
/// use subnet_calculator::ip::{IpAddr, print_ip};
///
/// let ip = IpAddr::V4(127, 0, 0, 1);
///
/// print_ip(ip);
/// //Output: IP Address: 127.0.0.1
/// ```
#[allow(unused)]
pub fn print_ip(ip_address: IpAddr) {
println!("IP Address: {}", ip_address.to_string())
}
mod tests {
#[test]
/// Tests if an IP is converted to a string
/// correctly using the ToString trait
fn ip_to_string() {
use super::*;
let ip = IpAddr::V4(192, 168, 0, 1);
assert_eq!(ip.to_string(), "192.168.0.1")
}
#[test]
/// Tests if an IP is converted from a string
/// correctly using the FromString trait
fn string_to_ip() {
use super::*;
let ip = "127.0.0.1";
assert_eq!(IpAddr::from_str(ip).unwrap(), IpAddr::V4(127, 0, 0, 1))
}
#[test]
#[should_panic]
///Tests if an invalid Ip will cause a panic when
/// converting from a string to an IpAddr
fn invalid_string_to_ip() {
use super::*;
let ip = "127.0.0.0.1";
IpAddr::from_str(ip).unwrap();
assert_eq!(IpAddr::from_str(ip), Err(NetworkingErr::InvalidIPErr))
}
#[test]
/// Tests the conversion of an IPv4 Address to
/// an array
fn ipaddr_to_arr() {
use super::*;
let mut ip_addr = IpAddr::V4(127, 0, 0, 1);
assert_eq!(ip_addr.to_arr().unwrap(), [127, 0, 0, 1]);
ip_addr = IpAddr::V6("::".to_string());
assert_eq!(ip_addr.to_arr().unwrap_err(), NetworkingErr::InvalidIPErr);
}
#[test]
/// Tests the conversion of an array to
// an IPv4 Address
fn arr_to_ipaddr() {
use super::*;
let ip_addr: [u8; 4] = [127, 0, 0, 1];
assert_eq!(
IpAddr::from_arr(&ip_addr).unwrap(),
IpAddr::V4(127, 0, 0, 1)
);
}
#[test]
/// Tests the conversion of an IPv4 Address to
/// an unsigned 32-bit integer
fn ipaddr_to_u32() {
use super::*;
let ip = IpAddr::V4(0, 0, 0, 0);
assert_eq!(0, u32::from(ip));
let ip = IpAddr::V4(127, 0, 0, 1);
assert_eq!(2_130_706_433, u32::from(ip));
let ip = IpAddr::V4(10, 10, 10, 0);
assert_eq!(168_430_080, u32::from(ip));
let ip = IpAddr::V4(255, 255, 255, 255);
assert_eq!(u32::MAX, u32::from(ip));
}
#[test]
/// Tests the conversion of an u32 IPv4 Address to
/// an IpAddr
fn u32_to_ipaddr() {
use super::*;
let ip = u32::from(IpAddr::V4(0, 0, 0, 0));
assert_eq!(IpAddr::V4(0, 0, 0, 0), IpAddr::from(ip));
let ip = u32::from(IpAddr::V4(127, 0, 0, 1));
assert_eq!(IpAddr::V4(127, 0, 0, 1), IpAddr::from(ip));
let ip = u32::from(IpAddr::V4(10, 10, 10, 0));
assert_eq!(IpAddr::V4(10, 10, 10, 0), IpAddr::from(ip));
let ip = u32::from(IpAddr::V4(255, 255, 255, 255));
assert_eq!(IpAddr::V4(255, 255, 255, 255), IpAddr::from(ip));
}
}