Created new errors for CIDR / Subnet mask generation

This commit is contained in:
Luke Else 2023-04-10 11:13:36 +01:00
parent d1d4bc02ca
commit a067a3e349

View File

@ -1,10 +1,11 @@
use std::{str::FromStr};
use ip::IpAddr;
use self::ip::InvalidIPErr;
pub mod ip;
#[derive(Debug, PartialEq, Eq)]
pub struct InvalidCIDRErr;
#[allow(unused)]
pub struct Network {
network_address: IpAddr,
@ -13,8 +14,8 @@ pub struct Network {
}
impl Network {
// pub fn new(given_address: IpAddr, CIDR: u8) -> Network {
// //Generate subnet Mask
// pub fn new(given_address: &IpAddr, CIDR: u8) -> Network {
// }
@ -27,9 +28,9 @@ impl Network {
///
/// >> IpAddr::V4(255, 255, 252, 0)
/// ```
pub fn gen_subnet_mask(mut CIDR: u8) -> Result<IpAddr, InvalidIPErr> {
pub fn gen_subnet_mask(mut CIDR: u8) -> Result<IpAddr, InvalidCIDRErr> {
if CIDR > 32 {
return Err(InvalidIPErr)
return Err(InvalidCIDRErr)
}
let mut oct: [u8; 4] = [0; 4];
@ -52,7 +53,6 @@ impl Network {
Ok(IpAddr::V4(oct[0], oct[1], oct[2], oct[3]))
}
//pub fn generate_subnets(self) -> Vec<Network> {}
//fn get_net_id(self) -> u8 {}
}
enum NetworkClassBits {
@ -123,6 +123,6 @@ mod tests {
assert_eq!(Network::gen_subnet_mask(24).unwrap(), IpAddr::V4(255, 255, 255, 0));
assert_eq!(Network::gen_subnet_mask(0).unwrap(), IpAddr::V4(0, 0, 0, 0));
assert_eq!(Network::gen_subnet_mask(4).unwrap(), IpAddr::V4(240, 0, 0, 0));
assert_eq!(Network::gen_subnet_mask(35).unwrap_err(), InvalidIPErr);
assert_eq!(Network::gen_subnet_mask(35).unwrap_err(), InvalidCIDRErr);
}
}