Made alterations to functions using IP to make it use new functions

This commit is contained in:
Luke Else 2023-04-26 21:19:35 +01:00
parent d0da5f587f
commit 6fafa97d61
2 changed files with 12 additions and 11 deletions

View File

@ -99,7 +99,7 @@ impl FromStr for IpAddr {
Err(_) => return Err(NetworkingErr::InvalidIPErr)
}
}
Ok(IpAddr::V4(ip[0],ip[1],ip[2],ip[3]))
Ok(IpAddr::from_arr(&ip)?)
}
}

View File

@ -69,7 +69,8 @@ impl Network {
/// let network_class = Network::get_network_class(&[127, 0, 0, 1])?;
/// >>> NetworkClass::A
/// ```
fn get_network_class(network_address: &[u8; 4]) -> Result<NetworkClass, NetworkingErr> {
fn get_network_class(network_address: &IpAddr) -> Result<NetworkClass, NetworkingErr> {
let network_address = network_address.to_arr()?;
if network_address[0] > 224 {
return Err(NetworkingErr::InvalidIPErr)
}else if network_address[0] >= 192 {
@ -112,7 +113,7 @@ impl Network {
}
}
Ok(IpAddr::V4(oct[0], oct[1], oct[2], oct[3]))
Ok(IpAddr::from_arr(&oct)?)
}
//pub fn generate_subnets(self) -> Vec<Network> {}
}
@ -185,13 +186,13 @@ mod tests {
#[test]
fn test_network_class() {
use super::*;
assert_eq!(Network::get_network_class(&IpAddr::V4(127, 0, 0, 1).to_arr().unwrap()).unwrap(), NetworkClass::A);
assert_eq!(Network::get_network_class(&IpAddr::V4(172, 6, 8, 10).to_arr().unwrap()).unwrap(), NetworkClass::B);
assert_eq!(Network::get_network_class(&IpAddr::V4(192, 168, 0, 1).to_arr().unwrap()).unwrap(), NetworkClass::C);
assert_eq!(Network::get_network_class(&IpAddr::V4(10, 6, 8, 10).to_arr().unwrap()).unwrap(), NetworkClass::A);
assert_eq!(Network::get_network_class(&IpAddr::V4(225, 255, 255, 255).to_arr().unwrap()).unwrap_err(), NetworkingErr::InvalidIPErr);
assert_eq!(Network::get_network_class(&IpAddr::V4(225, 0, 0, 0).to_arr().unwrap()).unwrap_err(), NetworkingErr::InvalidIPErr);
assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 0).to_arr().unwrap()).unwrap_err(), NetworkingErr::InvalidIPErr);
assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 1).to_arr().unwrap()).unwrap_err(), NetworkingErr::InvalidIPErr);
assert_eq!(Network::get_network_class(&IpAddr::V4(127, 0, 0, 1)).unwrap(), NetworkClass::A);
assert_eq!(Network::get_network_class(&IpAddr::V4(172, 6, 8, 10)).unwrap(), NetworkClass::B);
assert_eq!(Network::get_network_class(&IpAddr::V4(192, 168, 0, 1)).unwrap(), NetworkClass::C);
assert_eq!(Network::get_network_class(&IpAddr::V4(10, 6, 8, 10)).unwrap(), NetworkClass::A);
assert_eq!(Network::get_network_class(&IpAddr::V4(225, 255, 255, 255)).unwrap_err(), NetworkingErr::InvalidIPErr);
assert_eq!(Network::get_network_class(&IpAddr::V4(225, 0, 0, 0)).unwrap_err(), NetworkingErr::InvalidIPErr);
assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 0)).unwrap_err(), NetworkingErr::InvalidIPErr);
assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 1)).unwrap_err(), NetworkingErr::InvalidIPErr);
}
}